Handles parsing the ARGV in the command line and provides support for GetOpt compatible option definition. Provides a builder pattern implementation for creating shell option parsers.
Named arguments come in two forms, long and short. Long arguments are preceded by two - and give a more verbose option name. i.e. --version. Short arguments are preceded by one - and are only one character long. They usually match with a long option, and provide a more terse alternative.
Options can be defined with both long and short forms. By using $parser->addOption() you can define new options. The name of the option is used as its long form, and you can supply an additional short form, with the short option. Short options should only be one letter long. Using more than one letter for a short option will raise an exception.
Calling options can be done using syntax similar to most *nix command line tools. Long options cane either include an = or leave it out.
cake my_command --connection default --name=something
Short options can be defined singly or in groups.
cake my_command -cn
Short options can be combined into groups as seen above. Each letter in a group will be treated as a separate option. The previous example is equivalent to:
cake my_command -c -n
Short options can also accept values:
cake my_command -c default
If no positional arguments are defined, all of them will be parsed. If you define positional arguments any arguments greater than those defined will cause exceptions. Additionally you can declare arguments as optional, by setting the required param to false.
$parser->addArgument('model', ['required' => false]); By providing help text for your positional arguments and named arguments, the ConsoleOptionParser can generate a help display for you. You can view the help for shells by using the --help or -h switch.
\Cake\Console\ConsoleInputArgument[]Positional argument definitions.
stringCommand name.
stringDescription text - displays before options when help is generated
stringEpilog text - displays after options when help is generated
\Cake\Console\ConsoleInputOption[]Option definitions.
arrayMap of short -> long options, generated when using addOption()
boolSubcommand sorting option
\Cake\Console\ConsoleInputSubcommand[]Subcommands for this Shell.
arrayArray of args (argv).
stringRoot alias used in help output
Parse an argument, and ensure that the argument doesn't exceed the number of arguments and that the argument is a valid choice.
Parse the value for a long option out of $this->_tokens. Will handle options with an = in them.
Parse the value for a short option out of $this->_tokens If the $option is a combination of multiple shortcuts like -otf they will be shifted onto the token stack and parsed individually.
Add an option to the option parser. Options allow you to define optional or required parameters for your console application. Options are defined by the parameters they use.
Static factory method for creating new OptionParsers so you can chain methods off of them.
Parse the argv array into a set of params and args. If $command is not null and $command is equal to a subcommand that has a parser, that parser will be used to parse the $argv
Sets an epilog to the parser. The epilog is added to the end of the options and arguments listing when help is generated.
__construct(string $command, bool $defaultOptions)
Construct an OptionParser so you can define its behavior
string $command optional The command name this parser is for. The command name is used for generating help.
bool $defaultOptions optional Whether you want the verbose and quiet options set. Setting this to false will prevent the addition of --verbose & --quiet options.
_nextToken()
Find the next token in the argv set.
stringnext token or ''
_optionExists(string $name)
Check to see if $name has an option (short/long) defined for it.
string $name The name of the option.
bool_parseArg(string $argument, array $args)
Parse an argument, and ensure that the argument doesn't exceed the number of arguments and that the argument is a valid choice.
string $argument The argument to append
array $args The array of parsed args to append to.
string[]Args
Cake\Console\Exception\ConsoleException_parseLongOption(string $option, array $params)
Parse the value for a long option out of $this->_tokens. Will handle options with an = in them.
string $option The option to parse.
array $params The params to append the parsed value into
arrayParams with $option added in.
_parseOption(string $name, array $params)
Parse an option by its name index.
string $name The name to parse.
array $params The params to append the parsed value into
arrayParams with $option added in.
Cake\Console\Exception\ConsoleException_parseShortOption(string $option, array $params)
Parse the value for a short option out of $this->_tokens If the $option is a combination of multiple shortcuts like -otf they will be shifted onto the token stack and parsed individually.
string $option The option to parse.
array $params The params to append the parsed value into
arrayParams with $option added in.
Cake\Console\Exception\ConsoleExceptionaddArgument(mixed $name, array $params)
Add a positional argument to the option parser.
help The help text to display for this argument.required Whether this parameter is required.index The index for the arg, if left undefined the argument will be put onto the end of the arguments. If you define the same index twice the first option will be overwritten.choices A list of valid choices for this argument. If left empty all values are valid.. An exception will be raised when parse() encounters an invalid value.\Cake\Console\ConsoleInputArgument|string $name The name of the argument. Will also accept an instance of ConsoleInputArgument.
array $params optional Parameters for the argument, see above.
$thisaddArguments(array $args)
Add multiple arguments at once. Take an array of argument definitions.
The keys are used as the argument names, and the values as params for the argument.
array $args Array of arguments to add.
$thisaddOption(mixed $name, array $options)
Add an option to the option parser. Options allow you to define optional or required parameters for your console application. Options are defined by the parameters they use.
short - The single letter variant for this option, leave undefined for none.help - Help text for this option. Used when generating help for the option.default - The default value for this option. Defaults are added into the parsed params when the attached option is not provided or has no value. Using default and boolean together will not work. are added into the parsed parameters when the option is undefined. Defaults to null.boolean - The option uses no value, it's just a boolean switch. Defaults to false. If an option is defined as boolean, it will always be added to the parsed params. If no present it will be false, if present it will be true.multiple - The option can be provided multiple times. The parsed option will be an array of values when this option is enabled.choices A list of valid choices for this option. If left empty all values are valid.. An exception will be raised when parse() encounters an invalid value.\Cake\Console\ConsoleInputOption|string $name The long name you want to the value to be parsed out as when options are parsed. Will also accept an instance of ConsoleInputOption.
array $options optional An array of parameters that define the behavior of the option
$thisaddOptions(array $options)
Add multiple options at once. Takes an array of option definitions.
The keys are used as option names, and the values as params for the option.
array $options Array of options to add.
$thisaddSubcommand(mixed $name, array $options)
Append a subcommand to the subcommand list.
Subcommands are usually methods on your Shell, but can also be used to document Tasks.
help - Help text for the subcommand.parser - A ConsoleOptionParser for the subcommand. This allows you to create method specific option parsers. When help is generated for a subcommand, if a parser is present it will be used.\Cake\Console\ConsoleInputSubcommand|string $name Name of the subcommand. Will also accept an instance of ConsoleInputSubcommand.
array $options optional Array of params, see above.
$thisaddSubcommands(array $commands)
Add multiple subcommands at once.
array $commands Array of subcommands.
$thisargumentNames()
Get the list of argument names.
string[]arguments()
Gets the arguments defined in the parser.
\Cake\Console\ConsoleInputArgument[]buildFromArray(array $spec, bool $defaultOptions)
Build a parser from an array. Uses an array like
$spec = [
'description' => 'text',
'epilog' => 'text',
'arguments' => [
// list of arguments compatible with addArguments.
],
'options' => [
// list of options compatible with addOptions
],
'subcommands' => [
// list of subcommands to add.
]
]; array $spec The spec to build the OptionParser with.
bool $defaultOptions optional Whether you want the verbose and quiet options set.
staticcreate(string $command, bool $defaultOptions)
Static factory method for creating new OptionParsers so you can chain methods off of them.
string $command The command name this parser is for. The command name is used for generating help.
bool $defaultOptions optional Whether you want the verbose and quiet options set.
staticenableSubcommandSort(bool $value)
Enables sorting of subcommands
bool $value optional Whether or not to sort subcommands
$thisgetCommand()
Gets the command name for shell/task.
stringThe value of the command.
getDescription()
Gets the description text for shell/task.
stringThe value of the description
getEpilog()
Gets the epilog.
stringThe value of the epilog.
help(?string $subcommand, string $format, int $width)
Gets formatted help for this parser object.
Generates help text based on the description, options, arguments, subcommands and epilog in the parser.
string|null $subcommand optional If present and a valid subcommand that has a linked parser. That subcommands help will be shown instead.
string $format optional Define the output format, can be text or XML
int $width optional The width to format user content to. Defaults to 72
stringGenerated help.
isSubcommandSortEnabled()
Checks whether or not sorting is enabled for subcommands.
boolmerge(mixed $spec)
Get or set the command name for shell/task.
array|\Cake\Console\ConsoleOptionParser $spec ConsoleOptionParser or spec to merge with.
$thisoptions()
Get the defined options in the parser.
\Cake\Console\ConsoleInputOption[]parse(array $argv)
Parse the argv array into a set of params and args. If $command is not null and $command is equal to a subcommand that has a parser, that parser will be used to parse the $argv
array $argv Array of args (argv) to parse.
array[$params, $args]
Cake\Console\Exception\ConsoleExceptionremoveOption(string $name)
Remove an option from the option parser.
string $name The option name to remove.
$thisremoveSubcommand(string $name)
Remove a subcommand from the option parser.
string $name The subcommand name to remove.
$thissetCommand(string $text)
Sets the command name for shell/task.
string $text The text to set.
$thissetDescription(mixed $text)
Sets the description text for shell/task.
string|array $text The text to set. If an array the text will be imploded with "\n".
$thissetEpilog(mixed $text)
Sets an epilog to the parser. The epilog is added to the end of the options and arguments listing when help is generated.
string|array $text The text to set. If an array the text will be imploded with "\n".
$thissetRootName(string $name)
Set the root name used in the HelpFormatter
string $name The root command name
$thissubcommands()
Get the array of defined subcommands
\Cake\Console\ConsoleInputSubcommand[]toArray()
Returns an array representation of this parser.
arrayPositional argument definitions.
\Cake\Console\ConsoleInputArgument[]Command name.
stringDescription text - displays before options when help is generated
stringEpilog text - displays after options when help is generated
stringOption definitions.
\Cake\Console\ConsoleInputOption[]Map of short -> long options, generated when using addOption()
arraySubcommand sorting option
boolSubcommands for this Shell.
\Cake\Console\ConsoleInputSubcommand[]Array of args (argv).
arrayRoot alias used in help output
string
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.Console.ConsoleOptionParser.html