CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • Arguments
  • Command
  • CommandCollection
  • CommandFactory
  • CommandRunner
  • ConsoleErrorHandler
  • ConsoleInput
  • ConsoleInputArgument
  • ConsoleInputOption
  • ConsoleInputSubcommand
  • ConsoleIo
  • ConsoleOptionParser
  • ConsoleOutput
  • Helper
  • HelperRegistry
  • HelpFormatter
  • Shell
  • ShellDispatcher
  • TaskRegistry

Interfaces

  • CommandCollectionAwareInterface
  • CommandFactoryInterface

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 singly 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', ['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.

Namespace: Cake\Console
Location: Console/ConsoleOptionParser.php

Properties summary

  • $_args protected
    Cake\Console\ConsoleInputArgument[]
    Positional argument definitions.
  • $_command protected
    string
    Command name.
  • $_description protected
    string
    Description text - displays before options when help is generated
  • $_epilog protected
    string
    Epilog text - displays after options when help is generated
  • $_options protected
    Cake\Console\ConsoleInputOption[]
    Option definitions.
  • $_shortOptions protected
    array
    Map of short -> long options, generated when using addOption()
  • $_subcommandSort protected
    boolean
    Subcommand sorting option
  • $_subcommands protected
    Cake\Console\ConsoleInputSubcommand[]
    Subcommands for this Shell.
  • $_tokens protected
    array
    Array of args (argv).
  • $rootName protected
    string
    Root alias used in help output

Method Summary

  • __construct() public
    Construct an OptionParser so you can define its behavior
  • _nextToken() protected
    Find the next token in the argv set.
  • _optionExists() protected
    Check 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() protected
    Parse 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() public
    Add 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() public
    Add multiple subcommands at once.
  • argumentNames() public
    Get the list of argument names.
  • arguments() public
    Gets the arguments defined in the parser.
  • buildFromArray() public static
    Build a parser from an array. Uses an array like
  • command() public deprecated
    Gets or sets the command name for shell/task.
  • create() public static
    Static factory method for creating new OptionParsers so you can chain methods off of them.
  • description() public deprecated
    Get or set the description text for shell/task.
  • enableSubcommandSort() public
    Enables sorting of subcommands
  • epilog() public deprecated

    Gets or sets an epilog to the parser. The epilog is added to the end of the options and arguments listing when help is generated.

  • findClosestItem() protected

    Tries to guess the item name the user originally wanted using the some regex pattern and the levenshtein algorithm.

  • getCommand() public
    Gets the command name for shell/task.
  • getCommandError() protected

    Get the message output in the console stating that the command can not be found and tries to guess what the user wanted to say. Output a list of available subcommands as well.

  • getDescription() public
    Gets the description text for shell/task.
  • getEpilog() public
    Gets the epilog.
  • getOptionError() protected

    Get the message output in the console stating that the option can not be found and tries to guess what the user wanted to say. Output a list of available options as well.

  • getShortOptionError() protected

    Get the message output in the console stating that the short option can not be found. Output a list of available short options and what option they refer to as well.

  • help() public
    Gets formatted help for this parser object.
  • isSubcommandSortEnabled() public
    Checks whether or not sorting is enabled for subcommands.
  • merge() public
    Get or set the command name for shell/task.
  • options() public
    Get 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

  • removeOption() public
    Remove an option from the option parser.
  • removeSubcommand() public
    Remove a subcommand from the option parser.
  • setCommand() public
    Sets the command name for shell/task.
  • setDescription() public
    Sets the description text for shell/task.
  • setEpilog() public

    Sets an epilog to the parser. The epilog is added to the end of the options and arguments listing when help is generated.

  • setHelpAlias() public deprecated
    Set the alias used in the HelpFormatter
  • setRootName() public
    Set the root name used in the HelpFormatter
  • subcommands() public
    Get the array of defined subcommands
  • toArray() public
    Returns an array representation of this parser.

Method Detail

__construct() public ¶

__construct( string|null $command = null , boolean $defaultOptions = true )

Construct an OptionParser so you can define its behavior

Parameters
string|null $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() protected ¶

_nextToken( )

Find the next token in the argv set.

Returns
string
next token or ''

