Console_CommandLine
[ class tree: Console_CommandLine ] [ index: Console_CommandLine ] [ all elements ]
Packages:
Console_CommandLine


Classes:
Console_CommandLine
Console_CommandLine_Action
Console_CommandLine_Action_Callback
Console_CommandLine_Action_Counter
Console_CommandLine_Action_Help
Console_CommandLine_Action_Password
Console_CommandLine_Action_StoreArray
Console_CommandLine_Action_StoreFalse
Console_CommandLine_Action_StoreFloat
Console_CommandLine_Action_StoreInt
Console_CommandLine_Action_StoreString
Console_CommandLine_Action_StoreTrue
Console_CommandLine_Action_Version
Console_CommandLine_Argument
Console_CommandLine_Command
Console_CommandLine_Element
Console_CommandLine_Exception
Console_CommandLine_MessageProvider_Default
Console_CommandLine_Option
Console_CommandLine_Outputter_Default
Console_CommandLine_Renderer_Default
Console_CommandLine_Result
Console_CommandLine_XmlParser
Files:
Action.php
Argument.php
Callback.php
Command.php
CommandLine.php
Counter.php
Default.php
Default.php
Default.php
Element.php
ex1.php
ex2.php
Exception.php
Help.php
MessageProvider.php
Option.php
Outputter.php
Password.php
Renderer.php
Result.php
StoreArray.php
StoreFalse.php
StoreFloat.php
StoreInt.php
StoreString.php
StoreTrue.php
Version.php
XmlParser.php

Class: Console_CommandLine

Source Location: /Console_CommandLine-1.0.2/CommandLine.php


Child classes:

Console_CommandLine_Command
Class that represent a command with option and arguments.

Inherited Variables

Inherited Methods


Class Details

[line 63]
Main class for parsing command line options and arguments.

There are three ways to create parsers with this class:

  1. // direct usage
  2. $parser = new Console_CommandLine ();
  3. // with an xml definition file
  4. $parser = Console_CommandLine ::fromXmlFile ('path/to/file.xml');
  5. // with an xml definition string
  6. $validXmlString = '..your xml string...';
  7. $parser = Console_CommandLine ::fromXmlString ($validXmlString);



[ Top ]


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 214]

Array of valid actions for an option, this array will also store user registered actions.

The array format is:

  • Access: public

Type: array


[ Top ]

$add_help_option = true

[line 121]

Boolean that determine if the command line parser should add the help (-h, --help) option automatically.
  • Access: public

Type: bool


[ Top ]

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

  • Access: public

Type: bool


[ Top ]

$args = array()

[line 182]

An array of Console_CommandLine_Argument objects.
  • Access: public

Type: array


[ Top ]

$commands = array()

[line 190]

An array of Console_CommandLine_Command objects (sub commands).
  • Access: public

Type: array


[ Top ]

$description = ''

[line 101]

A description text that will be displayed in the help message.
  • Access: public

Type: string


[ Top ]

$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.
  • Access: public

Type: array


[ Top ]

$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.
  • Access: public

Type: bool


[ Top ]

$message_provider = false

[line 157]

The command line message provider instance.
  • Var: instance of Console_CommandLine_Message
  • Access: public

Type: object an


[ Top ]

$name =

[line 93]

The name of the program, if not given it defaults to argv[0].
  • Access: public

Type: string


[ Top ]

$options = array()

[line 174]

An array of Console_CommandLine_Option objects.
  • Access: public

Type: array


[ Top ]

$outputter = false

[line 149]

The command line parser outputter instance.
  • Var: implements Console_CommandLine_Outputter interface
  • Access: public

Type: object that


[ Top ]

$parent = false

[line 199]

Parent, only relevant in Command objects but left here for interface convenience.
  • Access: public



[ Top ]

$renderer = false

[line 141]

The command line parser renderer instance.
  • Var: implements Console_CommandLine_Renderer interface
  • Access: public

Type: object that


[ Top ]

$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.
  • Access: public

Type: string


[ Top ]



Method Detail

__construct (Constructor) [line 258]

Console_CommandLine __construct( [array $params = array()])

Constructor.

Example:

  1. $parser = new Console_CommandLine (array(
  2. 'name' => 'yourprogram', // defaults to argv[0]
  3. 'description' => 'Description of your program',
  4. 'version' => '0.0.1', // your program version
  5. 'add_help_option' => true, // or false to disable --version option
  6. 'add_version_option' => true, // or false to disable --help option
  7. 'force_posix' => false // or true to force posix compliance
  8. ));

  • Access: public

Parameters:

array $params — an optional array of parameters

[ Top ]

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

[ Top ]

addArgument [line 425]

object Console_CommandLine_Argument addArgument( mixed $name, [array $params = array()])

Add an argument with the given $name to the command line parser.

Example:

  1. $parser = new Console_CommandLine ();
  2. // add an array argument
  3. $parser->addArgument ('input_files', array('multiple'=>true));
  4. // add a simple argument
  5. $parser->addArgument ('output_file');
  6. $result = $parser->parse ();
  7. print_r ($result->args ['input_files']);
  8. print_r ($result->args ['output_file']);
  9. // will print:
  10. // array('file1', 'file2')
  11. // 'file3'
  12. // if the command line was:
  13. // myscript.php file1 file2 file3

In a terminal, the help will be displayed like this:

  1. $ myscript.php install -h
  2. 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

[ Top ]

addBuiltinOptions [line 988]

void addBuiltinOptions( )

Add the builtin "Help" and "Version" options if needed.
  • Access: public

[ Top ]

addCommand [line 487]

