Permalink
Browse files

backported changes and bug fixes from PEAR1 Console_CommandLine

  • Loading branch information...
1 parent 18ea84b commit 2c8406c7dab0ea65380b5a07fba29e86f8e5331a @izimobil izimobil committed Aug 22, 2008
Showing with 390 additions and 117 deletions.
  1. +6 −1 package.xml
  2. +113 −37 src/Console/CommandLine.php
  3. +0 −2 src/Console/CommandLine/Action/Callback.php
  4. +0 −2 src/Console/CommandLine/Action/Counter.php
  5. +0 −2 src/Console/CommandLine/Action/Help.php
  6. +0 −2 src/Console/CommandLine/Action/Password.php
  7. +0 −2 src/Console/CommandLine/Action/StoreArray.php
  8. +0 −2 src/Console/CommandLine/Action/StoreFalse.php
  9. +0 −2 src/Console/CommandLine/Action/StoreFloat.php
  10. +0 −2 src/Console/CommandLine/Action/StoreInt.php
  11. +0 −2 src/Console/CommandLine/Action/StoreString.php
  12. +0 −2 src/Console/CommandLine/Action/StoreTrue.php
  13. +0 −2 src/Console/CommandLine/Action/Version.php
  14. +0 −2 src/Console/CommandLine/Argument.php
  15. +0 −2 src/Console/CommandLine/Command.php
  16. +0 −2 src/Console/CommandLine/Exception.php
  17. +7 −8 src/Console/CommandLine/MessageProvider/Default.php
  18. +0 −3 src/Console/CommandLine/Option.php
  19. +10 −3 src/Console/CommandLine/Outputter/Default.php
  20. +0 −2 src/Console/CommandLine/Renderer/Default.php
  21. +0 −2 src/Console/CommandLine/XmlParser.php
  22. +3 −3 tests/console_commandline_parse_13.phpt
  23. +3 −3 tests/console_commandline_parse_14.phpt
  24. +3 −3 tests/console_commandline_parse_15.phpt
  25. +3 −3 tests/console_commandline_parse_16.phpt
  26. +62 −0 tests/console_commandline_parse_17.phpt
  27. +58 −0 tests/console_commandline_parse_18.phpt
  28. +3 −3 tests/console_commandline_parse_2.phpt
  29. +3 −4 tests/console_commandline_parse_4.phpt
  30. +3 −4 tests/console_commandline_parse_5.phpt
  31. +3 −4 tests/console_commandline_parse_6.phpt
  32. +0 −1 tests/console_commandline_parse_8.phpt
  33. +2 −4 tests/console_commandline_parse_9.phpt
  34. +15 −0 tests/console_commandline_webrequest_1.phpt
  35. +35 −0 tests/console_commandline_webrequest_2.phpt
  36. +56 −0 tests/console_commandline_webrequest_3.phpt
  37. +2 −1 tests/tests.inc.php