_optionExists() 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() 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
string[]
Args
Throws
Cake\Console\Exception\ConsoleException

_parseLongOption() 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
array
Params with $option added in.

_parseOption() 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
array
Params with $option added in.
Throws
Cake\Console\Exception\ConsoleException

_parseShortOption() 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
array
Params with $option added in.
Throws
Cake\Console\Exception\ConsoleException
When unknown short options are encountered.

addArgument() public ¶

addArgument( Cake\Console\ConsoleInputArgument|string $name , array $params = [] )

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
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.
Returns

$this

addArguments() 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

$this
See
\Cake\Console\ConsoleOptionParser::addArgument()

addOption() public ¶

addOption( Cake\Console\ConsoleInputOption|string $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.

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, 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.
Parameters
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
Returns

$this

addOptions() 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

$this
See
\Cake\Console\ConsoleOptionParser::addOption()

addSubcommand() public ¶

addSubcommand( Cake\Console\ConsoleInputSubcommand|string $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.

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
Cake\Console\ConsoleInputSubcommand|string $name
Name of the subcommand. Will also accept an instance of ConsoleInputSubcommand
array $options optional []
Array of params, see above.
Returns

$this

addSubcommands() public ¶

addSubcommands( array $commands )

Add multiple subcommands at once.

Parameters
array $commands
Array of subcommands.
Returns

$this

argumentNames() public ¶

argumentNames( )

Get the list of argument names.

Returns
string[]

arguments() public ¶

arguments( )

Gets the arguments defined in the parser.

Returns
Cake\Console\ConsoleInputArgument[]

buildFromArray() public static ¶

buildFromArray( array $spec , boolean $defaultOptions = true )

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.
     ]
];
Parameters
array $spec
The spec to build the OptionParser with.
boolean $defaultOptions optional true
Whether you want the verbose and quiet options set.
Returns
Cake\Console\ConsoleOptionParser

command() public deprecated ¶

command( string|null $text = null )

Gets or sets the command name for shell/task.

Deprecated
3.4.0 Use setCommand()/getCommand() instead.
Parameters
string|null $text optional null
The text to set, or null if you want to read
Returns
string|Cake\Console\ConsoleOptionParser
$this If reading, the value of the command. If setting $this will be returned.

create() public static ¶

create( string|null $command , boolean $defaultOptions = true )

Static factory method for creating new OptionParsers so you can chain methods off of them.

Parameters
string|null $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
Cake\Console\ConsoleOptionParser

description() public deprecated ¶

description( string|array|null $text = null )

Get or set the description text for shell/task.

Deprecated
3.4.0 Use setDescription()/getDescription() instead.
Parameters
string|array|null $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|Cake\Console\ConsoleOptionParser
$this If reading, the value of the description. If setting $this will be returned.

enableSubcommandSort() public ¶

enableSubcommandSort( boolean $value = true )

Enables sorting of subcommands

Parameters
boolean $value optional true
Whether or not to sort subcommands
Returns

$this

epilog() public deprecated ¶

epilog( string|array|null $text = null )

Gets or sets an epilog to the parser. The epilog is added to the end of the options and arguments listing when help is generated.

Deprecated
3.4.0 Use setEpilog()/getEpilog() instead.
Parameters
string|array|null $text optional null

Text when setting or null when reading. If an array the text will be imploded with "\n".

Returns
string|Cake\Console\ConsoleOptionParser
$this If reading, the value of the epilog. If setting $this will be returned.

findClosestItem() protected ¶

findClosestItem( string $needle , string[] $haystack )

Tries to guess the item name the user originally wanted using the some regex pattern and the levenshtein algorithm.

Parameters
string $needle
Unknown item (either a subcommand name or an option for instance) trying to be used.
string[] $haystack
List of items available for the type $needle belongs to.
Returns
string|null
The closest name to the item submitted by the user.

getCommand() public ¶

getCommand( )

Gets the command name for shell/task.

Returns
string
The value of the command.

getCommandError() protected ¶

getCommandError( string $command )

Get the message output in the console stating that the command can not be found and tries to guess what the user wanted to say. Output a list of available subcommands as well.

Parameters
string $command
Unknown command name trying to be dispatched.
Returns
string
The message to be displayed in the console.

