The parsed console arguments.
The parsed arguments provide access to the options and arguments passed via
the command line. Usually you can construct an Webmozart\Console\Api\Args\Args
instance by
parsing a Webmozart\Console\Api\Args\RawArgs
instance with an Webmozart\Console\Api\Args\ArgsParser
:
$format = ArgsFormat::build() ->addCommandName(new CommandName('server')) ->addCommandName(new CommandName('add')) ->addOption(new Option('port', 'p', Option::VALUE_REQUIRED | Option::INTEGER)) ->addArgument(new Argument('host', Argument::REQUIRED)) ->getFormat();
$args = $parser->parseArgs($rawArgs, $format);
The Webmozart\Console\Api\Args\Format\ArgsFormat
defines which rules the console arguments must adhere
to.
You can also create Webmozart\Console\Api\Args\Args
instances manually. This is especially
useful in tests:
$format = ArgsFormat::build() ->addOption(new Option('port', 'p', Option::VALUE_REQUIRED | Option::INTEGER)) ->addArgument(new Argument('host', Argument::REQUIRED)) ->getFormat();
$args = new Args($format);
$args->setOption('port', 80);
$args->setArgument('host', 'localhost');
Methods summary
public
|
|
public
string|null
|
#
getScriptName( )
Returns the PHP script as it was called on the console.
Returns the PHP script as it was called on the console.
Returns
string|null The script name or null if no script name is
available.
|
public
Webmozart\Console\Api\Args\Format\CommandName[]
|
#
getCommandNames( )
Returns the command names as array.
Returns the command names as array.
Returns
|
public
Webmozart\Console\Api\Args\Format\CommandOption[]
|
#
getCommandOptions( )
Returns the command options as array.
Returns the command options as array.
Returns
|
public
mixed
|
#
getOption( string $name )
Returns an option.
If the option accepts a value, the value set for that option is returned.
If no value was set with Webmozart\Console\Api\Args\Args::setOption() , the default value of the
option is returned.j
If the option accepts no value, the method returns true if the option
was set and false otherwise.
Parameters
- $name
- The long or short option name.
Returns
mixed The option value or true /false for options without
values.
Throws
|
public
array
|
#
getOptions( boolean $includeDefaults = true )
Returns all options.
By default, this method also includes the default values set for options
with values. You can disable this behavior by passing false for
$includeDefaults .
Parameters
- $includeDefaults
Whether to return the default values for
options that were not set.
Returns
array The option values and true /false for options without
values.
See
|
public
static
|
#
setOption( string $name, mixed $value = true )
Sets an option.
For options with values, you can pass the value in the second argument.
The value is converted to the type defined by the argument format.
For options without values, you can omit the second argument. Optionally,
you can pass true /false explicitly to enable/disable the option.
Parameters
- $name
- The long or short option name.
- $value
- The value to set for the option.
Returns
static The current instance.
Throws
|
public
static
|
#
addOptions( array $options )
Sets the values of multiple options.
Sets the values of multiple options.
The existing options are preserved.
Parameters
- $options
The options indexed by their long or short names
and their values.
Returns
static The current instance.
See
|
public
static
|
#
setOptions( array $options )
Sets the values of multiple options.
Sets the values of multiple options.
The existing options are unset.
Parameters
- $options
The options indexed by their long or short names
and their values.
Returns
static The current instance.
See
|
public
boolean
|
#
isOptionSet( string $name )
Returns whether an option is set.
Returns whether an option is set.
Parameters
- $name
- The long or short option name.
Returns
boolean Returns true if the option is set and false otherwise.
|
public
boolean
|
#
isOptionDefined( string $name )
Returns whether an option is defined in the format.
Returns whether an option is defined in the format.
Parameters
- $name
- The long or short option name.
Returns
boolean Returns true if the option exists and false otherwise.
|
public
mixed
|
#
getArgument( string|integer $name )
Returns the value of an argument.
Returns the value of an argument.
If the argument is not set, the default value configured in the argument
format is returned.
Parameters
- $name
The argument name or its 0-based position in the
argument list.
Returns
mixed The value of the argument.
Throws
|
public
array
|
#
getArguments( boolean $includeDefaults = true )
Returns the values of all arguments.
Returns the values of all arguments.
By default, this method also includes the default values of unset
arguments. You can disable this behavior by passing false for
$includeDefaults .
Parameters
- $includeDefaults
Whether to return the default values for
arguments that were not set.
Returns
array The argument values.
See
|
public
static
|
#
setArgument( string|integer $name, mixed $value )
Sets the value of an argument.
Sets the value of an argument.
The value is converted to the type defined by the argument format.
Parameters
- $name
The argument name or its 0-based position in the
argument list.
- $value
- The value of the argument.
Returns
static The current instance.
Throws
|
public
static
|
#
addArguments( array $arguments )
Sets the values of multiple arguments.
Sets the values of multiple arguments.
The existing arguments are preserved.
Parameters
- $arguments
The argument values indexed by the argument names
or their 0-based positions in the argument list.
Returns
static The current instance.
See
|
public
static
|
#
setArguments( array $arguments )
Sets the values of multiple arguments.
Sets the values of multiple arguments.
The existing arguments are unset.
Parameters
- $arguments
The argument values indexed by the argument names
or their 0-based positions in the argument list.
Returns
static The current instance.
See
|
public
boolean
|
#
isArgumentSet( string|integer $name )
Returns whether an argument is set.
Returns whether an argument is set.
Parameters
- $name
The argument name or its 0-based position in the
argument list.
Returns
boolean Returns true if the argument is set and false otherwise.
|
public
boolean
|
#
isArgumentDefined( string|integer $name )
Returns whether an argument is defined in the format.
Returns whether an argument is defined in the format.
Parameters
- $name
The argument name or its 0-based position in the
argument list.
Returns
boolean Returns true if the argument exists and false otherwise.
|
public
Webmozart\Console\Api\Args\Format\ArgsFormat
|
#
getFormat( )
Returns the format of the console arguments.
Returns the format of the console arguments.
Returns
|
public
Webmozart\Console\Api\Args\RawArgs
|
#
getRawArgs( )
Returns the raw console arguments.
Returns the raw console arguments.
Returns
|