PHP Class Webmozart\Console\Api\Args\Format\ArgsFormat

This class is a container for {@link CommandName}, {@link CommandOption}, {@link Option} and {@link Argument} objects. The format is used to interpret a given {@link RawArgs} instance. You can pass the options and arguments to the constructor of the class: php $format = new ArgsFormat(array( new CommandName('server'), new CommandName('add'), new Argument('host', Argument::REQUIRED), new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80), )); The previous example configures a command that can be called like this: $ console server add localhost $ console server add localhost --port 8080 If the "add" command should be called via an option, change the format to: php $format = new ArgsFormat(array( new CommandName('server'), new CommandOption('add', 'a'), new Argument('host', Argument::REQUIRED), new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80), )); The command is then called like this: $ console server --add localhost $ console server --add localhost --port 8080 The format is immutable after its construction. This is necessary to maintain consistency when one format inherits from another. For example, adding a required argument to the base format of a format that already contains optional arguments is an illegal operation that cannot be prevented if the formats are mutable. If you want to create a format stepwisely, use an {@link ArgsFormatBuilder}. If multiple formats share a common set of options and arguments, extract these options and arguments into a base format and let the other formats inherit from this base format: php $baseFormat = new ArgsFormat(array( new Option('verbose', 'v'), )); $format = new ArgsFormat(array( new CommandName('server'), new CommandName('add'), new Argument('host', Argument::REQUIRED), new Option('port', 'p', Option::VALUE_OPTIONAL, null, 80), ), $baseFormat);
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 ( array | ArgsFormatBuilder $elements = [], ArgsFormat $baseFormat = null ) Creates a new format.
build ( ArgsFormat $baseFormat = null ) : ArgsFormatBuilder Returns a format builder.
getArgument ( string | integer $name, boolean $includeBase = true ) : Argument Returns an argument by its name or position.
getArguments ( boolean $includeBase = true ) : Argument[] Returns all arguments of the format.
getBaseFormat ( ) : ArgsFormat Returns the base format.
getCommandNames ( boolean $includeBase = true ) : CommandName[] Returns the command names.
getCommandOption ( string $name, boolean $includeBase = true ) : CommandOption Returns a command option by its long or short name.
getCommandOptions ( boolean $includeBase = true ) : CommandOption[] Returns all command options of the format.
getNumberOfArguments ( boolean $includeBase = true ) : integer Returns the number of arguments.
getNumberOfRequiredArguments ( boolean $includeBase = true ) : integer Returns the number of required arguments.
getOption ( string $name, boolean $includeBase = true ) : Option Returns an option by its long or short name.
getOptions ( boolean $includeBase = true ) : Option[] Returns all options of the format.
hasArgument ( string | integer $name, boolean $includeBase = true ) : boolean Returns whether the format contains a specific argument.
hasArguments ( boolean $includeBase = true ) : boolean Returns whether the format contains any argument.
hasCommandNames ( boolean $includeBase = true ) : boolean Returns whether the format contains any command names.
hasCommandOption ( string $name, boolean $includeBase = true ) : boolean Returns whether the format contains a specific command option.
hasCommandOptions ( boolean $includeBase = true ) : boolean Returns whether the format contains command options.
hasMultiValuedArgument ( boolean $includeBase = true ) : boolean Returns whether the format contains a multi-valued argument.
hasOption ( string $name, boolean $includeBase = true ) : boolean Returns whether the format contains a specific option.
hasOptionalArgument ( boolean $includeBase = true ) : boolean Returns whether the format contains an optional argument.
hasOptions ( boolean $includeBase = true ) : boolean Returns whether the format contains options.
hasRequiredArgument ( boolean $includeBase = true ) : boolean Returns whether the format contains a required argument.

Private Methods

Méthode Description
createBuilderForElements ( array $elements, ArgsFormat $baseFormat = null ) : ArgsFormatBuilder Creates a format builder for a set of arguments and options.

Method Details

__construct() public méthode

You can optionally pass a base format. The created format inherits all the arguments and options defined in the base format.
public __construct ( array | ArgsFormatBuilder $elements = [], ArgsFormat $baseFormat = null )
$elements array | ArgsFormatBuilder The arguments and options or a builder instance.
$baseFormat ArgsFormat The format.

build() public static méthode

You can optionally pass a base format. The built format inherits all the arguments and options defined in the base format.
public static build ( ArgsFormat $baseFormat = null ) : ArgsFormatBuilder
$baseFormat ArgsFormat The base format.
Résultat ArgsFormatBuilder The created builder.

getArgument() public méthode

You can either pass the name of the argument or the 0-based position of the argument.
public getArgument ( string | integer $name, boolean $includeBase = true ) : Argument
$name string | integer The argument name or its 0-based position in the argument list.
$includeBase boolean Whether to include arguments in the base format in the search.
Résultat Argument The argument.

getArguments() public méthode