getDescription() public ¶

getDescription( )

Gets the description text for shell/task.

Returns
string
The value of the description

getEpilog() public ¶

getEpilog( )

Gets the epilog.

Returns
string
The value of the epilog.

getOptionError() protected ¶

getOptionError( string $option )

Get the message output in the console stating that the option can not be found and tries to guess what the user wanted to say. Output a list of available options as well.

Parameters
string $option
Unknown option name trying to be used.
Returns
string
The message to be displayed in the console.

getShortOptionError() protected ¶

getShortOptionError( string $option )

Get the message output in the console stating that the short option can not be found. Output a list of available short options and what option they refer to as well.

Parameters
string $option
Unknown short option name trying to be used.
Returns
string
The message to be displayed in the console.

help() public ¶

help( string|null $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|null $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
string
Generated help.

isSubcommandSortEnabled() public ¶

isSubcommandSortEnabled( )

Checks whether or not sorting is enabled for subcommands.

Returns
boolean

merge() public ¶

merge( array|Cake\Console\ConsoleOptionParser $spec )

Get or set the command name for shell/task.

Parameters
array|Cake\Console\ConsoleOptionParser $spec
ConsoleOptionParser or spec to merge with.
Returns

$this

options() public ¶

options( )

Get the defined options in the parser.

Returns
Cake\Console\ConsoleInputOption[]

parse() public ¶

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

Parameters
array $argv
Array of args (argv) to parse.
Returns
array
[$params, $args]
Throws
Cake\Console\Exception\ConsoleException
When an invalid parameter is encountered.

removeOption() public ¶

removeOption( string $name )

Remove an option from the option parser.

Parameters
string $name
The option name to remove.
Returns

$this

removeSubcommand() public ¶

removeSubcommand( string $name )

Remove a subcommand from the option parser.

Parameters
string $name
The subcommand name to remove.
Returns

$this

setCommand() public ¶

setCommand( string $text )

Sets the command name for shell/task.

Parameters
string $text
The text to set.
Returns

$this

setDescription() public ¶

setDescription( string|array $text )

Sets the description text for shell/task.

Parameters
string|array $text

The text to set. If an array the text will be imploded with "\n".

Returns

$this

setEpilog() public ¶

setEpilog( string|array $text )

Sets 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

The text to set. If an array the text will be imploded with "\n".

Returns

$this

setHelpAlias() public deprecated ¶

setHelpAlias( string $alias )

Set the alias used in the HelpFormatter

Deprecated
3.5.0 Use setRootName() instead.
Parameters
string $alias
The alias

setRootName() public ¶

setRootName( string $name )

Set the root name used in the HelpFormatter

Parameters
string $name
The root command name
Returns

$this

subcommands() public ¶

subcommands( )

Get the array of defined subcommands

Returns
Cake\Console\ConsoleInputSubcommand[]

toArray() public ¶

toArray( )

Returns an array representation of this parser.

Returns
array

Properties detail

$_args ¶

protected Cake\Console\ConsoleInputArgument[]

Positional argument definitions.

See
\Cake\Console\ConsoleOptionParser::addArgument()
[]

$_command ¶

protected string

Command name.

''

$_description ¶

protected string

Description text - displays before options when help is generated

See
\Cake\Console\ConsoleOptionParser::description()

$_epilog ¶

protected string

Epilog text - displays after options when help is generated

See
\Cake\Console\ConsoleOptionParser::epilog()

$_options ¶

protected Cake\Console\ConsoleInputOption[]

Option definitions.

See
\Cake\Console\ConsoleOptionParser::addOption()
[]

$_shortOptions ¶

protected array

Map of short -> long options, generated when using addOption()

[]

$_subcommandSort ¶

protected boolean

Subcommand sorting option

true

$_subcommands ¶

protected Cake\Console\ConsoleInputSubcommand[]

Subcommands for this Shell.

See
\Cake\Console\ConsoleOptionParser::addSubcommand()
[]

$_tokens ¶

protected array

Array of args (argv).

[]

$rootName ¶

protected string

Root alias used in help output

See
\Cake\Console\HelpFormatter::setAlias()
'cake'
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs