Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented feature request #18582 (add possibility to require a subc…

…ommand)

# sync with a commit from izi on the PEAR1 version
  • Loading branch information...
commit 4e23338489e6fb11ce1ad50d5995f55ec0a641e7 1 parent 19ab337
@helgi helgi authored
View
30 src/Console/CommandLine.php
@@ -123,6 +123,13 @@ class CommandLine
public $add_version_option = true;
/**
+ * Boolean that determine if providing a subcommand is mandatory.
+ *
+ * @var bool $subcommand_required Whether a subcommand is required or not
+ */
+ public $subcommand_required = false;
+
+ /**
* The command line parser renderer instance.
*
* @var object that implements PEAR2\Console\CommandLine\Renderer interface
@@ -302,6 +309,9 @@ public function __construct(array $params = array())
if (isset($params['add_help_option'])) {
$this->add_help_option = $params['add_help_option'];
}
+ if (isset($params['subcommand_required'])) {
+ $this->subcommand_required = $params['subcommand_required'];
+ }
if (isset($params['force_posix'])) {
$this->force_posix = $params['force_posix'];
} else if (getenv('POSIXLY_CORRECT')) {
@@ -882,6 +892,19 @@ public function parse($userArgc=null, $userArgv=null)
$this->messages
);
}
+ // if subcommand_required is set to true we must check that we have a
+ // subcommand.
+ if ( count($this->commands)
+ && $this->subcommand_required
+ && !$result->command_name
+ ) {
+ throw Console_CommandLine_Exception::factory(
+ 'SUBCOMMAND_REQUIRED',
+ array('commands' => implode(array_keys($this->commands), ', ')),
+ $this,
+ $this->messages
+ );
+ }
// minimum argument number check
$argnum = 0;
foreach ($this->args as $name=>$arg) {
@@ -961,9 +984,10 @@ 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' &&
- !empty($result->options[$lastopt->name]) &&
- count($this->args) > ($argc + count($args))) {
+ if ($lastopt->action == 'StoreArray'
+ && !empty($result->options[$lastopt->name])
+ && count($this->args) > ($argc + count($args))
+ ) {
if (!is_null($token)) {
$args[] = $token;
}
View
11 src/Console/CommandLine/MessageProvider/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
@@ -24,15 +24,15 @@
/**
- * Lightweight class that manages messages used by PEAR2\Console\CommandLine package,
- * allowing the developper to customize these messages, for example to
+ * Lightweight class that manages messages used by PEAR2\Console\CommandLine package,
+ * allowing the developper to customize these messages, for example to
* internationalize a command line frontend.
*
* @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 Release: @package_version@
* @link http://pear.php.net/package/Console_CommandLine
* @since Class available since release 0.1.0
@@ -70,6 +70,7 @@ class MessageProvider_Default
'LIST_OPTION_MESSAGE' => 'lists valid choices for option {$name}',
'LIST_DISPLAYED_MESSAGE' => 'Valid choices are: ',
'INVALID_SUBCOMMAND' => 'Command "{$command}" is not valid.',
+ 'SUBCOMMAND_REQUIRED' => 'Please enter one of the following command: {$commands}.',
);
// }}}
View
21 tests/console_commandline_addcommand_3.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Test for Console_CommandLine::addCommand() method.
+--ARGS--
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+$parser = new PEAR2\Console\CommandLine(array('subcommand_required' => true));
+$parser->addCommand('cmd1');
+$parser->addCommand('cmd2');
+$parser->addCommand('cmd3');
+try {
+ $parser->parse();
+} catch (PEAR2\Console\CommandLine\Exception $exc) {
+ echo $exc->getMessage();
+}
+
+?>
+--EXPECTF--
+Please enter one of the following command: cmd1, cmd2, cmd3.
Please sign in to comment.
Something went wrong with that request. Please try again.