Class: Console_CommandLine
Source Location: /Console_CommandLine-1.0.0RC3/CommandLine.php
Main class for parsing command line options and arguments.
Author(s):
Version:
Copyright:
|
|
|
Child classes:
|
Inherited Variables
|
Inherited Methods
|
Class Details
Class Variables
$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 211]
Array of valid actions for an option, this array will also store user registered actions. The array format is:
$add_help_option = true
[line 118]
Boolean that determine if the command line parser should add the help (-h, --help) option automatically.
$add_version_option = true
[line 130]
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 179]
An array of Console_CommandLine_Argument objects.
$commands = array()
[line 187]
An array of Console_CommandLine_Command objects (sub commands).
$description = ''
[line 98]
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 70]
Error messages.
$force_posix = false
[line 163]
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 154]
The command line message provider instance.
$name =
[line 90]
The name of the program, if not given it defaults to argv[0].
$options = array()
[line 171]
An array of Console_CommandLine_Option objects.
$outputter = false
[line 146]
The command line parser outputter instance.
$parent = false
[line 196]
Parent, only relevant in Command objects but left here for interface convenience.
$renderer = false
[line 138]
The command line parser renderer instance.
$version = ''
[line 109]
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.
Method Detail
__construct (Constructor) [line 262]
Console_CommandLine __construct(
[array
$params = array()])
|
|
Constructor. Example:
'name' => 'yourprogram', // if not given it defaults to argv[0]
'description' => 'Some meaningful 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
'renderer' => $rdr, // an instance that implements the
// Console_CommandLine_Renderer interface
'outputter' => $out, // an instance that implements the
// Console_CommandLine_Outputter interface
'message_provider' => $mp, // an instance that implements the
// Console_CommandLine_MessageProvider
// interface
'force_posix' => false // or true to force posix compliance
));
Parameters:
addArgument [line 411]
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:
addCommand [line 473]
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:
addOption [line 540]
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:
displayError [line 565]
void displayError(
string
$error, [int
$exitCode = 1])
|
|
Display an error to the user and exit with $exitCode.
Parameters:
displayUsage [line 582]
void displayUsage(
[int
$exitCode = 1])
|
|
Display the usage help message to the user and exit with $exitCode
Parameters:
displayVersion [line 597]
Display the program version to the user
findOption [line 615]
mixed findOption(
string
$str)
|
|
Find the option that matches the given short_name (ex: -v), long_name (ex: --verbose) or name (ex: verbose).
Parameters:
fromXmlFile [line 329]
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();
Parameters:
fromXmlString [line 368]
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();
Parameters:
parse [line 755]
Parse the command line arguments and return a Console_CommandLine_Result object.
Parameters:
parseToken [line 850]
void parseToken(
string
$token, object
$result, array
&$args, int
$argc)
|
|
Parse the command line token and modify *by reference* the $options and $args arrays.
Parameters:
registerAction [line 703]
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:
triggerError [line 729]
void triggerError(
string
$msgId, int
$level, [array
$params = array()])
|
|
A wrapper for programming errors triggering.
Parameters:
Documentation generated on Mon, 11 Mar 2019 15:17:13 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|
|