View
@@ -117,6 +117,11 @@ Main features:
<file role="test" name="console_commandline_parse_14.phpt"/>
<file role="test" name="console_commandline_parse_15.phpt"/>
<file role="test" name="console_commandline_parse_16.phpt"/>
+ <file role="test" name="console_commandline_parse_17.phpt"/>
+ <file role="test" name="console_commandline_parse_18.phpt"/>
+ <file role="test" name="console_commandline_webrequest_1.phpt"/>
+ <file role="test" name="console_commandline_webrequest_2.phpt"/>
+ <file role="test" name="console_commandline_webrequest_3.phpt"/>
<file role="test" name="test.xml"/>
<file role="test" name="tests.inc.php"/>
</dir>
@@ -125,7 +130,7 @@ Main features:
<dependencies>
<required>
<php>
- <min>5.2.0</min>
+ <min>5.3.0</min>
</php>
<pearinstaller>
<min>2.0.0a1</min>
View
@@ -251,8 +251,12 @@ public function __construct(array $params=array())
{
if (isset($params['name'])) {
$this->name = $params['name'];
- } else {
+ } else if (isset($argv) && count($argv) > 0) {
+ $this->name = $argv[0];
+ } else if (isset($_SERVER['argv']) && count($_SERVER['argv']) > 0) {
$this->name = $_SERVER['argv'][0];
+ } else if (isset($_SERVER['SCRIPT_NAME'])) {
+ $this->name = basename($_SERVER['SCRIPT_NAME']);
}
if (isset($params['description'])) {
$this->description = $params['description'];
@@ -320,7 +324,6 @@ public function accept($instance)
*
* Example:
* <code>
- * require_once 'Console/CommandLine.php';
* $parser = PEAR2_Console_CommandLine::fromXmlFile('path/to/file.xml');
* $result = $parser->parse();
* </code>
@@ -344,7 +347,6 @@ public static function fromXmlFile($file)
*
* Example:
* <code>
- * require_once 'Console/CommandLine.php';
* $xmldata = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
* <command>
* <description>Compress files</description>
@@ -663,9 +665,6 @@ public function findOption($str)
* // and in the result we will have:
* // $result->options['range']: array(1, 5)
*
- * require_once 'Console/CommandLine.php';
- * require_once 'Console/CommandLine/Action.php';
- *
* class ActionRange extends PEAR2_Console_CommandLine_Action
* {
* public function execute($value=false, $params=array())
@@ -754,52 +753,39 @@ public static function triggerError($msgId, $level, $params=array())
* @access public
* @throws Exception on user errors
*/
- public function parse($userArgc=null, $userArgv=null, $beginAt=0)
+ public function parse($userArgc=null, $userArgv=null)
{
- // add "auto" options help and version if needed
- if ($this->add_help_option) {
- $this->addOption('help', array(
- 'short_name' => '-h',
- 'long_name' => '--help',
- 'description' => 'show this help message and exit',
- 'action' => 'Help'
- ));
+ $this->addBuiltinOptions();
+ if ($userArgc !== null && $userArgv !== null) {
+ $argc = $userArgc;
+ $argv = $userArgv;
+ } else {
+ list($argc, $argv) = $this->getArgcArgv();
}
- if ($this->add_version_option && !empty($this->version)) {
- $this->addOption('version', array(
- 'long_name' => '--version',
- 'description' => 'show the program version and exit',
- 'action' => 'Version'
- ));
+ // build an empty result
+ include_once 'Console/CommandLine/Result.php';
+ $result = new PEAR2_Console_CommandLine_Result();
+ if (!$argc || empty($argv)) {
+ return $result;
}
- $argc = ($userArgc === null) ?
- (isset($argc) ? $argc : $_SERVER['argc']) : $userArgc;
- $argv = ($userArgv === null) ?
- (isset($argv) ? $argv : $_SERVER['argv']) : $userArgv;
- // case of a subcommand, skip main program args
- for ($i=0; $i<$beginAt; $i++) {
- $argc--;
+ if (!($this instanceof PEAR2_Console_CommandLine_Command)) {
+ // remove script name if we're not in a subcommand
array_shift($argv);
+ $argc--;
}
- // remove script name
- array_shift($argv);
- $argc--;
// will contain aruments
$args = array();
- // build an empty result
- $result = new PEAR2_Console_CommandLine_Result();
foreach ($this->options as $name=>$option) {
$result->options[$name] = $option->default;
}
// parse command line tokens
- $i = 0;
- while (++$i && $argc--) {
+ while ($argc--) {
$token = array_shift($argv);
try {
if (isset($this->commands[$token])) {
$result->command_name = $token;
- $result->command = $this->commands[$token]->parse(null,
- null, $i);
+ $result->command = $this->commands[$token]->parse($argc,
+ $argv);
break;
} else {
$this->parseToken($token, $result, $args, $argc);
@@ -1002,6 +988,96 @@ protected function parseToken($token, $result, &$args, $argc)
}
// }}}
+ // addBuiltinOptions() {{{
+
+ /**
+ * Add the builtin "Help" and "Version" options if needed.
+ *
+ * @return void
+ * @access protected
+ */
+ public function addBuiltinOptions()
+ {
+ if ($this->add_help_option) {
+ $helpOptionParams = array(
+ 'long_name' => '--help',
+ 'description' => 'show this help message and exit',
+ 'action' => 'Help'
+ );
+ if (!$this->findOption('-h')) {
+ // short name is available, take it
+ $helpOptionParams['short_name'] = '-h';
+ }
+ $this->addOption('help', $helpOptionParams);
+ }
+ if ($this->add_version_option && !empty($this->version)) {
+ $versionOptionParams = array(
+ 'long_name' => '--version',
+ 'description' => 'show the program version and exit',
+ 'action' => 'Version'
+ );
+ if (!$this->findOption('-v')) {
+ // short name is available, take it
+ $versionOptionParams['short_name'] = '-v';
+ }
+ $this->addOption('version', $versionOptionParams);
+ }
+ }
+
+ // }}}
+ // getArgcArgv() {{{
+
+ /**
+ * Try to return an array containing argc and argv, or trigger an error
+ * if it fails to get them.
+ *
+ * @return array
+ * @access protected
+ * @throws Exception
+ */
+ protected function getArgcArgv()
+ {
+ if (php_sapi_name() != 'cli') {
+ // we have a web request
+ $argv = array($this->name);
+ if (isset($_REQUEST)) {
+ foreach ($_REQUEST as $key => $value) {
+ if (!is_array($value)) {
+ $value = array($value);
+ }
+ $opt = $this->findOption($key);
+ if ($opt instanceof PEAR2_Console_CommandLine_Option) {
+ // match a configured option
+ $argv[] = $opt->short_name ?
+ $opt->short_name : $opt->long_name;
+ foreach($value as $v) {
+ if ($opt->expectsArgument()) {
+ $argv[] = isset($_GET[$key]) ? urldecode($v) : $v;
+ } else if ($v == '0' || $v == 'false') {
+ array_pop($argv);
+ }
+ }
+ } else if (isset($this->args[$key])) {
+ // match a configured argument
+ foreach($value as $v) {
+ $argv[] = isset($_GET[$key]) ? urldecode($v) : $v;
+ }
+ }
+ }
+ }
+ return array(count($argv), $argv);
+ }
+ if (isset($argc) && isset($argv)) {
+ // case of register_argv_argc = 1
+ return array($argc, $argv);
+ }
+ if (isset($_SERVER['argc']) && isset($_SERVER['argv'])) {
+ return array($_SERVER['argc'], $_SERVER['argv']);
+ }
+ return array(0, array());
+ }
+
+ // }}}
// _dispatchAction() {{{
/**
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the Callback action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the Version action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the Help action, a special action that displays the
* help message, telling the user how to use the program.
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the Password action, a special action that allow the
* user to specify the password on the commandline or to be prompted for
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreArray action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreFalse action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreFloat action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreInt action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreString action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreTrue action.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the Version action, a special action that displays the
* version string of the program.
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Element.php';
-
/**
* Class that represent a command line argument.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine.php';
-
/**
* Class that represent a command with option and arguments.
*
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'PEAR/Exception.php';
-
/**
* Class for exceptions raised by the PEAR2_Console_CommandLine package.
*
@@ -21,7 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/MessageProvider.php';
/**
* Lightweight class that manages messages used by PEAR2_Console_CommandLine package,
@@ -49,13 +48,13 @@ class PEAR2_Console_CommandLine_MessageProvider_Default implements PEAR2_Console
* @access protected
*/
protected $messages = array(
- 'OPTION_VALUE_REQUIRED' => 'option "{$name}" require a value.',
- 'OPTION_VALUE_UNEXPECTED' => 'option "{$name}" does not expects a value (got "{$value}").',
- 'OPTION_VALUE_NOT_VALID' => 'option "{$name}" must be one of the following: "{$choices}" (got "{$value}").',
- 'OPTION_VALUE_TYPE_ERROR' => 'option "{$name}" require a value of type {$type} (got "{$value}").',
- 'OPTION_AMBIGUOUS' => 'ambiguous option "{$name}", can be one of the following: {$matches}.',
- 'OPTION_UNKNOWN' => 'unknown option "{$name}".',
- 'ARGUMENT_REQUIRED' => 'you must provide at least {$argnum} argument{$plural}.',
+ 'OPTION_VALUE_REQUIRED' => 'Option "{$name}" requires a value.',
+ 'OPTION_VALUE_UNEXPECTED' => 'Option "{$name}" does not expect a value (got "{$value}").',
+ 'OPTION_VALUE_NOT_VALID' => 'Option "{$name}" must be one of the following: "{$choices}" (got "{$value}").',
+ 'OPTION_VALUE_TYPE_ERROR' => 'Option "{$name}" requires a value of type {$type} (got "{$value}").',
+ 'OPTION_AMBIGUOUS' => 'Ambiguous option "{$name}", can be one of the following: {$matches}.',
+ 'OPTION_UNKNOWN' => 'Unknown option "{$name}".',
+ 'ARGUMENT_REQUIRED' => 'You must provide at least {$argnum} argument{$plural}.',
'PROG_HELP_LINE' => 'Type "{$progname} -h" to get help.',
'PROG_VERSION_LINE' => '{$progname} version {$version}.',
'COMMAND_HELP_LINE' => 'Type "{$progname} <command> -h" to get help on specific command.',
@@ -21,9 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine.php';
-// require_once 'Console/CommandLine/Element.php';
-
/**
* Class that represent a commandline option.
*
Oops, something went wrong.

0 comments on commit 2c8406c

Please sign in to comment.