$actions = array(
'StoreTrue' => array('Console_CommandLine_Action_StoreTrue', true),'StoreFalse'=>array('Console_CommandLine_Action_StoreFalse',true),'StoreString'=>array('Console_CommandLine_Action_StoreString',true),'StoreInt'=>array('Console_CommandLine_Action_StoreInt',true),'StoreFloat'=>array('Console_CommandLine_Action_StoreFloat',true),'StoreArray'=>array('Console_CommandLine_Action_StoreArray',true),'Callback'=>array('Console_CommandLine_Action_Callback',true),'Counter'=>array('Console_CommandLine_Action_Counter',true),'Help'=>array('Console_CommandLine_Action_Help',true),'Version'=>array('Console_CommandLine_Action_Version',true),'Password'=>array('Console_CommandLine_Action_Password',true))
[line 214]
Array of valid actions for an option, this array will also store user registered actions.
The array format is:
$add_help_option = true
[line 121]
Boolean that determine if the command line parser should add the help (-h, --help) option automatically.
$add_version_option = true
[line 133]
Boolean that determine if the command line parser should add the version (-v, --version) option automatically.
Note that the version option is also generated only if the version property is not empty, it's up to you to provide a version string of course.
$args = array()
[line 182]
An array of Console_CommandLine_Argument objects.
$commands = array()
[line 190]
An array of Console_CommandLine_Command objects (sub commands).
$description = ''
[line 101]
A description text that will be displayed in the help message.
$errors = array(
'option_bad_name' => 'option name must be a valid php variable name (got: {$name})',
'argument_bad_name' => 'argument name must be a valid php variable name (got: {$name})',
'option_long_and_short_name_missing' => 'you must provide at least an option short name or long name for option "{$name}"',
'option_bad_short_name' => 'option "{$name}" short name must be a dash followed by a letter (got: "{$short_name}")',
'option_bad_long_name' => 'option "{$name}" long name must be 2 dashes followed by a word (got: "{$long_name}")',
'option_unregistered_action' => 'unregistered action "{$action}" for option "{$name}".',
'option_bad_action' => 'invalid action for option "{$name}".',
'option_invalid_callback' => 'you must provide a valid callback for option "{$name}"',
'action_class_does_not_exists' => 'action "{$name}" class "{$class}" not found, make sure that your class is available before calling Console_CommandLine::registerAction()',
'invalid_xml_file' => 'XML definition file "{$file}" does not exists or is not readable',
'invalid_rng_file' => 'RNG file "{$file}" does not exists or is not readable'
)
[line 73]
Error messages.
$force_posix = false
[line 166]
Boolean that tells the parser to be POSIX compliant, POSIX demands the following behavior: the first non-option stops option processing.
$message_provider = false
[line 157]
The command line message provider instance.
- Var: instance of Console_CommandLine_Message
- Access: public
$name =
[line 93]
The name of the program, if not given it defaults to argv[0].
$options = array()
[line 174]
An array of Console_CommandLine_Option objects.
$outputter = false
[line 149]
The command line parser outputter instance.
- Var: implements Console_CommandLine_Outputter interface
- Access: public
$parent = false
[line 199]
Parent, only relevant in Command objects but left here for interface convenience.
$renderer = false
[line 141]
The command line parser renderer instance.
- Var: implements Console_CommandLine_Renderer interface
- Access: public
$version = ''
[line 112]
A string that represents the version of the program, if this property is not empty and property add_version_option is not set to false, the command line parser will add a --version option, that will display the property content.
__construct (Constructor) [line 258]
Console_CommandLine __construct(
[array
$params = array()])
Constructor.
Example:
'name' => 'yourprogram', // defaults to argv[0]
'description' => 'Description of your program',
'version' => '0.0.1', // your program version
'add_help_option' => true, // or false to disable --version option
'add_version_option' => true, // or false to disable --help option
'force_posix' => false // or true to force posix compliance
));
Parameters:
array
$params
—
an optional array of parameters
accept [line 307]
void accept(
mixed
$instance)
Method to allow Console_CommandLine to accept either:
- a custom renderer,
- a custom outputter,
- or a custom message provider
- Throws: Console_CommandLine_Exception if wrong argument passed
- Access: public
Parameters:
mixed
$instance
—
the custom instance
addArgument [line 425]
Add an argument with the given $name to the command line parser.
Example:
// add an array argument
$parser->addArgument ('input_files', array
('multiple'=>true
));
// add a simple argument
$result =
$parser->parse ();
// will print:
// array('file1', 'file2')
// 'file3'
// if the command line was:
// myscript.php file1 file2 file3
In a terminal, the help will be displayed like this: $ myscript.php install -h
Usage: myscript.php <input_files...> <output_file>
Parameters:
mixed
$name
—
a string containing the argument name or an instance of Console_CommandLine_Argument
array
$params
—
an array containing the argument attributes
addBuiltinOptions [line 988]
void addBuiltinOptions(
)
Add the builtin "Help" and "Version" options if needed.
addCommand [line 487]
Add a sub-command to the command line parser.
Add a command with the given $name to the parser and return the Console_CommandLine_Command instance, you can then populate the command with options, configure it, etc... like you would do for the main parser because the class Console_CommandLine_Command inherits from Console_CommandLine.
An example:
'verbose',
array(
'short_name' => '-v',
'long_name' => '--verbose',
'description' => 'be noisy when installing stuff',
'action' => 'StoreTrue'
)
);
Then in a terminal: $ myscript.php install -h
Usage: myscript.php install
[options ]
Options:
-h, --help display this help message and exit
-v, --verbose be noisy when installing stuff
$ myscript.php install --verbose
Installing whatever...
$
Parameters:
mixed
$name
—
a string containing the command name or an instance of Console_CommandLine_Command
array
$params
—
an array containing the command attributes
addOption [line 554]
Add an option to the command line parser.
Add an option with the name (variable name) $optname and set its attributes with the array $params, then return the Console_CommandLine_Option instance created. The method accepts another form: you can directly pass a Console_CommandLine_Option object as the sole argument, this allows to contruct the option separately, in order to reuse an option in different command line parsers or commands for example.
Example:
'short_name' => '-p', // a short name
'long_name' => '--path', // a long name
'description' => 'path to the dir', // a description msg
'action' => 'StoreString',
'default' => '/tmp' // a default value
));
In a terminal, the help will be displayed like this: $ myscript.php --help
Options:
-h, --help display this help message and exit
-p
, --path path to the
dir
Various methods to specify an option, these 3 commands are equivalent: $ myscript.php --path=some/path
$ myscript.php -p some/path
$ myscript.php -psome/path
Parameters:
mixed
$name
—
a string containing the option name or an instance of Console_CommandLine_Option
array
$params
—
an array containing the option attributes
displayError [line 579]
void displayError(
string
$error, [int
$exitCode = 1])
Display an error to the user and exit with $exitCode.
Parameters:
string
$error
—
the error message
int
$exitCode
—
the exit code number
displayUsage [line 596]
void displayUsage(
[int
$exitCode = 1])
Display the usage help message to the user and exit with $exitCode
Parameters:
int
$exitCode
—
the exit code number
displayVersion [line 611]
Display the program version to the user
findOption [line 629]
mixed findOption(
string
$str)
Find the option that matches the given short_name (ex: -v), long_name (ex: --verbose) or name (ex: verbose).
- Return: a Console_CommandLine_Option instance or false
- Access: public
Parameters:
string
$str
—
the option identifier
fromXmlFile [line 343]
object a fromXmlFile(
string
$file)
Return a command line parser instance built from an xml file.
Example: require_once 'Console/CommandLine.php';
$result =
$parser->parse ();
- Return: Console_CommandLine instance
- Access: public
Parameters:
string
$file
—
path to the xml file
fromXmlString [line 382]
object a fromXmlString(
string
$string)
Return a command line parser instance built from an xml string.
Example: require_once 'Console/CommandLine.php';
$xmldata = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<command>
<description>Compress files</description>
<option name="quiet">
<short_name>-q</short_name>
<long_name>--quiet</long_name>
<description>be quiet when run</description>
<action>StoreTrue/action>
</option>
<argument name="files">
<description>a list of files</description>
<multiple>true</multiple>
</argument>
</command>';
$result =
$parser->parse ();
- Return: Console_CommandLine instance
- Access: public
Parameters:
string
$string
—
the xml data
getArgcArgv [line 1027]
Try to return an array containing argc and argv, or trigger an error if it fails to get them.
- Throws: Exception
- Access: protected
parse [line 768]
Parse the command line arguments and return a Console_CommandLine_Result object.
- Throws: Exception on user errors
- Access: public
Parameters:
integer
$userArgc
—
number of arguments (optional)
array
$userArgv
—
array containing arguments (optional)
parseToken [line 849]
void parseToken(
string
$token, object
$result, array
&$args, int
$argc)
Parse the command line token and modify *by reference* the $options and $args arrays.
- Throws: Exception on user errors
- Access: protected
Parameters:
string
$token
—
the command line token to parse
object
$result
—
the Console_CommandLine_Result instance
array
&$args
—
the argv array
int
$argc
—
number of lasting args
registerAction [line 717]
void registerAction(
string
$name, string
$class)
Register a custom action for the parser, an example:
<?php
// in this example we create a "range" action:
// the user will be able to enter something like:
// $ <program> -r 1,5
// and in the result we will have:
// $result->options['range']: array(1, 5)
require_once 'Console/CommandLine.php';
require_once 'Console/CommandLine/Action.php';
{
public function execute($value=false, $params=array())
{
if
(count ($range) != 2
) {
'Option "%s" must be 2 integers separated by a comma',
$this->option->name
));
}
$this->setResult($range);
}
}
// then we can register our action
// and now our action is available !
$parser->addOption('range', array(
'short_name' => '-r',
'long_name' => '--range',
'action' => 'Range', // note our custom action
'description' => 'A range of two integers separated by a comma'
));
// etc...
?>
Parameters:
string
$name
—
the name of the custom action
string
$class
—
the class name of the custom action
triggerError [line 743]
void triggerError(
string
$msgId, int
$level, [array
$params = array()])
A wrapper for programming errors triggering.
Parameters:
string
$msgId
—
identifier of the message
int
$level
—
the php error level
array
$params
—
an array of search=>replaces entries