Skip to content
Browse files

implemented subcommand aliases as requested by greg for pyrus

  • Loading branch information...
1 parent ba63247 commit c038298c7c48c384546ef5ad066b0562a36af35b @izimobil izimobil committed Jun 9, 2009
View
99 data/xmlschema.rng
@@ -17,48 +17,75 @@
</start>
<!-- Command node -->
+ <define name="ref_command_subcommand_common">
+ <interleave>
+ <optional>
+ <element name="name">
+ <text/>
+ </element>
+ </optional>
+ <optional>
+ <element name="description">
+ <text/>
+ </element>
+ </optional>
+ <optional>
+ <element name="version">
+ <text/>
+ </element>
+ </optional>
+ <optional>
+ <element name="add_help_option">
+ <ref name="ref_bool_choices"/>
+ </element>
+ </optional>
+ <optional>
+ <element name="add_version_option">
+ <ref name="ref_bool_choices"/>
+ </element>
+ </optional>
+ <optional>
+ <element name="force_posix">
+ <ref name="ref_bool_choices"/>
+ </element>
+ </optional>
+ <zeroOrMore>
+ <ref name="ref_option"/>
+ </zeroOrMore>
+ <zeroOrMore>
+ <ref name="ref_argument"/>
+ </zeroOrMore>
+ <zeroOrMore>
+ <ref name="ref_subcommand"/>
+ </zeroOrMore>
+ </interleave>
+ </define>
+
+ <!-- command element -->
+
<define name="ref_command">
<element name="command">
<interleave>
+ <ref name="ref_command_subcommand_common"/>
+ </interleave>
+ </element>
+ </define>
+
+ <!-- subcommand element -->
+
+ <define name="ref_subcommand">
+ <element name="command">
+ <interleave>
+ <ref name="ref_command_subcommand_common"/>
<optional>
- <element name="name">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="description">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="version">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="add_help_option">
- <ref name="ref_bool_choices"/>
- </element>
- </optional>
- <optional>
- <element name="add_version_option">
- <ref name="ref_bool_choices"/>
- </element>
- </optional>
- <optional>
- <element name="force_posix">
- <ref name="ref_bool_choices"/>
+ <element name="aliases">
+ <zeroOrMore>
+ <element name="alias">
+ <text/>
+ </element>
+ </zeroOrMore>
</element>
</optional>
- <zeroOrMore>
- <ref name="ref_option"/>
- </zeroOrMore>
- <zeroOrMore>
- <ref name="ref_argument"/>
- </zeroOrMore>
- <zeroOrMore>
- <ref name="ref_command"/>
- </zeroOrMore>
</interleave>
</element>
</define>
View
5 examples/ex3.php
@@ -58,9 +58,10 @@
'description' => 'the text to output'
));
-// add the bar subcommand
+// add the bar subcommand with a "baz" alias
$bar_cmd = $parser->addCommand('bar', array(
- 'description' => 'output the given string with a bar prefix'
+ 'description' => 'output the given string with a bar prefix',
+ 'aliases' => array('baz'),
));
$bar_cmd->addOption('reverse', array(
'short_name' => '-r',
View
28 src/Console/CommandLine.php
@@ -778,10 +778,9 @@ public function parse($userArgc=null, $userArgv=null)
while ($argc--) {
$token = array_shift($argv);
try {
- if (isset($this->commands[$token])) {
- $result->command_name = $token;
- $result->command = $this->commands[$token]->parse($argc,
- $argv);
+ if ($cmd = $this->_getSubCommand($token)) {
+ $result->command_name = $cmd->name;
+ $result->command = $cmd->parse($argc, $argv);
break;
} else {
$this->parseToken($token, $result, $args, $argc);
@@ -1096,4 +1095,25 @@ private function _dispatchAction($option, $token, $result)
}
}
// }}}
+ // _getSubCommand() {{{
+
+ /**
+ * Tries to return the subcommand that matches the given token or returns
+ * false if no subcommand was found.
+ *
+ * @param string $token Current command line token
+ *
+ * @return mixed An instance of Console_CommandLine_Command or false
+ */
+ private function _getSubCommand($token)
+ {
+ foreach ($this->commands as $cmd) {
+ if ($cmd->name == $token || in_array($token, $cmd->aliases)) {
+ return $cmd;
+ }
+ }
+ return false;
+ }
+
+ // }}}
}
View
28 src/Console/CommandLine/Command.php
@@ -40,4 +40,32 @@
*/
class PEAR2_Console_CommandLine_Command extends PEAR2_Console_CommandLine
{
+ // Public properties {{{
+
+ /**
+ * An array of aliases for the subcommand.
+ *
+ * @var array $aliases Aliases for the subcommand.
+ */
+ public $aliases = array();
+
+ // }}}
+ // __construct() {{{
+
+ /**
+ * Constructor.
+ *
+ * @param array $params An optional array of parameters
+ *
+ * @return void
+ */
+ public function __construct($params = array())
+ {
+ if (isset($params['aliases'])) {
+ $this->aliases = $params['aliases'];
+ }
+ parent::__construct($params);
+ }
+
+ // }}}
}
View
12 src/Console/CommandLine/Renderer/Default.php
@@ -320,7 +320,7 @@ protected function commandList()
$col = 0;
foreach ($this->parser->commands as $cmdname=>$command) {
$cmdname = ' ' . $cmdname;
- $commands[] = array($cmdname, $command->description);
+ $commands[] = array($cmdname, $command->description, $command->aliases);
$ln = strlen($cmdname);
if ($col < $ln) {
$col = $ln;
@@ -329,6 +329,16 @@ protected function commandList()
$ret = $this->parser->message_provider->get('COMMAND_WORD') . ":";
foreach ($commands as $command) {
$text = str_pad($command[0], $col) . ' ' . $command[1];
+ if ($aliasesCount = count($command[2])) {
+ $pad = '';
+ $text .= ' (';
+ $text .= $aliasesCount > 1 ? 'aliases: ' : 'alias: ';
+ foreach ($command[2] as $alias) {
+ $text .= $pad . $alias;
+ $pad = ', ';
+ }
+ $text .= ')';
+ }
$ret .= "\n" . $this->columnWrap($text, $col+2);
}
return $ret;
View
9 src/Console/CommandLine/XmlParser.php
@@ -151,6 +151,15 @@ private static function _parseCommandNode($node, $isRootNode = false)
case 'command':
$obj->addCommand(self::_parseCommandNode($cNode));
break;
+ case 'aliases':
+ if (!$isRootNode) {
+ foreach ($cNode->childNodes as $subChildNode) {
+ if ($subChildNode->nodeName == 'alias') {
+ $obj->aliases[] = trim($subChildNode->nodeValue);
+ }
+ }
+ }
+ break;
default:
break;
}
View
4 tests/console_commandline_fromxmlfile.phpt
@@ -29,5 +29,5 @@ Options:
--version show the program version and exit
Commands:
- zip zip given files in the destination file
- unzip unzip given file in the destination dir
+ zip zip given files in the destination file (aliases: compress, zp)
+ unzip unzip given file in the destination dir (alias: uzp)
View
4 tests/console_commandline_fromxmlstring.phpt
@@ -30,5 +30,5 @@ Options:
--version show the program version and exit
Commands:
- zip zip given files in the destination file
- unzip unzip given file in the destination dir
+ zip zip given files in the destination file (aliases: compress, zp)
+ unzip unzip given file in the destination dir (alias: uzp)
View
2 tests/console_commandline_parse_11.phpt
@@ -27,5 +27,5 @@ Options:
--version show the program version and exit
Commands:
- install install given package
+ install install given package (aliases: inst, instbis)
uninstall uninstall given package
View
2 tests/console_commandline_parse_12.phpt
@@ -3,7 +3,7 @@ Test for PEAR2_Console_CommandLine::parse() method (subcommand help 2).
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
-install --help 2>&1
+inst --help 2>&1
--FILE--
<?php
View
2 tests/console_commandline_parse_19.phpt
@@ -5,7 +5,7 @@ some_package
--SKIPIF--
<?php if(php_sapi_name()!='cli') echo 'skip'; ?>
--ARGS--
--v install -f -
+-v instbis -f -
--FILE--
<?php
View
7 tests/test.xml
@@ -33,6 +33,10 @@
<short_name>-v</short_name>
</option>
<command>
+ <aliases>
+ <alias>compress</alias>
+ <alias>zp</alias>
+ </aliases>
<name>zip</name>
<!-- Comment -->
<description>zip given files in the destination file</description>
@@ -50,6 +54,9 @@
<description>destination directory</description>
</argument>
<name>unzip</name>
+ <aliases>
+ <alias>uzp</alias>
+ </aliases>
<description>unzip given file in the destination dir</description>
<argument name="zipfile">
<description>path to the zip file to unzip</description>
View
3 tests/tests.inc.php
@@ -172,7 +172,8 @@ function buildParser2()
// install subcommand
$cmd1 = $parser->addCommand('install', array(
- 'description' => 'install given package'
+ 'description' => 'install given package',
+ 'aliases' => array('inst', 'instbis'),
));
$cmd1->addOption('force', array(
'short_name' => '-f',

0 comments on commit c038298

Please sign in to comment.
Something went wrong with that request. Please try again.