PHP Class Webmozart\Console\Api\Args\Args

The parsed arguments provide access to the options and arguments passed via the command line. Usually you can construct an {@link Args} instance by parsing a {@link RawArgs} instance with an {@link ArgsParser}: php $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 {@link ArgsFormat} defines which rules the console arguments must adhere to. You can also create {@link Args} instances manually. This is especially useful in tests: php $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');
Since: 1.0
Author: Bernhard Schussek ([email protected])
Afficher le fichier Open project: webmozart/console Class Usage Examples

Méthodes publiques

Méthode Description
__construct ( ArgsFormat $format, Webmozart\Console\Api\Args\RawArgs $rawArgs = null ) Creates the console arguments.
addArguments ( array $arguments ) : static Sets the values of multiple arguments.
addOptions ( array $options ) : static Sets the values of multiple options.
getArgument ( string | integer $name ) : mixed Returns the value of an argument.
getArguments ( boolean $includeDefaults = true ) : array Returns the values of all arguments.
getCommandNames ( ) : CommandName[] Returns the command names as array.
getCommandOptions ( ) : CommandOption[] Returns the command options as array.
getFormat ( ) : ArgsFormat Returns the format of the console arguments.
getOption ( string $name ) : mixed Returns an option.
getOptions ( boolean $includeDefaults = true ) : array Returns all options.
getRawArgs ( ) : Webmozart\Console\Api\Args\RawArgs Returns the raw console arguments.
getScriptName ( ) : string | null Returns the PHP script as it was called on the console.
isArgumentDefined ( string | integer $name ) : boolean Returns whether an argument is defined in the format.
isArgumentSet ( string | integer $name ) : boolean Returns whether an argument is set.
isOptionDefined ( string $name ) : boolean Returns whether an option is defined in the format.
isOptionSet ( string $name ) : boolean Returns whether an option is set.
setArgument ( string | integer $name, mixed $value ) : static Sets the value of an argument.
setArguments ( array $arguments ) : static Sets the values of multiple arguments.
setOption ( string $name, mixed $value = true ) : static Sets an option.
setOptions ( array $options ) : static Sets the values of multiple options.

Method Details

__construct() public méthode

Creates the console arguments.
public __construct ( ArgsFormat $format, Webmozart\Console\Api\Args\RawArgs $rawArgs = null )
$format Webmozart\Console\Api\Args\Format\ArgsFormat The format that the arguments and options must adhere to.
$rawArgs Webmozart\Console\Api\Args\RawArgs The raw console arguments.

addArguments() public méthode

The existing arguments are preserved.
See also: setArgument()
public addArguments ( array $arguments ) : static
$arguments array The argument values indexed by the argument names or their 0-based positions in the argument list.
Résultat static The current instance.

addOptions() public méthode

The existing options are preserved.
See also: setOption()
public addOptions ( array $options ) : static
$options array The options indexed by their long or short names and their values.
Résultat static The current instance.

getArgument() public méthode

If the argument is not set, the default value configured in the argument format is returned.
public getArgument ( string | integer $name ) : mixed
$name string | integer The argument name or its 0-based position in the argument list.
Résultat mixed The value of the argument.

getArguments() public méthode

By default, this method also includes the default values of unset arguments. You can disable this behavior by passing false for $includeDefaults.
See also: getArgument()
public getArguments ( boolean $includeDefaults = true ) : array
$includeDefaults boolean Whether to return the default values for arguments that were not set.
Résultat array The argument values.

getCommandNames() public méthode

Returns the command names as array.
public getCommandNames ( ) : CommandName[]
Résultat Webmozart\Console\Api\Args\Format\CommandName[] The command names.

getCommandOptions() public méthode

Returns the command options as array.
public getCommandOptions ( ) : CommandOption[]
Résultat Webmozart\Console\Api\Args\Format\CommandOption[] The command options.

getFormat() public méthode

Returns the format of the console arguments.
public getFormat ( ) : ArgsFormat
Résultat Webmozart\Console\Api\Args\Format\ArgsFormat The format.

getOption() public méthode

If the option accepts a value, the value set for that option is returned. If no value was set with {@link 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.
public getOption ( string $name ) : mixed
$name string The long or short option name.
Résultat mixed The option value or `true`/`false` for options without values.

getOptions() public méthode

By default, this method also includes the default values set for options with values. You can disable this behavior by passing false for $includeDefaults.
See also: getOption()
public getOptions ( boolean $includeDefaults = true ) : array
$includeDefaults boolean Whether to return the default values for options that were not set.
Résultat array The option values and `true`/`false` for options without values.

getRawArgs() public méthode

Returns the raw console arguments.
public getRawArgs ( ) : Webmozart\Console\Api\Args\RawArgs
Résultat Webmozart\Console\Api\Args\RawArgs The raw arguments.

getScriptName() public méthode

Returns the PHP script as it was called on the console.
public getScriptName ( ) : string | null
Résultat string | null The script name or null if no script name is available.

isArgumentDefined() public méthode

Returns whether an argument is defined in the format.
public isArgumentDefined ( string | integer $name ) : boolean
$name string | integer The argument name or its 0-based position in the argument list.
Résultat boolean Returns `true` if the argument exists and `false` otherwise.

isArgumentSet() public méthode

Returns whether an argument is set.
public isArgumentSet ( string | integer $name ) : boolean
$name string | integer The argument name or its 0-based position in the argument list.
Résultat boolean Returns `true` if the argument is set and `false` otherwise.

isOptionDefined() public méthode

Returns whether an option is defined in the format.
public isOptionDefined ( string $name ) : boolean
$name string The long or short option name.
Résultat boolean Returns `true` if the option exists and `false` otherwise.

isOptionSet() public méthode

Returns whether an option is set.
public isOptionSet ( string $name ) : boolean
$name string The long or short option name.
Résultat boolean Returns `true` if the option is set and `false` otherwise.

setArgument() public méthode

The value is converted to the type defined by the argument format.
public setArgument ( string | integer $name, mixed $value ) : static
$name string | integer The argument name or its 0-based position in the argument list.
$value mixed The value of the argument.
Résultat static The current instance.

setArguments() public méthode

The existing arguments are unset.
See also: setArgument()
public setArguments ( array $arguments ) : static
$arguments array The argument values indexed by the argument names or their 0-based positions in the argument list.
Résultat static The current instance.

setOption() public méthode

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.
public setOption ( string $name, mixed $value = true ) : static
$name string The long or short option name.
$value mixed The value to set for the option.
Résultat static The current instance.

setOptions() public méthode

The existing options are unset.
See also: setOption()
public setOptions ( array $options ) : static
$options array The options indexed by their long or short names and their values.
Résultat static The current instance.