Class ConsoleOptionParser
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.
Options
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.
Using Options
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 myshell command --connection default --name=something
Short options can be defined signally or in groups.
cake myshell 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 myshell command -c -n
Short options can also accept values:
cake myshell command -c default
Positional arguments
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', array('required' => false));
Providing Help text
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.
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Console/ConsoleOptionParser.php
Properties summary
-
$_args
protectedPositional argument definitions.array
-
$_command
protectedCommand name.string
-
$_description
protectedDescription text - displays before options when help is generatedstring
-
$_epilog
protectedEpilog text - displays after options when help is generatedstring
-
$_options
protectedOption definitions.array
-
$_shortOptions
protectedMap of short -> long options, generated when using addOption()string
-
$_subcommands
protectedSubcommands for this Shell.array
Method Summary
- __construct() publicConstruct an OptionParser so you can define its behavior
- _nextToken() protectedFind the next token in the argv set.
- _optionExists() protectedCheck to see if $name has an option (short/long) defined for it.
- _parseArg() protected
Parse an argument, and ensure that the argument doesn't exceed the number of arguments and that the argument is a valid choice.
- _parseLongOption() protected
Parse the value for a long option out of $this->_tokens. Will handle options with an
=
in them. - _parseOption() protectedParse an option by its name index.
- _parseShortOption() protected
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.
- addArgument() publicAdd a positional argument to the option parser.
- addArguments() public
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.
- addOption() public
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.
- addOptions() public
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.
- addSubcommand() public
Append a subcommand to the subcommand list. Subcommands are usually methods on your Shell, but can also be used to document Tasks.
- addSubcommands() publicAdd multiple subcommands at once.
- arguments() publicGets the arguments defined in the parser.
- Build a parser from an array. Uses an array like
- command() publicGet or set the command name for shell/task.
- Static factory method for creating new OptionParsers so you can chain methods off of them.
- description() publicGet or set the description text for shell/task.
- epilog() public
Get or set an epilog to the parser. The epilog is added to the end of the options and arguments listing when help is generated.
- help() public
Gets formatted help for this parser object. Generates help text based on the description, options, arguments, subcommands and epilog in the parser.
- options() publicGet the defined options in the parser.
- parse() public
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
- removeSubcommand() publicRemove a subcommand from the option parser.
- subcommands() publicGet the array of defined subcommands
Method Detail
__construct()source public
__construct( string $command null , boolean $defaultOptions true )
Construct an OptionParser so you can define its behavior
Parameters
- string
$command
optional null - The command name this parser is for. The command name is used for generating help.
- boolean
$defaultOptions
optional true Whether you want the verbose and quiet options set. Setting this to false will prevent the addition of
--verbose
&--quiet
options.
_nextToken()source protected
_nextToken( )
Find the next token in the argv set.
Returns
stringnext token or ''
_optionExists()source protected
_optionExists( string $name )
Check to see if $name has an option (short/long) defined for it.
Parameters
- string
$name
- The name of the option.
Returns
boolean_parseArg()source protected
_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.
Parameters
- string
$argument
- The argument to append
- array
$args
- The array of parsed args to append to.
Returns
arrayArgs
Throws
ConsoleException
_parseLongOption()source protected
_parseLongOption( string $option , array $params )
Parse the value for a long option out of $this->_tokens. Will handle options with an =
in them.
Parameters
- string
$option
- The option to parse.
- array
$params
- The params to append the parsed value into
Returns
arrayParams with $option added in.
_parseOption()source protected
_parseOption( string $name , array $params )
Parse an option by its name index.
Parameters
- string
$name
- The name to parse.
- array
$params
- The params to append the parsed value into
Returns
arrayParams with $option added in.
Throws
ConsoleException
_parseShortOption()source protected
_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.
Parameters
- string
$option
- The option to parse.
- array
$params
- The params to append the parsed value into
Returns
arrayParams with $option added in.
Throws
ConsoleException
When unknown short options are encountered.
addArgument()source public
addArgument( ConsoleInputArgument|string $name , array $params array() )
Add a positional argument to the option parser.
Params
-
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.
Parameters
-
ConsoleInputArgument
|string$name
- The name of the argument. Will also accept an instance of ConsoleInputArgument
- array
$params
optional array() - Parameters for the argument, see above.
Returns
ConsoleOptionParser
addArguments()source public
addArguments( 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.
Parameters
- array
$args
- Array of arguments to add.
Returns
ConsoleOptionParser
See
ConsoleOptionParser::addArgument()addOption()source public
addOption( ConsoleInputOption|string $name , array $options array() )
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.
Options
-
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, its 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. -
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.
Parameters
-
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 array() - An array of parameters that define the behavior of the option
Returns
ConsoleOptionParser
addOptions()source public
addOptions( 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.
Parameters
- array
$options
- Array of options to add.
Returns
ConsoleOptionParser
See
ConsoleOptionParser::addOption()addSubcommand()source public
addSubcommand( ConsoleInputSubcommand|string $name , array $options array() )
Append a subcommand to the subcommand list. Subcommands are usually methods on your Shell, but can also be used to document Tasks.
Options
-
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.
Parameters
-
ConsoleInputSubcommand
|string$name
- Name of the subcommand. Will also accept an instance of ConsoleInputSubcommand
- array
$options
optional array() - Array of params, see above.
Returns
ConsoleOptionParser
addSubcommands()source public
addSubcommands( array $commands )
Add multiple subcommands at once.
Parameters
- array
$commands
- Array of subcommands.
Returns
ConsoleOptionParser
arguments()source public
arguments( )
Gets the arguments defined in the parser.
Returns
arrayArray of argument descriptions
buildFromArray()source public static
buildFromArray( array $spec )
Build a parser from an array. Uses an array like
$spec = array( 'description' => 'text', 'epilog' => 'text', 'arguments' => array( // list of arguments compatible with addArguments. ), 'options' => array( // list of options compatible with addOptions ), 'subcommands' => array( // list of subcommands to add. ) );
Parameters
- array
$spec
- The spec to build the OptionParser with.
Returns
ConsoleOptionParser
command()source public
command( string $text null )
Get or set the command name for shell/task.
Parameters
- string
$text
optional null - The text to set, or null if you want to read
Returns
string|ConsoleOptionParser
If reading, the value of the command. If setting $this will be returned.
create()source public static
create( string $command , boolean $defaultOptions true )
Static factory method for creating new OptionParsers so you can chain methods off of them.
Parameters
- string
$command
- The command name this parser is for. The command name is used for generating help.
- boolean
$defaultOptions
optional true - Whether you want the verbose and quiet options set.
Returns
ConsoleOptionParser
description()source public
description( string|array $text null )
Get or set the description text for shell/task.
Parameters
- string|array
$text
optional null The text to set, or null if you want to read. If an array the text will be imploded with "\n"
Returns
string|ConsoleOptionParser
If reading, the value of the description. If setting $this will be returned.
epilog()source public
epilog( string|array $text null )
Get or set an epilog to the parser. The epilog is added to the end of the options and arguments listing when help is generated.
Parameters
- string|array
$text
optional null - Text when setting or null when reading. If an array the text will be imploded with "\n"
Returns
string|ConsoleOptionParser
If reading, the value of the epilog. If setting $this will be returned.
help()source public
help( string $subcommand null , string $format 'text' , integer $width 72 )
Gets formatted help for this parser object. Generates help text based on the description, options, arguments, subcommands and epilog in the parser.
Parameters
- string
$subcommand
optional null If present and a valid subcommand that has a linked parser. That subcommands help will be shown instead.
- string
$format
optional 'text' - Define the output format, can be text or xml
- integer
$width
optional 72 - The width to format user content to. Defaults to 72
Returns
stringGenerated help.
parse()source public
parse( array $argv , string $command null )
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
Parameters
- array
$argv
- Array of args (argv) to parse.
- string
$command
optional null The subcommand to use. If this parameter is a subcommand, that has a parser, That parser will be used to parse $argv instead.
Returns
arrayarray($params, $args)
Throws
ConsoleException
When an invalid parameter is encountered.
removeSubcommand()source public
removeSubcommand( string $name )
Remove a subcommand from the option parser.
Parameters
- string
$name
- The subcommand name to remove.
Returns
ConsoleOptionParser
Properties detail
$_argssource
protected array
Positional argument definitions.
See
ConsoleOptionParser::addArgument()array()
$_descriptionsource
protected string
Description text - displays before options when help is generated
See
ConsoleOptionParser::description()null
$_epilogsource
protected string
Epilog text - displays after options when help is generated
See
ConsoleOptionParser::epilog()null
$_shortOptionssource
protected string
Map of short -> long options, generated when using addOption()
array()
$_subcommandssource
protected array
Subcommands for this Shell.
See
ConsoleOptionParser::addSubcommand()array()
© 2005–2017 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/2.10/class-ConsoleOptionParser.html