Skip to content

Commit

Permalink
Implemented feature request #18582 (add possibility to require a subc…
Browse files Browse the repository at this point in the history
…ommand)

# sync with a commit from izi on the PEAR1 version
  • Loading branch information
helgi committed Jul 23, 2011
1 parent 19ab337 commit 4e23338
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
30 changes: 27 additions & 3 deletions src/Console/CommandLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,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.
*
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Console/CommandLine/MessageProvider/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}.',
);

// }}}
Expand Down
21 changes: 21 additions & 0 deletions tests/console_commandline_addcommand_3.phpt
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 4e23338

Please sign in to comment.