Modified: pear2/sandbox/Console_CommandLine/trunk/data/xmlschema.rng
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/data/xmlschema.rng 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/data/xmlschema.rng 2010-05-10 03:52:17 UTC (rev 299197)
@@ -49,6 +49,9 @@
+
+
+
@@ -90,6 +93,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -109,6 +127,9 @@
+
+
+
@@ -166,6 +187,11 @@
+
+
+
+
+
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action/StoreArray.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action/StoreArray.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action/StoreArray.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -39,6 +39,16 @@
*/
class PEAR2_Console_CommandLine_Action_StoreArray extends PEAR2_Console_CommandLine_Action
{
+ // Protected properties {{{
+
+ /**
+ * Force a clean result when first called, overriding any defaults assigned.
+ *
+ * @var object $firstPass First time this action has been called.
+ */
+ protected $firstPass = true;
+
+ // }}}
// execute() {{{
/**
@@ -52,8 +62,9 @@
public function execute($value = false, $params = array())
{
$result = $this->getResult();
- if (null === $result) {
- $result = array();
+ if (null === $result || $this->firstPass) {
+ $result = array();
+ $this->firstPass = false;
}
$result[] = $value;
$this->setResult($result);
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action/StoreFloat.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action/StoreFloat.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action/StoreFloat.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -51,7 +51,7 @@
* @return string
* @throws PEAR2_Console_CommandLine_Exception
*/
- public function execute($value=false, $params=array())
+ public function execute($value = false, $params = array())
{
if (!is_numeric($value)) {
throw PEAR2_Console_CommandLine_Exception::factory(
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Action.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -95,6 +95,22 @@
}
// }}}
+ // format() {{{
+
+ /**
+ * Allow a value to be pre-formatted prior to being used in a choices test.
+ * Setting $value to the new format will keep the formatting.
+ *
+ * @param mixed &$value The value to format
+ *
+ * @return mixed The formatted value
+ */
+ public function format(&$value)
+ {
+ return $value;
+ }
+
+ // }}}
// setResult() {{{
/**
Added: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/CustomMessageProvider.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/CustomMessageProvider.php (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/CustomMessageProvider.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,66 @@
+
+ * @author Michael Gauthier
+ * @copyright 2007 David JEAN LOUIS, 2009 silverorange
+ * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @version CVS: $Id: CustomMessageProvider.php 282427 2009-06-19 10:22:48Z izi $
+ * @link http://pear.php.net/package/Console_CommandLine
+ * @since File available since release 1.1.0
+ * @filesource
+ */
+
+/**
+ * Common interfacefor message providers that allow overriding with custom
+ * messages
+ *
+ * Message providers may optionally implement this interface.
+ *
+ * @category Console
+ * @package PEAR2_Console_CommandLine
+ * @author David JEAN LOUIS
+ * @author Michael Gauthier
+ * @copyright 2007 David JEAN LOUIS, 2009 silverorange
+ * @license http://opensource.org/licenses/mit-license.php MIT License
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/Console_CommandLine
+ * @since Interface available since release 1.1.0
+ */
+interface PEAR2_Console_CommandLine_CustomMessageProvider
+{
+ // getWithCustomMesssages() {{{
+
+ /**
+ * Retrieves the given string identifier corresponding message.
+ *
+ * For a list of identifiers please see the provided default message
+ * provider.
+ *
+ * @param string $code The string identifier of the message
+ * @param array $vars An array of template variables
+ * @param array $messages An optional array of messages to use. Array
+ * indexes are message codes.
+ *
+ * @return string
+ * @see PEAR2_Console_CommandLine_MessageProvider
+ * @see PEAR2_Console_CommandLine_MessageProvider_Default
+ */
+ public function getWithCustomMessages(
+ $code, $vars = array(), $messages = array()
+ );
+
+ // }}}
+}
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Element.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Element.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Element.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -60,6 +60,35 @@
*/
public $description;
+ /**
+ * Custom errors messages for this element
+ *
+ * This array is of the form:
+ *
+ * $messageText,
+ * $messageName => $messageText,
+ * ...
+ * );
+ * ?>
+ *
+ *
+ * If specified, these messages override the messages provided by the
+ * default message provider. For example:
+ *
+ * 'The argument foo is required.',
+ * );
+ * ?>
+ *
+ *
+ * @var array
+ * @see PEAR2_Console_CommandLine_MessageProvider_Default
+ */
+ public $messages = array();
+
// }}}
// __construct() {{{
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Exception.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Exception.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Exception.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -46,24 +46,38 @@
const OPTION_VALUE_TYPE_ERROR = 3;
const OPTION_UNKNOWN = 4;
const ARGUMENT_REQUIRED = 5;
+ const INVALID_SUBCOMMAND = 6;
/**#@-*/
// }}}
// factory() {{{
/**
- * Convenience method that builds the exception with the array of params by
+ * Convenience method that builds the exception with the array of params by
* calling the message provider class.
*
- * @param string $code The string identifier of the exception
- * @param array $params Array of template vars/values
- * @param PEAR2_Console_CommandLine $parser An instance of the parser
+ * @param string $code The string identifier of the
+ * exception.
+ * @param array $params Array of template vars/values
+ * @param PEAR2_Console_CommandLine $parser An instance of the parser
+ * @param array $messages An optional array of messages
+ * passed to the message provider.
*
* @return PEAR2_Console_CommandLine_Exception The exception instance
*/
- public static function factory($code, $params, $parser)
- {
- $msg = $parser->message_provider->get($code, $params);
+ public static function factory(
+ $code, $params, $parser, array $messages = array()
+ ) {
+ $provider = $parser->message_provider;
+ if ($provider instanceof PEAR2_Console_CommandLine_CustomMessageProvider) {
+ $msg = $provider->getWithCustomMessages(
+ $code,
+ $params,
+ $messages
+ );
+ } else {
+ $msg = $provider->get($code, $params);
+ }
$const = 'PEAR2_Console_CommandLine_Exception::' . $code;
$code = defined($const) ? constant($const) : 0;
return new PEAR2_Console_CommandLine_Exception($msg, $code);
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/MessageProvider/Default.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/MessageProvider/Default.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/MessageProvider/Default.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -37,7 +37,9 @@
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
*/
-class PEAR2_Console_CommandLine_MessageProvider_Default implements PEAR2_Console_CommandLine_MessageProvider
+class PEAR2_Console_CommandLine_MessageProvider_Default
+ implements PEAR2_Console_CommandLine_MessageProvider,
+ PEAR2_Console_CommandLine_CustomMessageProvider
{
// Properties {{{
@@ -66,6 +68,7 @@
'INVALID_CUSTOM_INSTANCE' => 'Instance does not implement the required interface',
'LIST_OPTION_MESSAGE' => 'lists valid choices for option {$name}',
'LIST_DISPLAYED_MESSAGE' => 'Valid choices are: ',
+ 'INVALID_SUBCOMMAND' => 'Command "{$command}" is not valid.',
);
// }}}
@@ -84,12 +87,55 @@
if (!isset($this->messages[$code])) {
return 'UNKNOWN';
}
+ return $this->replaceTemplateVars($this->messages[$code], $vars);
+ }
+
+ // }}}
+ // getWithCustomMessages() {{{
+
+ /**
+ * Retrieve the given string identifier corresponding message.
+ *
+ * @param string $code The string identifier of the message
+ * @param array $vars An array of template variables
+ * @param array $messages An optional array of messages to use. Array
+ * indexes are message codes.
+ *
+ * @return string
+ */
+ public function getWithCustomMessages(
+ $code, $vars = array(), $messages = array()
+ ) {
+ // get message
+ if (isset($messages[$code])) {
+ $message = $messages[$code];
+ } elseif (isset($this->messages[$code])) {
+ $message = $this->messages[$code];
+ } else {
+ $message = 'UNKNOWN';
+ }
+ return $this->replaceTemplateVars($message, $vars);
+ }
+
+ // }}}
+ // replaceTemplateVars() {{{
+
+ /**
+ * Replaces template vars in a message
+ *
+ * @param string $message The message
+ * @param array $vars An array of template variables
+ *
+ * @return string
+ */
+ protected function replaceTemplateVars($message, $vars = array())
+ {
$tmpkeys = array_keys($vars);
$keys = array();
foreach ($tmpkeys as $key) {
$keys[] = '{$' . $key . '}';
}
- return str_replace($keys, array_values($vars), $this->messages[$code]);
+ return str_replace($keys, array_values($vars), $message);
}
// }}}
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Option.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Option.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Option.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -67,7 +67,7 @@
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.
*
@@ -138,6 +138,16 @@
public $add_list_option = false;
// }}}
+ // Private properties {{{
+
+ /**
+ * When an action is called remember it to allow for multiple calls.
+ *
+ * @var object $action_instance Placeholder for action
+ */
+ private $_action_instance = null;
+
+ // }}}
// __construct() {{{
/**
@@ -224,25 +234,26 @@
*/
public function dispatchAction($value, $result, $parser)
{
+ $actionInfo = PEAR2_Console_CommandLine::$actions[$this->action];
+ $clsname = $actionInfo[0];
+ if ($this->_action_instance === null) {
+ $this->_action_instance = new $clsname($result, $this, $parser);
+ }
+
// check value is in option choices
- if (!empty($this->choices) && !in_array($value, $this->choices)) {
+ if (!empty($this->choices) && !in_array($this->_action_instance->format($value), $this->choices)) {
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_VALUE_NOT_VALID',
array(
'name' => $this->name,
'choices' => implode('", "', $this->choices),
'value' => $value,
- ), $parser
+ ),
+ $parser,
+ $this->messages
);
}
- $actionInfo = PEAR2_Console_CommandLine::$actions[$this->action];
- if (true === $actionInfo[1]) {
- // we have a "builtin" action
- $tokens = explode('_', $actionInfo[0]);
- }
- $clsname = $actionInfo[0];
- $action = new $clsname($result, $this, $parser);
- $action->execute($value, $this->action_params);
+ $this->_action_instance->execute($value, $this->action_params);
}
// }}}
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Renderer/Default.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Renderer/Default.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/Renderer/Default.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -154,19 +154,18 @@
*/
protected function name()
{
- $name = '';
+ $name = $this->parser->name;
$parent = $this->parser->parent;
while ($parent) {
- $name .= $parent->name . ' ';
if (count($parent->options) > 0) {
- $name .= '['
+ $name = '['
. strtolower($this->parser->message_provider->get('OPTION_WORD',
array('plural' => 's')))
- . '] ';
+ . '] ' . $name;
}
+ $name = $parent->name . ' ' . $name;
$parent = $parent->parent;
}
- $name .= $this->parser->name;
return $this->wrap($name);
}
@@ -227,8 +226,24 @@
. strtolower($this->parser->message_provider->get('OPTION_WORD'))
. ']';
}
- //XXX
- $ret .= " [options] [args]";
+ $ret .= " ";
+ $hasArgs = false;
+ $hasOptions = false;
+ foreach ($this->parser->commands as $command) {
+ if (!$hasArgs && count($command->args) > 0) {
+ $hasArgs = true;
+ }
+ if (!$hasOptions && ($command->add_help_option ||
+ $command->add_version_option || count($command->options) > 0)) {
+ $hasOptions = true;
+ }
+ }
+ if ($hasOptions) {
+ $ret .= ' [options]';
+ }
+ if ($hasArgs) {
+ $ret .= ' [args]';
+ }
return $this->columnWrap($ret, 2);
}
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/XmlParser.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/XmlParser.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine/XmlParser.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -113,7 +113,7 @@
// _parseCommandNode() {{{
/**
- * Parses the root command node or a command node and return the
+ * Parses the root command node or a command node and returns the
* constructed PEAR2_Console_CommandLine or PEAR2_Console_CommandLine_Command
* instance.
*
@@ -160,6 +160,9 @@
}
}
break;
+ case 'messages':
+ $obj->messages = self::_messages($cNode);
+ break;
default:
break;
}
@@ -183,14 +186,22 @@
$obj = new PEAR2_Console_CommandLine_Option($node->getAttribute('name'));
foreach ($node->childNodes as $cNode) {
$cNodeName = $cNode->nodeName;
- if ($cNodeName == 'choices') {
+ switch ($cNodeName) {
+ case 'choices':
foreach ($cNode->childNodes as $subChildNode) {
if ($subChildNode->nodeName == 'choice') {
$obj->choices[] = trim($subChildNode->nodeValue);
}
}
- } elseif (property_exists($obj, $cNodeName)) {
- $obj->$cNodeName = trim($cNode->nodeValue);
+ break;
+ case 'messages':
+ $obj->messages = self::_messages($cNode);
+ break;
+ default:
+ if (property_exists($obj, $cNodeName)) {
+ $obj->$cNodeName = trim($cNode->nodeValue);
+ }
+ break;
}
}
if ($obj->action == 'Password') {
@@ -224,6 +235,12 @@
case 'multiple':
$obj->multiple = self::_bool(trim($cNode->nodeValue));
break;
+ case 'optional':
+ $obj->optional = self::_bool(trim($cNode->nodeValue));
+ break;
+ case 'messages':
+ $obj->messages = self::_messages($cNode);
+ break;
default:
break;
}
@@ -247,4 +264,33 @@
}
// }}}
+ // _messages() {{{
+
+ /**
+ * Returns an array of custom messages for the element
+ *
+ * @param DOMNode $node The messages node to process
+ *
+ * @return array an array of messages
+ *
+ * @see PEAR2_Console_CommandLine::$messages
+ * @see PEAR2_Console_CommandLine_Element::$messages
+ */
+ private static function _messages(DOMNode $node)
+ {
+ $messages = array();
+
+ foreach ($node->childNodes as $cNode) {
+ if ($cNode->nodeType == XML_ELEMENT_NODE) {
+ $name = $cNode->getAttribute('name');
+ $value = trim($cNode->nodeValue);
+
+ $messages[$name] = $value;
+ }
+ }
+
+ return $messages;
+ }
+
+ // }}}
}
Modified: pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/src/Console/CommandLine.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -217,6 +217,38 @@
);
/**
+ * Custom errors messages for this command
+ *
+ * This array is of the form:
+ *
+ * $messageText,
+ * $messageName => $messageText,
+ * ...
+ * );
+ * ?>
+ *
+ *
+ * If specified, these messages override the messages provided by the
+ * default message provider. For example:
+ *
+ * 'The argument foo is required.',
+ * );
+ * ?>
+ *
+ *
+ * @var array
+ * @see PEAR2_Console_CommandLine_MessageProvider_Default
+ */
+ public $messages = array();
+
+ // }}}
+ // {{{ Private properties
+
+ /**
* Array of options that must be dispatched at the end.
*
* @var array $_dispatchLater Options to be dispatched
@@ -235,8 +267,8 @@
* '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
+ * 'add_help_option' => true, // or false to disable --help option
+ * 'add_version_option' => true, // or false to disable --version option
* 'force_posix' => false // or true to force posix compliance
* ));
*
@@ -273,6 +305,9 @@
} else if (getenv('POSIXLY_CORRECT')) {
$this->force_posix = true;
}
+ if (isset($params['messages']) && is_array($params['messages'])) {
+ $this->messages = $params['messages'];
+ }
// set default instances
$this->renderer = new PEAR2_Console_CommandLine_Renderer_Default($this);
$this->outputter = new PEAR2_Console_CommandLine_Outputter_Default();
@@ -307,7 +342,9 @@
} else {
throw PEAR2_Console_CommandLine_Exception::factory(
'INVALID_CUSTOM_INSTANCE',
- array(), $this
+ array(),
+ $this,
+ $this->messages
);
}
}
@@ -479,8 +516,30 @@
} else {
$params['name'] = $name;
$command = new PEAR2_Console_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
+ // properties have already been set
+ $cascade = array(
+ 'add_help_option',
+ 'add_version_option',
+ 'outputter',
+ 'message_provider',
+ 'force_posix',
+ 'force_options_defaults'
+ );
+ foreach ($cascade as $property) {
+ if (!isset($params[$property])) {
+ $command->$property = $this->$property;
+ }
+ }
+ if (!isset($params['renderer'])) {
+ $renderer = clone $this->renderer;
+ $renderer->parser = $command;
+ $command->renderer = $renderer;
+ }
}
- $command->parent = $this;
+ $command->parent = $this;
$this->commands[$command->name] = $command;
return $command;
}
@@ -567,47 +626,62 @@
// displayError() {{{
/**
- * Displays an error to the user and exit with $exitCode.
+ * Displays an error to the user via stderr and exit with $exitCode if its
+ * value is not equals to false.
*
* @param string $error The error message
- * @param int $exitCode The exit code number
+ * @param int $exitCode The exit code number (default: 1). If set to
+ * false, the exit() function will not be called
*
* @return void
*/
public function displayError($error, $exitCode = 1)
{
$this->outputter->stderr($this->renderer->error($error));
- exit($exitCode);
+ if ($exitCode !== false) {
+ exit($exitCode);
+ }
}
// }}}
// displayUsage() {{{
/**
- * Displays the usage help message to the user and exit with $exitCode
+ * Displays the usage help message to the user via stdout and exit with
+ * $exitCode if its value is not equals to false.
*
- * @param int $exitCode The exit code number
+ * @param int $exitCode The exit code number (default: 0). If set to
+ * false, the exit() function will not be called
*
* @return void
*/
- public function displayUsage($exitCode = 1)
+ public function displayUsage($exitCode = 0)
{
- $this->outputter->stderr($this->renderer->usage());
- exit($exitCode);
+ $this->outputter->stdout($this->renderer->usage());
+ if ($exitCode !== false) {
+ exit($exitCode);
+ }
}
// }}}
// displayVersion() {{{
/**
- * Displays the program version to the user
+ * Displays the program version to the user via stdout and exit with
+ * $exitCode if its value is not equals to false.
*
+ *
+ * @param int $exitCode The exit code number (default: 0). If set to
+ * false, the exit() function will not be called
+ *
* @return void
*/
- public function displayVersion()
+ public function displayVersion($exitCode = 0)
{
$this->outputter->stdout($this->renderer->version());
- exit(0);
+ if ($exitCode !== false) {
+ exit($exitCode);
+ }
}
// }}}
@@ -650,7 +724,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_AMBIGUOUS',
array('name' => $str, 'matches' => $matches_str),
- $this
+ $this,
+ $this->messages
);
}
return $matches[0];
@@ -769,7 +844,7 @@
array_shift($argv);
$argc--;
}
- // will contain aruments
+ // will contain arguments
$args = array();
foreach ($this->options as $name=>$option) {
$result->options[$name] = $option->default;
@@ -789,6 +864,22 @@
throw $exc;
}
}
+ // Parse a null token to allow any undespatched actions to be despatched.
+ $this->parseToken(null, $result, $args, 0);
+ // Check if an invalid subcommand was specified. If there are
+ // subcommands and no arguments, but an argument was provided, it is
+ // an invalid subcommand.
+ if ( count($this->commands) > 0
+ && count($this->args) === 0
+ && count($args) > 0
+ ) {
+ throw PEAR2_Console_CommandLine_Exception::factory(
+ 'INVALID_SUBCOMMAND',
+ array('command' => $args[0]),
+ $this,
+ $this->messages
+ );
+ }
// minimum argument number check
$argnum = 0;
foreach ($this->args as $name=>$arg) {
@@ -800,7 +891,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'ARGUMENT_REQUIRED',
array('argnum' => $argnum, 'plural' => $argnum>1 ? 's': ''),
- $this
+ $this,
+ $this->messages
);
}
// handle arguments
@@ -841,7 +933,6 @@
static $lastopt = false;
static $stopflag = false;
$last = $argc === 0;
- $token = trim($token);
if (!$stopflag && $lastopt) {
if (substr($token, 0, 1) == '-') {
if ($lastopt->argument_optional) {
@@ -856,7 +947,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_VALUE_REQUIRED',
array('name' => $lastopt->name),
- $this
+ $this,
+ $this->messages
);
}
} else {
@@ -867,10 +959,14 @@
if ($lastopt->action == 'StoreArray' &&
!empty($result->options[$lastopt->name]) &&
count($this->args) > ($argc + count($args))) {
- $args[] = $token;
+ if (!is_null($token)) {
+ $args[] = $token;
+ }
return;
}
- $this->_dispatchAction($lastopt, $token, $result);
+ if (!is_null($token) || $lastopt->action == 'Password') {
+ $this->_dispatchAction($lastopt, $token, $result);
+ }
if ($lastopt->action != 'StoreArray') {
$lastopt = false;
}
@@ -891,7 +987,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_UNKNOWN',
array('name' => $optkv[0]),
- $this
+ $this,
+ $this->messages
);
}
$value = isset($optkv[1]) ? $optkv[1] : false;
@@ -899,7 +996,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_VALUE_UNEXPECTED',
array('name' => $opt->name, 'value' => $value),
- $this
+ $this,
+ $this->messages
);
}
if ($opt->expectsArgument() && $value === false) {
@@ -909,7 +1007,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_VALUE_REQUIRED',
array('name' => $opt->name),
- $this
+ $this,
+ $this->messages
);
}
// we will have a value next time
@@ -933,7 +1032,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_UNKNOWN',
array('name' => $optname),
- $this
+ $this,
+ $this->messages
);
}
// parse other options or set the value
@@ -946,7 +1046,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_VALUE_REQUIRED',
array('name' => $opt->name),
- $this
+ $this,
+ $this->messages
);
}
// we will have a value next time
@@ -965,7 +1066,8 @@
throw PEAR2_Console_CommandLine_Exception::factory(
'OPTION_UNKNOWN',
array('name' => $next),
- $this
+ $this,
+ $this->messages
);
}
}
@@ -982,7 +1084,9 @@
if (!$stopflag && $this->force_posix) {
$stopflag = true;
}
- $args[] = $token;
+ if (!is_null($token)) {
+ $args[] = $token;
+ }
}
}
@@ -1049,7 +1153,7 @@
$opt->short_name : $opt->long_name;
foreach ($value as $v) {
if ($opt->expectsArgument()) {
- $argv[] = isset($_GET[$key]) ? urldecode($v) : $v;
+ $argv[] = isset($_REQUEST[$key]) ? urldecode($v) : $v;
} else if ($v == '0' || $v == 'false') {
array_pop($argv);
}
@@ -1057,7 +1161,7 @@
} else if (isset($this->args[$key])) {
// match a configured argument
foreach ($value as $v) {
- $argv[] = isset($_GET[$key]) ? urldecode($v) : $v;
+ $argv[] = isset($_REQUEST[$key]) ? urldecode($v) : $v;
}
}
}
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_addargument.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_addargument.phpt 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_addargument.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -27,7 +27,7 @@
--EXPECTF--
array(4) {
["arg1"]=>
- object(PEAR2_Console_CommandLine_Argument)#8 (5) {
+ object(PEAR2_Console_CommandLine_Argument)#6 (6) {
["multiple"]=>
bool(false)
["optional"]=>
@@ -38,9 +38,12 @@
string(4) "arg1"
["description"]=>
NULL
+ ["messages"]=>
+ array(0) {
+ }
}
["arg2"]=>
- object(PEAR2_Console_CommandLine_Argument)#9 (5) {
+ object(PEAR2_Console_CommandLine_Argument)#7 (6) {
["multiple"]=>
bool(true)
["optional"]=>
@@ -51,9 +54,12 @@
string(4) "arg2"
["description"]=>
string(19) "description of arg2"
+ ["messages"]=>
+ array(0) {
+ }
}
["arg3"]=>
- object(PEAR2_Console_CommandLine_Argument)#10 (5) {
+ object(PEAR2_Console_CommandLine_Argument)#8 (6) {
["multiple"]=>
bool(true)
["optional"]=>
@@ -64,9 +70,12 @@
string(4) "arg3"
["description"]=>
string(19) "description of arg3"
+ ["messages"]=>
+ array(0) {
+ }
}
["arg4"]=>
- object(PEAR2_Console_CommandLine_Argument)#11 (5) {
+ object(PEAR2_Console_CommandLine_Argument)#9 (6) {
["multiple"]=>
bool(false)
["optional"]=>
@@ -77,7 +86,10 @@
string(4) "arg4"
["description"]=>
NULL
+ ["messages"]=>
+ array(0) {
+ }
}
}
-Fatal error: argument name must be a valid php variable name (got: Some invalid name) in %sCommandLine.php on line %d
+Fatal error: argument name must be a valid php variable name (got: Some invalid name) in %sCommandLine.php on line %d
\ No newline at end of file
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_addoption.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_addoption.phpt 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_addoption.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -28,10 +28,10 @@
var_dump($parser->options);
?>
---EXPECT--
+--EXPECTF--
array(4) {
["opt1"]=>
- object(PEAR2_Console_CommandLine_Option)#8 (12) {
+ object(PEAR2_Console_CommandLine_Option)#6 (14) {
["short_name"]=>
string(2) "-a"
["long_name"]=>
@@ -52,15 +52,20 @@
bool(false)
["add_list_option"]=>
bool(false)
+ ["_action_instance":"PEAR2_Console_CommandLine_Option":private]=>
+ NULL
["name"]=>
string(4) "opt1"
["help_name"]=>
string(4) "opt1"
["description"]=>
NULL
+ ["messages"]=>
+ array(0) {
+ }
}
["opt2"]=>
- object(PEAR2_Console_CommandLine_Option)#9 (12) {
+ object(PEAR2_Console_CommandLine_Option)#7 (14) {
["short_name"]=>
string(2) "-b"
["long_name"]=>
@@ -87,15 +92,20 @@
bool(false)
["add_list_option"]=>
bool(true)
+ ["_action_instance":"PEAR2_Console_CommandLine_Option":private]=>
+ NULL
["name"]=>
string(4) "opt2"
["help_name"]=>
string(3) "bar"
["description"]=>
string(19) "description of opt2"
+ ["messages"]=>
+ array(0) {
+ }
}
["list_opt2"]=>
- object(PEAR2_Console_CommandLine_Option)#10 (12) {
+ object(PEAR2_Console_CommandLine_Option)#8 (14) {
["short_name"]=>
NULL
["long_name"]=>
@@ -125,15 +135,20 @@
bool(false)
["add_list_option"]=>
bool(false)
+ ["_action_instance":"PEAR2_Console_CommandLine_Option":private]=>
+ NULL
["name"]=>
string(9) "list_opt2"
["help_name"]=>
string(9) "list_opt2"
["description"]=>
string(35) "lists valid choices for option opt2"
+ ["messages"]=>
+ array(0) {
+ }
}
["opt3"]=>
- object(PEAR2_Console_CommandLine_Option)#11 (12) {
+ object(PEAR2_Console_CommandLine_Option)#9 (14) {
["short_name"]=>
NULL
["long_name"]=>
@@ -154,11 +169,16 @@
bool(false)
["add_list_option"]=>
bool(false)
+ ["_action_instance":"PEAR2_Console_CommandLine_Option":private]=>
+ NULL
["name"]=>
string(4) "opt3"
["help_name"]=>
string(4) "opt3"
["description"]=>
string(19) "description of opt3"
+ ["messages"]=>
+ array(0) {
+ }
}
-}
+}
\ No newline at end of file
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_17.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_17.phpt 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_17.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -19,7 +19,7 @@
?>
--EXPECT--
-object(PEAR2_Console_CommandLine_Result)#21 (4) {
+object(PEAR2_Console_CommandLine_Result)#19 (4) {
["options"]=>
array(11) {
["true"]=>
@@ -37,7 +37,12 @@
["callback"]=>
NULL
["array"]=>
- NULL
+ array(2) {
+ [0]=>
+ string(4) "spam"
+ [1]=>
+ string(3) "egg"
+ }
["password"]=>
NULL
["help"]=>
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_18.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_18.phpt 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_18.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -19,7 +19,7 @@
?>
--EXPECT--
-object(PEAR2_Console_CommandLine_Result)#23 (4) {
+object(PEAR2_Console_CommandLine_Result)#17 (4) {
["options"]=>
array(4) {
["verbose"]=>
@@ -37,7 +37,7 @@
["command_name"]=>
string(7) "install"
["command"]=>
- object(PEAR2_Console_CommandLine_Result)#25 (4) {
+ object(PEAR2_Console_CommandLine_Result)#20 (4) {
["options"]=>
array(2) {
["force"]=>
Added: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_23.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_23.phpt (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_23.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,21 @@
+--TEST--
+Test for PEAR2_Console_CommandLine::parse() method (invalid subcommand detection).
+--SKIPIF--
+
+--ARGS--
+-v invalid subcommand
+--FILE--
+parse();
+} catch (Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECT--
+Command "invalid" is not valid.
Added: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_24.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_24.phpt (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_24.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,21 @@
+--TEST--
+Test for PEAR2_Console_CommandLine::parse() method (invalid subcommand detection).
+--SKIPIF--
+
+--ARGS--
+upgrade
+--FILE--
+parse();
+} catch (Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECT--
+Package name is required.
Added: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_25.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_25.phpt (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_25.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,21 @@
+--TEST--
+Test for PEAR2_Console_CommandLine::parse() method (invalid subcommand detection).
+--SKIPIF--
+
+--ARGS--
+upgrade -s foo
+--FILE--
+parse();
+} catch (Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECT--
+Valid states are "stable" and "beta".
Added: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_26.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_26.phpt (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_26.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,21 @@
+--TEST--
+Test for PEAR2_Console_CommandLine::parse() method (invalid subcommand detection).
+--SKIPIF--
+
+--ARGS--
+upgrade -s
+--FILE--
+parse();
+} catch (Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECT--
+Option requires value.
Added: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_27.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_27.phpt (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_27.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,21 @@
+--TEST--
+Test for PEAR2_Console_CommandLine::parse() method (invalid subcommand detection).
+--SKIPIF--
+
+--ARGS--
+upgrade -t
+--FILE--
+parse();
+} catch (Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECT--
+Mysterious option encountered.
Added: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_28.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_28.phpt (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_28.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,21 @@
+--TEST--
+Test for PEAR2_Console_CommandLine::parse() method (invalid subcommand detection).
+--SKIPIF--
+
+--ARGS--
+upgrade --dry-run=foo
+--FILE--
+parse();
+} catch (Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECT--
+Option should not have a value.
Added: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_29.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_29.phpt (rev 0)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_29.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -0,0 +1,21 @@
+--TEST--
+Test for PEAR2_Console_CommandLine::parse() method (invalid subcommand detection).
+--SKIPIF--
+
+--ARGS--
+foo
+--FILE--
+parse();
+} catch (Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECT--
+Only "upgrade" is supported.
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_7.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_7.phpt 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_7.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -19,7 +19,7 @@
?>
--EXPECT--
-object(PEAR2_Console_CommandLine_Result)#21 (4) {
+object(PEAR2_Console_CommandLine_Result)#19 (4) {
["options"]=>
array(11) {
["true"]=>
@@ -37,7 +37,12 @@
["callback"]=>
NULL
["array"]=>
- NULL
+ array(2) {
+ [0]=>
+ string(4) "spam"
+ [1]=>
+ string(3) "egg"
+ }
["password"]=>
NULL
["help"]=>
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_8.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_8.phpt 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_8.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -20,7 +20,7 @@
?>
--EXPECT--
-object(PEAR2_Console_CommandLine_Result)#21 (4) {
+object(PEAR2_Console_CommandLine_Result)#19 (4) {
["options"]=>
array(11) {
["true"]=>
@@ -38,7 +38,12 @@
["callback"]=>
NULL
["array"]=>
- NULL
+ array(2) {
+ [0]=>
+ string(4) "spam"
+ [1]=>
+ string(3) "egg"
+ }
["password"]=>
NULL
["help"]=>
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_9.phpt
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_9.phpt 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/console_commandline_parse_9.phpt 2010-05-10 03:52:17 UTC (rev 299197)
@@ -34,7 +34,12 @@
["callback"]=>
NULL
["array"]=>
- NULL
+ array(2) {
+ [0]=>
+ string(4) "spam"
+ [1]=>
+ string(3) "egg"
+ }
["password"]=>
string(10) "secretpass"
["help"]=>
Modified: pear2/sandbox/Console_CommandLine/trunk/tests/tests.inc.php
===================================================================
--- pear2/sandbox/Console_CommandLine/trunk/tests/tests.inc.php 2010-05-10 03:48:09 UTC (rev 299196)
+++ pear2/sandbox/Console_CommandLine/trunk/tests/tests.inc.php 2010-05-10 03:52:17 UTC (rev 299197)
@@ -120,6 +120,7 @@
$parser->addOption('array', array(
'short_name' => '-a',
'long_name' => '--array',
+ 'default' => array('spam', 'egg'),
'action' => 'StoreArray',
'help_name' => 'ARRAY',
'description' => 'test the StoreArray action'
@@ -275,6 +276,64 @@
}
// }}}
+// {{{ buildParser4()
+
+/**
+ * Build a parser instance and return it.
+ *
+ * For testing custom messages.
+ *
+ * @return object Console_CommandLine instance
+ */
+function buildParser4()
+{
+ $parser = new PEAR2_Console_CommandLine(array(
+ 'messages' => array(
+ 'INVALID_SUBCOMMAND' => 'Only "upgrade" is supported.',
+ ),
+ ));
+ $parser->name = 'some_program';
+ $parser->version = '0.1.0';
+ $parser->description = 'Description of our parser goes here...';
+
+ // some subcommand
+ $cmd1 = $parser->addCommand('upgrade', array(
+ 'description' => 'upgrade given package',
+ 'aliases' => array('up'),
+ 'messages' => array(
+ 'ARGUMENT_REQUIRED' => 'Package name is required.',
+ 'OPTION_VALUE_REQUIRED' => 'Option requires value.',
+ 'OPTION_VALUE_UNEXPECTED' => 'Option should not have a value.',
+ 'OPTION_UNKNOWN' => 'Mysterious option encountered.',
+ ),
+ ));
+ // add option
+ $cmd1->addOption('state', array(
+ 'short_name' => '-s',
+ 'long_name' => '--state',
+ 'action' => 'StoreString',
+ 'choices' => array('stable', 'beta'),
+ 'description' => 'accepted package states',
+ 'messages' => array(
+ 'OPTION_VALUE_NOT_VALID' => 'Valid states are "stable" and "beta".',
+ ),
+ ));
+ // add another option
+ $cmd1->addOption('dry_run', array(
+ 'short_name' => '-d',
+ 'long_name' => '--dry-run',
+ 'action' => 'StoreTrue',
+ 'description' => 'dry run',
+ ));
+ // add argument
+ $cmd1->addArgument('package', array(
+ 'description' => 'package to upgrade'
+ ));
+
+ return $parser;
+}
+
+// }}}
// CustomRenderer() {{{
/**