Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Implemented feature request #18583 (default values for arguments),

* Fixed bug #18584 (optional arguments not shown as optional), also removed the enclosing "< >" on argument names (it looks better IMO).

# sync from a commit izi did for the PEAR1 version
  • Loading branch information...
commit 19ab337ab77bd6bbb3de6875ae29ecfac96cff0e 1 parent 844bc79
@helgi helgi authored
View
78 src/Console/CommandLine.php
@@ -5,8 +5,8 @@
/**
* This file is part of the PEAR2\Console\CommandLine package.
*
- * A full featured package for managing command-line options and arguments
- * hightly inspired from python optparse module, it allows the developper to
+ * A full featured package for managing command-line options and arguments
+ * hightly inspired from python optparse module, it allows the developper to
* easily build complex command line interfaces.
*
* PHP version 5
@@ -15,11 +15,11 @@
* through the world-wide-web at the following URI:
* http://opensource.org/licenses/mit-license.php
*
- * @category Console
+ * @category Console
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
@@ -27,7 +27,7 @@
/**
* Main class for parsing command line options and arguments.
- *
+ *
* There are three ways to create parsers with this class:
* <code>
* // direct usage
@@ -45,7 +45,7 @@
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Console_CommandLine
* @since File available since release 0.1.0
@@ -66,6 +66,7 @@ class CommandLine
public static $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})',
+ 'argument_no_default' => 'only optional arguments can have a default value',
'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}")',
@@ -92,7 +93,7 @@ class CommandLine
public $description = '';
/**
- * A string that represents the version of the program, if this property is
+ * 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.
@@ -113,8 +114,8 @@ class CommandLine
/**
* 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
+ * 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.
*
* @var bool $add_version_option Whether to add a version option or not
@@ -143,7 +144,7 @@ class CommandLine
public $message_provider = false;
/**
- * Boolean that tells the parser to be POSIX compliant, POSIX demands the
+ * Boolean that tells the parser to be POSIX compliant, POSIX demands the
* following behavior: the first non-option stops option processing.
*
* @var bool $force_posix Whether to force posix compliance or not
@@ -151,7 +152,7 @@ class CommandLine
public $force_posix = false;
/**
- * Boolean that tells the parser to set relevant options default values,
+ * Boolean that tells the parser to set relevant options default values,
* according to the option action.
*
* @see PEAR2\Console\CommandLine\Option::setDefaults()
@@ -181,7 +182,7 @@ class CommandLine
public $commands = array();
/**
- * Parent, only relevant in Command objects but left here for interface
+ * Parent, only relevant in Command objects but left here for interface
* convenience.
*
* @var PEAR2\Console\CommandLine The parent instance
@@ -190,7 +191,7 @@ class CommandLine
public $parent = false;
/**
- * Array of valid actions for an option, this array will also store user
+ * Array of valid actions for an option, this array will also store user
* registered actions.
*
* The array format is:
@@ -278,7 +279,7 @@ class CommandLine
*
* @return void
*/
- public function __construct(array $params = array())
+ public function __construct(array $params = array())
{
if (isset($params['name'])) {
$this->name = $params['name'];
@@ -320,7 +321,7 @@ public function __construct(array $params = array())
/**
* Method to allow PEAR2\Console\CommandLine to accept either:
- * + a custom renderer,
+ * + a custom renderer,
* + a custom outputter,
* + or a custom message provider
*
@@ -329,7 +330,7 @@ public function __construct(array $params = array())
* @return void
* @throws PEAR2\Console\CommandLine\Exception if wrong argument passed
*/
- public function accept($instance)
+ public function accept($instance)
{
if ($instance instanceof CommandLine\Renderer) {
if (property_exists($instance, 'parser') && !$instance->parser) {
@@ -366,7 +367,7 @@ public function accept($instance)
*
* @return PEAR2\Console\CommandLine The parser instance
*/
- public static function fromXmlFile($file)
+ public static function fromXmlFile($file)
{
return CommandLine\XmlParser::parse($file);
}
@@ -401,7 +402,7 @@ public static function fromXmlFile($file)
*
* @return PEAR2\Console\CommandLine The parser instance
*/
- public static function fromXmlString($string)
+ public static function fromXmlString($string)
{
return CommandLine\XmlParser::parseString($string);
}
@@ -415,7 +416,7 @@ public static function fromXmlString($string)
* Adds an argument with the name $name and set its attributes with the
* array $params, then return the PEAR2\Console\CommandLine\Argument instance
* created.
- * The method accepts another form: you can directly pass a
+ * The method accepts another form: you can directly pass a
* PEAR2\Console\CommandLine\Argument object as the sole argument, this allows
* you to contruct the argument separately, in order to reuse it in
* different command line parsers or commands for example.
@@ -468,7 +469,7 @@ public function addArgument($name, $params = array())
/**
* Adds a sub-command to the command line parser.
*
- * Adds a command with the given $name to the parser and returns the
+ * Adds a command with the given $name to the parser and returns the
* PEAR2\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 PEAR2\Console\CommandLine\Command inherits from
@@ -517,9 +518,9 @@ public function addCommand($name, $params = array())
} else {
$params['name'] = $name;
$command = new CommandLine\Command($params);
- // some properties must cascade to the child command if not
- // passed explicitely. This is done only in this case, because if
- // we have a Command object we have no way to determine if theses
+ // some properties must cascade to the child command if not
+ // passed explicitely. This is done only in this case, because if
+ // we have a Command object we have no way to determine if theses
// properties have already been set
$cascade = array(
'add_help_option',
@@ -554,7 +555,7 @@ public function addCommand($name, $params = array())
* Adds an option with the name $name and set its attributes with the
* array $params, then return the PEAR2\Console\CommandLine\Option instance
* created.
- * The method accepts another form: you can directly pass a
+ * The method accepts another form: you can directly pass a
* PEAR2\Console\CommandLine\Option object as the sole argument, this allows
* you to contruct the option separately, in order to reuse it in different
* command line parsers or commands for example.
@@ -780,7 +781,7 @@ public function findOption($str)
*
* @return void
*/
- public static function registerAction($name, $class)
+ public static function registerAction($name, $class)
{
if (!isset(self::$actions[$name])) {
if (!class_exists($class)) {
@@ -805,11 +806,11 @@ public static function registerAction($name, $class)
* @return void
* @todo remove Console::triggerError() and use exceptions only
*/
- public static function triggerError($msgId, $level, $params=array())
+ public static function triggerError($msgId, $level, $params=array())
{
if (isset(self::$errors[$msgId])) {
$msg = str_replace(array_keys($params),
- array_values($params), self::$errors[$msgId]);
+ array_values($params), self::$errors[$msgId]);
trigger_error($msg, $level);
} else {
trigger_error('unknown error', $level);
@@ -905,6 +906,9 @@ public function parse($userArgc=null, $userArgv=null)
} else {
$result->args[$name] = array_shift($args);
}
+ if (!$result->args[$name] && $arg->optional && $arg->default) {
+ $result->args[$name] = $arg->default;
+ }
}
// dispatch deferred options
foreach ($this->_dispatchLater as $optArray) {
@@ -957,7 +961,7 @@ protected function parseToken($token, $result, &$args, $argc)
// is to consider that if there's already an element in the
// array, and the commandline expects one or more args, we
// leave last tokens to arguments
- if ($lastopt->action == 'StoreArray' &&
+ if ($lastopt->action == 'StoreArray' &&
!empty($result->options[$lastopt->name]) &&
count($this->args) > ($argc + count($args))) {
if (!is_null($token)) {
@@ -978,7 +982,7 @@ protected function parseToken($token, $result, &$args, $argc)
// a long option
$optkv = explode('=', $token, 2);
if (trim($optkv[0]) == '--') {
- // the special argument "--" forces in all cases the end of
+ // the special argument "--" forces in all cases the end of
// option scanning.
$stopflag = true;
return;
@@ -1002,7 +1006,7 @@ protected function parseToken($token, $result, &$args, $argc)
);
}
if ($opt->expectsArgument() && $value === false) {
- // maybe the long option argument is separated by a space, if
+ // maybe the long option argument is separated by a space, if
// this is the case it will be the next arg
if ($last && !$opt->argument_optional) {
throw CommandLine\Exception::factory(
@@ -1057,7 +1061,7 @@ protected function parseToken($token, $result, &$args, $argc)
}
$value = false;
} else {
- if (!$opt->expectsArgument()) {
+ if (!$opt->expectsArgument()) {
if ($nextopt = $this->findOption('-' . $next)) {
$this->_dispatchAction($opt, false, $result);
$this->parseToken('-' . substr($token, 2), $result,
@@ -1080,7 +1084,7 @@ protected function parseToken($token, $result, &$args, $argc)
$this->_dispatchAction($opt, $value, $result);
} else {
// We have an argument.
- // if we are in POSIX compliant mode, we must set the stop flag to
+ // if we are in POSIX compliant mode, we must set the stop flag to
// true in order to stop option parsing.
if (!$stopflag && $this->force_posix) {
$stopflag = true;
@@ -1105,7 +1109,7 @@ public function addBuiltinOptions()
$helpOptionParams = array(
'long_name' => '--help',
'description' => 'show this help message and exit',
- 'action' => 'Help'
+ 'action' => 'Help'
);
if (!($option = $this->findOption('-h')) || $option->action == 'Help') {
// short name is available, take it
@@ -1117,7 +1121,7 @@ public function addBuiltinOptions()
$versionOptionParams = array(
'long_name' => '--version',
'description' => 'show the program version and exit',
- 'action' => 'Version'
+ 'action' => 'Version'
);
if (!$this->findOption('-v')) {
// short name is available, take it
@@ -1125,7 +1129,7 @@ public function addBuiltinOptions()
}
$this->addOption('version', $versionOptionParams);
}
- }
+ }
// }}}
// getArgcArgv() {{{
@@ -1135,7 +1139,7 @@ public function addBuiltinOptions()
* if it fails to get them.
*
* @return array The argc/argv array
- * @throws PEAR2\Console\CommandLine\Exception
+ * @throws PEAR2\Console\CommandLine\Exception
*/
protected function getArgcArgv()
{
@@ -1150,7 +1154,7 @@ protected function getArgcArgv()
$opt = $this->findOption($key);
if ($opt instanceof CommandLine\Option) {
// match a configured option
- $argv[] = $opt->short_name ?
+ $argv[] = $opt->short_name ?
$opt->short_name : $opt->long_name;
foreach ($value as $v) {
if ($opt->expectsArgument()) {
View
18 src/Console/CommandLine/Argument.php
@@ -11,11 +11,11 @@
* through the world-wide-web at the following URI:
* http://opensource.org/licenses/mit-license.php
*
- * @category Console
+ * @category Console
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @link http://pear.php.net/package/Console_CommandLine
* @since File available since release 0.1.0
@@ -29,7 +29,7 @@
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
@@ -40,7 +40,7 @@ class Argument extends Element
// Public properties {{{
/**
- * Setting this to true will tell the parser that the argument expects more
+ * Setting this to true will tell the parser that the argument expects more
* than one argument and that argument values should be stored in an array.
*
* @var boolean $multiple Whether the argument expects multiple values
@@ -48,9 +48,9 @@ class Argument extends Element
public $multiple = false;
/**
- * Setting this to true will tell the parser that the argument is optional
+ * Setting this to true will tell the parser that the argument is optional
* and can be ommited.
- * Note that it is not a good practice to make arguments optional, it is
+ * Note that it is not a good practice to make arguments optional, it is
* the role of the options to be optional, by essence.
*
* @var boolean $optional Whether the argument is optional or not.
@@ -78,6 +78,12 @@ public function validate()
array('{$name}' => $this->name)
);
}
+ if (!$this->optional && $this->default !== null) {
+ Console_CommandLine::triggerError(
+ 'argument_no_default',
+ E_USER_ERROR
+ );
+ }
parent::validate();
}
View
16 src/Console/CommandLine/Element.php
@@ -11,11 +11,11 @@
* through the world-wide-web at the following URI:
* http://opensource.org/licenses/mit-license.php
*
- * @category Console
+ * @category Console
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @link http://pear.php.net/package/Console_CommandLine
* @since File available since release 0.1.0
@@ -29,7 +29,7 @@
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
@@ -47,7 +47,7 @@
public $name;
/**
- * The name of variable displayed in the usage message, if no set it
+ * The name of variable displayed in the usage message, if no set it
* defaults to the "name" property.
*
* @var string $help_name Element "help" variable name
@@ -60,6 +60,12 @@
* @var string $description Element description
*/
public $description;
+ /**
+ * The default value of the element if not provided on the command line.
+ *
+ * @var mixed $default Default value of the option.
+ */
+ public $default;
/**
* Custom errors messages for this element
@@ -101,7 +107,7 @@
*
* @return void
*/
- public function __construct($name = null, $params = array())
+ public function __construct($name = null, $params = array())
{
$this->name = $name;
foreach ($params as $attr => $value) {
View
49 src/Console/CommandLine/Option.php
@@ -11,11 +11,11 @@
* through the world-wide-web at the following URI:
* http://opensource.org/licenses/mit-license.php
*
- * @category Console
+ * @category Console
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @link http://pear.php.net/package/Console_CommandLine
* @since File available since release 0.1.0
@@ -29,7 +29,7 @@
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
@@ -64,14 +64,7 @@ class Option extends Element
public $action = 'StoreString';
/**
- * The default value of the option if not provided on the command line.
- *
- * @var mixed $default Default value of the option.
- */
- public $default;
-
- /**
- * An array of possible values for the option. If this array is not empty
+ * An array of possible values for the option. If this array is not empty
* and the value passed is not in the array an exception is raised.
* This only make sense for actions that accept values of course.
*
@@ -80,10 +73,10 @@ class Option extends Element
public $choices = array();
/**
- * The callback function (or method) to call for an action of type
- * Callback, this can be any callable supported by the php function
+ * The callback function (or method) to call for an action of type
+ * Callback, this can be any callable supported by the php function
* call_user_func.
- *
+ *
* Example:
*
* <code>
@@ -100,8 +93,8 @@ class Option extends Element
public $callback;
/**
- * An associative array of additional params to pass to the class
- * corresponding to the action, this array will also be passed to the
+ * An associative array of additional params to pass to the class
+ * corresponding to the action, this array will also be passed to the
* callback defined for an action of type Callback, Example:
*
* <code>
@@ -116,7 +109,7 @@ class Option extends Element
* // if the user type:
* // $ <yourprogram> -m spam
* // in your MyCustomAction class the execute() method will be called
- * // with the value 'spam' as first parameter and
+ * // with the value 'spam' as first parameter and
* // array('foo'=>true, 'bar'=>false) as second parameter
* </code>
*
@@ -125,7 +118,7 @@ class Option extends Element
public $action_params = array();
/**
- * For options that expect an argument, this property tells the parser if
+ * For options that expect an argument, this property tells the parser if
* the option argument is optional and can be ommited.
*
* @var bool $argumentOptional Whether the option arg is optional or not
@@ -134,7 +127,7 @@ class Option extends Element
/**
* For options that uses the "choice" property only.
- * Adds a --list-<choice> option to the parser that displays the list of
+ * Adds a --list-<choice> option to the parser that displays the list of
* choices for the option.
*
* @var bool $add_list_option Whether to add a list option or not
@@ -162,11 +155,11 @@ class Option extends Element
*
* @return void
*/
- public function __construct($name = null, $params = array())
+ public function __construct($name = null, $params = array())
{
parent::__construct($name, $params);
if ($this->action == 'Password') {
- // special case for Password action, password can be passed to the
+ // special case for Password action, password can be passed to the
// commandline or prompted by the parser
$this->argument_optional = true;
}
@@ -207,7 +200,7 @@ public function toString($delim = ", ")
// expectsArgument() {{{
/**
- * Returns true if the option requires one or more argument and false
+ * Returns true if the option requires one or more argument and false
* otherwise.
*
* @return bool Whether the option expects an argument or not
@@ -226,7 +219,7 @@ public function expectsArgument()
// dispatchAction() {{{
/**
- * Formats the value $value according to the action of the option and
+ * Formats the value $value according to the action of the option and
* updates the passed PEAR2\Console\CommandLine_Result object.
*
* @param mixed $value The value to format
@@ -286,20 +279,20 @@ public function validate()
E_USER_ERROR, array('{$name}' => $this->name));
}
// check if the option short_name is valid
- if ($this->short_name != null &&
+ if ($this->short_name != null &&
!(preg_match('/^\-[a-zA-Z]{1}$/', $this->short_name))) {
Console\CommandLine::triggerError('option_bad_short_name',
E_USER_ERROR, array(
- '{$name}' => $this->name,
+ '{$name}' => $this->name,
'{$short_name}' => $this->short_name
));
}
// check if the option long_name is valid
- if ($this->long_name != null &&
+ if ($this->long_name != null &&
!preg_match('/^\-\-[a-zA-Z]+[a-zA-Z0-9_\-]*$/', $this->long_name)) {
Console\CommandLine::triggerError('option_bad_long_name',
E_USER_ERROR, array(
- '{$name}' => $this->name,
+ '{$name}' => $this->name,
'{$long_name}' => $this->long_name
));
}
@@ -328,7 +321,7 @@ public function validate()
/**
* Set the default value according to the configured action.
*
- * Note that for backward compatibility issues this method is only called
+ * Note that for backward compatibility issues this method is only called
* when the 'force_options_defaults' is set to true, it will become the
* default behaviour in the next major release of PEAR2\Console\CommandLine.
*
View
37 src/Console/CommandLine/Renderer/Default.php
@@ -11,11 +11,11 @@
* through the world-wide-web at the following URI:
* http://opensource.org/licenses/mit-license.php
*
- * @category Console
+ * @category Console
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @link http://pear.php.net/package/Console_CommandLine
* @since File available since release 0.1.0
@@ -28,7 +28,7 @@
* @package PEAR2\Console\CommandLine
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007-2009 David JEAN LOUIS
- * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
@@ -69,7 +69,7 @@ class Renderer_Default implements Renderer
*
* @return void
*/
- public function __construct($parser = false)
+ public function __construct($parser = false)
{
$this->parser = $parser;
}
@@ -85,7 +85,7 @@ public function __construct($parser = false)
public function usage()
{
$ret = '';
- if (!empty($this->parser->description)) {
+ if (!empty($this->parser->description)) {
$ret .= $this->description() . "\n\n";
}
$ret .= $this->usageLine() . "\n";
@@ -159,9 +159,9 @@ protected function name()
$parent = $this->parser->parent;
while ($parent) {
if (count($parent->options) > 0) {
- $name = '['
+ $name = '['
. strtolower($this->parser->message_provider->get('OPTION_WORD',
- array('plural' => 's')))
+ array('plural' => 's')))
. '] ' . $name;
}
$name = $parent->name . ' ' . $name;
@@ -196,13 +196,20 @@ protected function usageLine()
$usage = $this->parser->message_provider->get('USAGE_WORD') . ":\n";
$ret = $usage . ' ' . $this->name();
if (count($this->parser->options) > 0) {
- $ret .= ' ['
+ $ret .= ' ['
. strtolower($this->parser->message_provider->get('OPTION_WORD'))
. ']';
}
if (count($this->parser->args) > 0) {
foreach ($this->parser->args as $name=>$arg) {
- $ret .= ' <' . $arg->help_name . ($arg->multiple?'...':'') . '>';
+ $arg_str = $arg->help_name;
+ if ($arg->multiple) {
+ $arg_str .= '1 ' . $arg->help_name . '2 ...';
+ }
+ if ($arg->optional) {
+ $arg_str = '[' . $arg_str . ']';
+ }
+ $ret .= ' ' . $arg_str;
}
}
return $this->columnWrap($ret, 2);
@@ -223,7 +230,7 @@ protected function commandUsageLine()
}
$ret = ' ' . $this->name();
if (count($this->parser->options) > 0) {
- $ret .= ' ['
+ $ret .= ' ['
. strtolower($this->parser->message_provider->get('OPTION_WORD'))
. ']';
}
@@ -234,7 +241,7 @@ protected function commandUsageLine()
if (!$hasArgs && count($command->args) > 0) {
$hasArgs = true;
}
- if (!$hasOptions && ($command->add_help_option ||
+ if (!$hasOptions && ($command->add_help_option ||
$command->add_version_option || count($command->options) > 0)) {
$hasOptions = true;
}
@@ -252,7 +259,7 @@ protected function commandUsageLine()
// argumentList() {{{
/**
- * Render the arguments list that will be displayed to the user, you can
+ * Render the arguments list that will be displayed to the user, you can
* override this method if you want to change the look of the list.
*
* @return string The formatted argument list
@@ -281,7 +288,7 @@ protected function argumentList()
// optionList() {{{
/**
- * Render the options list that will be displayed to the user, you can
+ * Render the options list that will be displayed to the user, you can
* override this method if you want to change the look of the list.
*
* @return string The formatted option list
@@ -324,7 +331,7 @@ protected function optionList()
// commandList() {{{
/**
- * Render the command list that will be displayed to the user, you can
+ * Render the command list that will be displayed to the user, you can
* override this method if you want to change the look of the list.
*
* @return string The formatted subcommand list
@@ -397,7 +404,7 @@ protected function columnWrap($text, $cw)
{
$tokens = explode("\n", $this->wrap($text));
$ret = $tokens[0];
- $chunks = $this->wrap(trim(substr($text, strlen($ret))),
+ $chunks = $this->wrap(trim(substr($text, strlen($ret))),
$this->line_width - $cw);
$tokens = explode("\n", $chunks);
foreach ($tokens as $token) {
View
18 tests/console_commandline_addargument.phpt
@@ -13,7 +13,7 @@ $parser->addArgument('arg2', array(
));
$arg3 = new PEAR2\Console\CommandLine\Argument('arg3', array(
'multiple' => true,
- 'description' => 'description of arg3'
+ 'description' => 'description of arg3'
));
$parser->addArgument($arg3);
$parser->addArgument('arg4', array('optional' => true));
@@ -27,7 +27,7 @@ $parser->addArgument('Some invalid name');
--EXPECTF--
array(4) {
["arg1"]=>
- object(PEAR2\Console\CommandLine\Argument)#6 (6) {
+ object(PEAR2\Console\CommandLine\Argument)#6 (7) {
["multiple"]=>
bool(false)
["optional"]=>
@@ -38,12 +38,14 @@ array(4) {
string(4) "arg1"
["description"]=>
NULL
+ ["default"]=>
+ NULL
["messages"]=>
array(0) {
}
}
["arg2"]=>
- object(PEAR2\Console\CommandLine\Argument)#7 (6) {
+ object(PEAR2\Console\CommandLine\Argument)#7 (7) {
["multiple"]=>
bool(true)
["optional"]=>
@@ -54,12 +56,14 @@ array(4) {
string(4) "arg2"
["description"]=>
string(19) "description of arg2"
+ ["default"]=>
+ NULL
["messages"]=>
array(0) {
}
}
["arg3"]=>
- object(PEAR2\Console\CommandLine\Argument)#8 (6) {
+ object(PEAR2\Console\CommandLine\Argument)#8 (7) {
["multiple"]=>
bool(true)
["optional"]=>
@@ -70,12 +74,14 @@ array(4) {
string(4) "arg3"
["description"]=>
string(19) "description of arg3"
+ ["default"]=>
+ NULL
["messages"]=>
array(0) {
}
}
["arg4"]=>
- object(PEAR2\Console\CommandLine\Argument)#9 (6) {
+ object(PEAR2\Console\CommandLine\Argument)#9 (7) {
["multiple"]=>
bool(false)
["optional"]=>
@@ -86,6 +92,8 @@ array(4) {
string(4) "arg4"
["description"]=>
NULL
+ ["default"]=>
+ NULL
["messages"]=>
array(0) {
}
View
28 tests/console_commandline_addargument2.phpt
@@ -0,0 +1,28 @@
+--TEST--
+Test for Console_CommandLine::addArgument() method.
+--SKIPIF--
+<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
+--ARGS--
+foo
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+$parser = new PEAR2\Console\CommandLine();
+$parser->addArgument('arg1');
+$parser->addArgument('arg2', array(
+ 'optional' => true,
+ 'default' => 'bar'
+));
+
+$result = $parser->parse();
+echo $result->args['arg1'] . ' ' . $result->args['arg2'];
+
+// a bad argument
+$parser->addArgument('arg3', array('default' => 'baz'));
+
+?>
+--EXPECTF--
+foo bar
+Fatal error: only optional arguments can have a default value in %sCommandLine.php on line %d
View
16 tests/console_commandline_addoption.phpt
@@ -38,8 +38,6 @@ array(4) {
NULL
["action"]=>
string(11) "StoreString"
- ["default"]=>
- NULL
["choices"]=>
array(0) {
}
@@ -60,6 +58,8 @@ array(4) {
string(4) "opt1"
["description"]=>
NULL
+ ["default"]=>
+ NULL
["messages"]=>
array(0) {
}
@@ -72,8 +72,6 @@ array(4) {
string(5) "--foo"
["action"]=>
string(8) "StoreInt"
- ["default"]=>
- int(2)
["choices"]=>
array(3) {
[0]=>
@@ -100,6 +98,8 @@ array(4) {
string(3) "bar"
["description"]=>
string(19) "description of opt2"
+ ["default"]=>
+ int(2)
["messages"]=>
array(0) {
}
@@ -112,8 +112,6 @@ array(4) {
string(11) "--list-opt2"
["action"]=>
string(4) "List"
- ["default"]=>
- NULL
["choices"]=>
array(0) {
}
@@ -143,6 +141,8 @@ array(4) {
string(9) "list_opt2"
["description"]=>
string(35) "lists valid choices for option opt2"
+ ["default"]=>
+ NULL
["messages"]=>
array(0) {
}
@@ -155,8 +155,6 @@ array(4) {
string(5) "--bar"
["action"]=>
string(11) "StoreString"
- ["default"]=>
- NULL
["choices"]=>
array(0) {
}
@@ -177,6 +175,8 @@ array(4) {
string(4) "opt3"
["description"]=>
string(19) "description of opt3"
+ ["default"]=>
+ NULL
["messages"]=>
array(0) {
}
View
2  tests/console_commandline_parse_12.phpt
@@ -17,7 +17,7 @@ $result = $parser->parse();
install given package
Usage:
- some_program [options] install [options] <package>
+ some_program [options] install [options] package
Options:
-f, --force force installation
View
2  tests/console_commandline_parse_2.phpt
@@ -17,7 +17,7 @@ $parser->parse();
Description of our parser goes here...
Usage:
- some_program [options] <simple> <multiple...>
+ some_program [options] simple [multiple1 multiple2 ...]
Options:
-t, --true test the StoreTrue action
View
2  tests/console_commandline_parse_21.phpt
@@ -19,7 +19,7 @@ $parser->parse();
Description of our parser goes here...
Usage:
- some_program [options] <simple> <multiple...>
+ some_program [options] simple [multiple1 multiple2 ...]
Options:
-t
Please sign in to comment.
Something went wrong with that request. Please try again.