Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

backported changes and bug fixes from PEAR1 Console_CommandLine

  • Loading branch information...
commit 2c8406c7dab0ea65380b5a07fba29e86f8e5331a 1 parent 18ea84b
@izimobil izimobil authored
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
7 package.xml
@@ -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
150 src/Console/CommandLine.php
@@ -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() {{{
/**
View
2  src/Console/CommandLine/Action/Callback.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the Callback action.
*
View
2  src/Console/CommandLine/Action/Counter.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the Version action.
*
View
2  src/Console/CommandLine/Action/Help.php
@@ -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.
View
2  src/Console/CommandLine/Action/Password.php
@@ -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
View
2  src/Console/CommandLine/Action/StoreArray.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreArray action.
*
View
2  src/Console/CommandLine/Action/StoreFalse.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreFalse action.
*
View
2  src/Console/CommandLine/Action/StoreFloat.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreFloat action.
*
View
2  src/Console/CommandLine/Action/StoreInt.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreInt action.
*
View
2  src/Console/CommandLine/Action/StoreString.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreString action.
*
View
2  src/Console/CommandLine/Action/StoreTrue.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Action.php';
-
/**
* Class that represent the StoreTrue action.
*
View
2  src/Console/CommandLine/Action/Version.php
@@ -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.
View
2  src/Console/CommandLine/Argument.php
@@ -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.
*
View
2  src/Console/CommandLine/Command.php
@@ -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.
*
View
2  src/Console/CommandLine/Exception.php
@@ -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.
*
View
15 src/Console/CommandLine/MessageProvider/Default.php
@@ -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.',
View
3  src/Console/CommandLine/Option.php
@@ -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.
*
View
13 src/Console/CommandLine/Outputter/Default.php
@@ -21,7 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Outputter.php';
/**
* PEAR2_Console_CommandLine default Outputter.
@@ -49,7 +48,11 @@ class PEAR2_Console_CommandLine_Outputter_Default implements PEAR2_Console_Comma
*/
public function stdout($msg)
{
- fwrite(STDOUT, $msg);
+ if (defined('STDOUT')) {
+ fwrite(STDOUT, $msg);
+ } else {
+ echo $msg;
+ }
}
// }}}
@@ -65,7 +68,11 @@ public function stdout($msg)
*/
public function stderr($msg)
{
- fwrite(STDERR, $msg);
+ if (defined('STDERR')) {
+ fwrite(STDERR, $msg);
+ } else {
+ echo $msg;
+ }
}
// }}}
View
2  src/Console/CommandLine/Renderer/Default.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine/Renderer.php';
-
/**
* PEAR2_Console_CommandLine default renderer.
*
View
2  src/Console/CommandLine/XmlParser.php
@@ -21,8 +21,6 @@
* @since File available since release 0.1.0
*/
-// require_once 'Console/CommandLine.php';
-
/**
* Parser for command line xml definitions.
*
View
6 tests/console_commandline_parse_13.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (user errors 1).
+Test for Console_CommandLine::parse() method (user errors 1).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,7 +7,7 @@ Test for PEAR2_Console_CommandLine::parse() method (user errors 1).
--FILE--
<?php
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
$parser = buildParser1();
try {
@@ -18,4 +18,4 @@ try {
?>
--EXPECT--
-option "float" require a value of type float (got "foo").
+Option "float" requires a value of type float (got "foo").
View
6 tests/console_commandline_parse_14.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (user errors 2).
+Test for Console_CommandLine::parse() method (user errors 2).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,7 +7,7 @@ Test for PEAR2_Console_CommandLine::parse() method (user errors 2).
--FILE--
<?php
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
$parser = buildParser1();
try {
@@ -18,4 +18,4 @@ try {
?>
--EXPECT--
-option "int" require a value of type int (got "foo").
+Option "int" requires a value of type int (got "foo").
View
6 tests/console_commandline_parse_15.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (subcommand error).
+Test for Console_CommandLine::parse() method (subcommand error).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,7 +7,7 @@ install -f 2>&1
--FILE--
<?php
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
$parser = buildParser2();
try {
@@ -18,6 +18,6 @@ try {
?>
--EXPECT--
-Error: you must provide at least 1 argument.
+Error: You must provide at least 1 argument.
Type "some_program -h" to get help.
Type "some_program <command> -h" to get help on specific command.
View
6 tests/console_commandline_parse_16.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (user errors 3).
+Test for Console_CommandLine::parse() method (user errors 3).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,7 +7,7 @@ Test for PEAR2_Console_CommandLine::parse() method (user errors 3).
--FILE--
<?php
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
$parser = buildParser1();
try {
@@ -18,4 +18,4 @@ try {
?>
--EXPECT--
-option "string" must be one of the following: "foo", "bar", "baz" (got "fooz").
+Option "string" must be one of the following: "foo", "bar", "baz" (got "fooz").
View
62 tests/console_commandline_parse_17.phpt
@@ -0,0 +1,62 @@
+--TEST--
+Test for Console_CommandLine::parse() method (user argc/argv 1).
+--SKIPIF--
+<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+$argv = array('somename', '-t', '-f', '--float=1.2', 'foo', 'bar');
+$argc = count($argv);
+try {
+ $parser = buildParser1();
+ $result = $parser->parse($argc, $argv);
+ var_dump($result);
+} catch (Console_CommandLine_Exception $exc) {
+ $parser->displayError($exc->getMessage());
+}
+
+?>
+--EXPECT--
+object(PEAR2_Console_CommandLine_Result)#21 (4) {
+ ["options"]=>
+ array(11) {
+ ["true"]=>
+ bool(true)
+ ["false"]=>
+ bool(false)
+ ["int"]=>
+ int(1)
+ ["float"]=>
+ float(1.2)
+ ["string"]=>
+ NULL
+ ["counter"]=>
+ NULL
+ ["callback"]=>
+ NULL
+ ["array"]=>
+ NULL
+ ["password"]=>
+ NULL
+ ["help"]=>
+ NULL
+ ["version"]=>
+ NULL
+ }
+ ["args"]=>
+ array(2) {
+ ["simple"]=>
+ string(3) "foo"
+ ["multiple"]=>
+ array(1) {
+ [0]=>
+ string(3) "bar"
+ }
+ }
+ ["command_name"]=>
+ bool(false)
+ ["command"]=>
+ bool(false)
+}
View
58 tests/console_commandline_parse_18.phpt
@@ -0,0 +1,58 @@
+--TEST--
+Test for Console_CommandLine::parse() method (user argc/argv 2).
+--SKIPIF--
+<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+$argv = array('somename', '-v', 'install', '-f', 'foo');
+$argc = count($argv);
+try {
+ $parser = buildParser2();
+ $result = $parser->parse($argc, $argv);
+ var_dump($result);
+} catch (PEAR2_Console_CommandLine_Exception $exc) {
+ $parser->displayError($exc->getMessage());
+}
+
+?>
+--EXPECT--
+object(PEAR2_Console_CommandLine_Result)#23 (4) {
+ ["options"]=>
+ array(4) {
+ ["verbose"]=>
+ bool(true)
+ ["logfile"]=>
+ NULL
+ ["help"]=>
+ NULL
+ ["version"]=>
+ NULL
+ }
+ ["args"]=>
+ array(0) {
+ }
+ ["command_name"]=>
+ string(7) "install"
+ ["command"]=>
+ object(PEAR2_Console_CommandLine_Result)#25 (4) {
+ ["options"]=>
+ array(2) {
+ ["force"]=>
+ bool(true)
+ ["help"]=>
+ NULL
+ }
+ ["args"]=>
+ array(1) {
+ ["package"]=>
+ string(3) "foo"
+ }
+ ["command_name"]=>
+ bool(false)
+ ["command"]=>
+ bool(false)
+ }
+}
View
6 tests/console_commandline_parse_2.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (--help).
+Test for Console_CommandLine::parse() method (--help).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,7 +7,7 @@ Test for PEAR2_Console_CommandLine::parse() method (--help).
--FILE--
<?php
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
$parser = buildParser1();
$parser->parse();
@@ -30,7 +30,7 @@ Options:
-a ARRAY, --array=ARRAY test the StoreArray action
-p password, --password=password test the Password action
-h, --help show this help message and exit
- --version show the program version and exit
+ -v, --version show the program version and exit
Arguments:
simple test a simple argument
View
7 tests/console_commandline_parse_4.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (errors 1).
+Test for Console_CommandLine::parse() method (errors 1).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,8 +7,7 @@ Test for PEAR2_Console_CommandLine::parse() method (errors 1).
--FILE--
<?php
-require_once 'Console/CommandLine.php' ;
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
try {
$parser = buildParser1();
@@ -19,5 +18,5 @@ try {
?>
--EXPECT--
-Error: unknown option "-d".
+Error: Unknown option "-d".
Type "some_program -h" to get help.
View
7 tests/console_commandline_parse_5.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (errors 2).
+Test for Console_CommandLine::parse() method (errors 2).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,8 +7,7 @@ Test for PEAR2_Console_CommandLine::parse() method (errors 2).
--FILE--
<?php
-require_once 'Console/CommandLine.php' ;
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
try {
$parser = buildParser1();
@@ -19,5 +18,5 @@ try {
?>
--EXPECT--
-Error: option "float" require a value.
+Error: Option "float" requires a value.
Type "some_program -h" to get help.
View
7 tests/console_commandline_parse_6.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (errors 3).
+Test for Console_CommandLine::parse() method (errors 3).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -7,8 +7,7 @@ Test for PEAR2_Console_CommandLine::parse() method (errors 3).
--FILE--
<?php
-require_once 'Console/CommandLine.php' ;
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
try {
$parser = buildParser1();
@@ -19,5 +18,5 @@ try {
?>
--EXPECT--
-Error: you must provide at least 2 arguments.
+Error: You must provide at least 2 arguments.
Type "some_program -h" to get help.
View
1  tests/console_commandline_parse_8.phpt
@@ -7,7 +7,6 @@ Test for PEAR2_Console_CommandLine::parse() method (special cases 2).
--FILE--
<?php
-require_once 'Console/CommandLine.php' ;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
try {
View
6 tests/console_commandline_parse_9.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test for PEAR2_Console_CommandLine::parse() method (password option).
+Test for Console_CommandLine::parse() method (password option).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
@@ -9,9 +9,7 @@ secretpass
--FILE--
<?php
-require_once 'Console/CommandLine.php' ;
-require_once 'Console/CommandLine/Argument.php' ;
-require_once __DIR__ . DIRECTORY_SEPARATOR . 'tests.inc.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
$parser = buildParser1();
$result = $parser->parse();
View
15 tests/console_commandline_webrequest_1.phpt
@@ -0,0 +1,15 @@
+--TEST--
+Test for Console_CommandLine::parse() with a web request 1
+--GET--
+version=1
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+$parser = buildParser1();
+$parser->parse();
+
+?>
+--EXPECT--
+some_program version 0.1.0.
View
35 tests/console_commandline_webrequest_2.phpt
@@ -0,0 +1,35 @@
+--TEST--
+Test for Console_CommandLine::parse() with a web request 2
+--GET--
+help
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+$parser = buildParser1();
+$parser->parse();
+
+?>
+--EXPECT--
+Description of our parser goes here...
+
+Usage:
+ some_program [options] <simple> <multiple...>
+
+Options:
+ -t, --true test the StoreTrue action
+ -f, --false test the StoreFalse action
+ --int=INT test the StoreInt action
+ --float=FLOAT test the StoreFloat action
+ -s STRING, --string=STRING test the StoreString action
+ -c, --counter test the Counter action
+ --callback=callback test the Callback action
+ -a ARRAY, --array=ARRAY test the StoreArray action
+ -p password, --password=password test the Password action
+ -h, --help show this help message and exit
+ -v, --version show the program version and exit
+
+Arguments:
+ simple test a simple argument
+ multiple test a multiple argument
View
56 tests/console_commandline_webrequest_3.phpt
@@ -0,0 +1,56 @@
+--TEST--
+Test for Console_CommandLine::parse() with a web request 3
+--POST--
+true=1&false=1&string=foo&int=3&float=4.0&callback=somestring&-a[]=foo&-a[]=bar&-a[]=baz&simple=foo&multiple=bar
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+$parser = buildParser1();
+$result = $parser->parse();
+var_dump($result->options);
+var_dump($result->args);
+
+?>
+--EXPECT--
+array(11) {
+ ["true"]=>
+ bool(true)
+ ["false"]=>
+ bool(false)
+ ["int"]=>
+ int(3)
+ ["float"]=>
+ float(4)
+ ["string"]=>
+ string(3) "foo"
+ ["counter"]=>
+ NULL
+ ["callback"]=>
+ string(20) "foo__fbzrfgevat__bar"
+ ["array"]=>
+ array(3) {
+ [0]=>
+ string(3) "foo"
+ [1]=>
+ string(3) "bar"
+ [2]=>
+ string(3) "baz"
+ }
+ ["password"]=>
+ NULL
+ ["help"]=>
+ NULL
+ ["version"]=>
+ NULL
+}
+array(2) {
+ ["simple"]=>
+ string(3) "foo"
+ ["multiple"]=>
+ array(1) {
+ [0]=>
+ string(3) "bar"
+ }
+}
View
3  tests/tests.inc.php
@@ -20,10 +20,11 @@
// ensure that errors will be printed
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors',true);
+set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__). '/../src/');
// uncomment this when package won't be in the SandBox anymore
// $basedir = __DIR__ . '/../..';
-$basedir = __DIR__ . '/../../..';
+$basedir = dirname(__FILE__) . '/../../..';
require_once $basedir . '/autoload.php';
Please sign in to comment.
Something went wrong with that request. Please try again.