object Console_CommandLine_Command addCommand( mixed $name, [array $params = array()])

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:

  1. $parser = new Console_CommandLine ();
  2. $install_cmd = $parser->addCommand ('install');
  3. $install_cmd->addOption (
  4. 'verbose',
  5. array(
  6. 'short_name' => '-v',
  7. 'long_name' => '--verbose',
  8. 'description' => 'be noisy when installing stuff',
  9. 'action' => 'StoreTrue'
  10. )
  11. );
  12. $parser->parse ();
Then in a terminal:
  1. $ myscript.php install -h
  2. Usage: myscript.php install [options ]
  3. Options:
  4. -h, --help display this help message and exit
  5. -v, --verbose be noisy when installing stuff
  6. $ myscript.php install --verbose
  7. Installing whatever...
  8. $


Parameters:

mixed $name — a string containing the command name or an instance of Console_CommandLine_Command
array $params — an array containing the command attributes

[ Top ]

addOption [line 554]

object Console_CommandLine_Option addOption( mixed $name, [array $params = array()])

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:

  1. $parser = new Console_CommandLine ();
  2. $parser->addOption ('path', array(
  3. 'short_name' => '-p', // a short name
  4. 'long_name' => '--path', // a long name
  5. 'description' => 'path to the dir', // a description msg
  6. 'action' => 'StoreString',
  7. 'default' => '/tmp' // a default value
  8. ));
  9. $parser->parse ();

In a terminal, the help will be displayed like this:

  1. $ myscript.php --help
  2. Usage: myscript.php [options ]
  3. Options:
  4. -h, --help display this help message and exit
  5. -p, --path path to the dir

Various methods to specify an option, these 3 commands are equivalent:

  1. $ myscript.php --path=some/path
  2. $ myscript.php -p some/path
  3. $ 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

[ Top ]

displayError [line 579]

void displayError( string $error, [int $exitCode = 1])

Display an error to the user and exit with $exitCode.
  • Access: public

Parameters:

string $error — the error message
int $exitCode — the exit code number

[ Top ]

displayUsage [line 596]

void displayUsage( [int $exitCode = 1])

Display the usage help message to the user and exit with $exitCode
  • Access: public

Parameters:

int $exitCode — the exit code number

[ Top ]

displayVersion [line 611]

void displayVersion( )

Display the program version to the user
  • Access: public

[ Top ]

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

[ Top ]

fromXmlFile [line 343]

object a fromXmlFile( string $file)

Return a command line parser instance built from an xml file.

Example:

  1. require_once 'Console/CommandLine.php';
  2. $parser = Console_CommandLine ::fromXmlFile ('path/to/file.xml');
  3. $result = $parser->parse ();

  • Return: Console_CommandLine instance
  • Access: public

Parameters:

string $file — path to the xml file

[ Top ]

fromXmlString [line 382]

object a fromXmlString( string $string)

Return a command line parser instance built from an xml string.

Example:

  1. require_once 'Console/CommandLine.php';
  2. $xmldata = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
  3. <command>
  4. <description>Compress files</description>
  5. <option name="quiet">
  6. <short_name>-q</short_name>
  7. <long_name>--quiet</long_name>
  8. <description>be quiet when run</description>
  9. <action>StoreTrue/action>
  10. </option>
  11. <argument name="files">
  12. <description>a list of files</description>
  13. <multiple>true</multiple>
  14. </argument>
  15. </command>';
  16. $parser = Console_CommandLine ::fromXmlString ($xmldata);
  17. $result = $parser->parse ();

  • Return: Console_CommandLine instance
  • Access: public

Parameters:

string $string — the xml data

[ Top ]

getArgcArgv [line 1027]

array getArgcArgv( )

Try to return an array containing argc and argv, or trigger an error if it fails to get them.
  • Throws: Exception
  • Access: protected

[ Top ]

parse [line 768]

object Console_CommandLine_Result parse( [integer $userArgc = null], [array $userArgv = null])

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)

[ Top ]

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

[ Top ]

registerAction [line 717]

void registerAction( string $name, string $class)

Register a custom action for the parser, an example:

  1. <?php
  2. // in this example we create a "range" action:
  3. // the user will be able to enter something like:
  4. // $ <program> -r 1,5
  5. // and in the result we will have:
  6. // $result->options['range']: array(1, 5)
  7. require_once 'Console/CommandLine.php';
  8. require_once 'Console/CommandLine/Action.php';
  9. class ActionRange extends Console_CommandLine_Action
  10. {
  11. public function execute($value=false, $params=array())
  12. {
  13. $range = explode (',', str_replace (' ', '', $value));
  14. if (count ($range) != 2) {
  15. throw new Exception(sprintf (
  16. 'Option "%s" must be 2 integers separated by a comma',
  17. $this->option->name
  18. ));
  19. }
  20. $this->setResult($range);
  21. }
  22. }
  23. // then we can register our action
  24. Console_CommandLine ::registerAction ('Range', 'ActionRange');
  25. // and now our action is available !
  26. $parser = new Console_CommandLine ();
  27. $parser->addOption('range', array(
  28. 'short_name' => '-r',
  29. 'long_name' => '--range',
  30. 'action' => 'Range', // note our custom action
  31. 'description' => 'A range of two integers separated by a comma'
  32. ));
  33. // etc...
  34. ?>

  • Access: public

Parameters:

string $name — the name of the custom action
string $class — the class name of the custom action

[ Top ]

triggerError [line 743]

void triggerError( string $msgId, int $level, [array $params = array()])

A wrapper for programming errors triggering.
  • Access: public

Parameters:

string $msgId — identifier of the message
int $level — the php error level
array $params — an array of search=>replaces entries

[ Top ]


Documentation generated on 2019年3月11日 15:25:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.

AltStyle によって変換されたページ (->オリジナル) /