Returns all arguments of the format.
public getArguments ( boolean $includeBase = true ) : Argument[]
$includeBase boolean Whether to include arguments of the base format in the result.
Résultat Argument[] The arguments.

getBaseFormat() public méthode

Returns the base format.
public getBaseFormat ( ) : ArgsFormat
Résultat ArgsFormat The base format.

getCommandNames() public méthode

Returns the command names.
public getCommandNames ( boolean $includeBase = true ) : CommandName[]
$includeBase boolean Whether to include command names in the base format in the result.
Résultat CommandName[] The command names.

getCommandOption() public méthode

Returns a command option by its long or short name.
public getCommandOption ( string $name, boolean $includeBase = true ) : CommandOption
$name string The long or short option name.
$includeBase boolean Whether to include options in the base format in the search.
Résultat CommandOption The command option.

getCommandOptions() public méthode

Returns all command options of the format.
public getCommandOptions ( boolean $includeBase = true ) : CommandOption[]
$includeBase boolean Whether to include options of the base format in the result.
Résultat CommandOption[] The command options.

getNumberOfArguments() public méthode

Returns the number of arguments.
public getNumberOfArguments ( boolean $includeBase = true ) : integer
$includeBase boolean Whether to include arguments in the base format in the result.
Résultat integer The number of arguments.

getNumberOfRequiredArguments() public méthode

Returns the number of required arguments.
public getNumberOfRequiredArguments ( boolean $includeBase = true ) : integer
$includeBase boolean Whether to include arguments in the base format in the result.
Résultat integer The number of required arguments.

getOption() public méthode

Returns an option by its long or short name.
public getOption ( string $name, boolean $includeBase = true ) : Option
$name string The long or short option name.
$includeBase boolean Whether to include options in the base format in the search.
Résultat Option The option.

getOptions() public méthode

Returns all options of the format.
public getOptions ( boolean $includeBase = true ) : Option[]
$includeBase boolean Whether to include options of the base format in the result.
Résultat Option[] The options.

hasArgument() public méthode

You can either pass the name of the argument or the 0-based position of the argument.
public hasArgument ( string | integer $name, boolean $includeBase = true ) : boolean
$name string | integer The argument name or its 0-based position in the argument list.
$includeBase boolean Whether to include arguments in the base format in the search.
Résultat boolean Returns `true` if the argument with the given name or position could be found and `false` otherwise.

hasArguments() public méthode

Returns whether the format contains any argument.
public hasArguments ( boolean $includeBase = true ) : boolean
$includeBase boolean Whether to include arguments in the base format in the search.
Résultat boolean Returns `true` if the format contains any argument and `false` otherwise.

hasCommandNames() public méthode

Returns whether the format contains any command names.
public hasCommandNames ( boolean $includeBase = true ) : boolean
$includeBase boolean Whether to consider command names in the base format.
Résultat boolean Returns `true` if the format contains command names and `false` otherwise.

hasCommandOption() public méthode

You can either pass the long or the short name of the command option.
public hasCommandOption ( string $name, boolean $includeBase = true ) : boolean
$name string The long or short option name.
$includeBase boolean Whether to include options in the base format in the search.
Résultat boolean Returns `true` if the command option with the given name could be found and `false` otherwise.

hasCommandOptions() public méthode

Returns whether the format contains command options.
public hasCommandOptions ( boolean $includeBase = true ) : boolean
$includeBase boolean Whether to include options in the base format in the search.
Résultat boolean Returns `true` if the format contains command options and `false` otherwise.

hasMultiValuedArgument() public méthode

Returns whether the format contains a multi-valued argument.
public hasMultiValuedArgument ( boolean $includeBase = true ) : boolean
$includeBase boolean Whether to include arguments in the base format in the search.
Résultat boolean Returns `true` if the format contains a multi-valued argument and `false` otherwise.

hasOption() public méthode

You can either pass the long or the short name of the option.
public hasOption ( string $name, boolean $includeBase = true ) : boolean
$name string The long or short option name.
$includeBase boolean Whether to include options in the base format in the search.
Résultat boolean Returns `true` if the option with the given name could be found and `false` otherwise.

hasOptionalArgument() public méthode

Returns whether the format contains an optional argument.
public hasOptionalArgument ( boolean $includeBase = true ) : boolean
$includeBase boolean Whether to include arguments in the base format in the search.
Résultat boolean Returns `true` if the format contains an optional argument and `false` otherwise.

hasOptions() public méthode

Returns whether the format contains options.
public hasOptions ( boolean $includeBase = true ) : boolean
$includeBase boolean Whether to include options in the base format in the search.
Résultat boolean Returns `true` if the format contains options and `false` otherwise.

hasRequiredArgument() public méthode

Returns whether the format contains a required argument.
public hasRequiredArgument ( boolean $includeBase = true ) : boolean
$includeBase boolean Whether to include arguments in the base format in the search.
Résultat boolean Returns `true` if the format contains a required argument and `false` otherwise.