From faf94de0f3197761c9af4ca6a8dc5af366b91784 Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Wed, 25 Jul 2018 19:16:56 +0700 Subject: [PATCH 1/4] feat(command): support alias --- src/Input/Command.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Input/Command.php b/src/Input/Command.php index cb1f533..4efc5e4 100644 --- a/src/Input/Command.php +++ b/src/Input/Command.php @@ -34,9 +34,12 @@ class Command extends Parser /** @var string */ protected $_desc; - /** @var string */ + /** @var string Usage examples */ protected $_usage; + /** @var string Command alias */ + protected $_alias; + /** @var App The cli app this command is bound to */ protected $_app; @@ -244,6 +247,24 @@ public function usage(string $usage = null) return $this; } + /** + * Gets or sets alias. + * + * @param string|null $alias + * + * @return string|self + */ + public function alias(string $alias = null) + { + if (\func_num_args() === 0) { + return $this->_alias; + } + + $this->_alias = $alias; + + return $this; + } + /** * Sets event handler for last (or given) option. * @@ -313,7 +334,7 @@ public function showHelp() $helper ->showArgumentsHelp($this->allArguments()) - ->showOptionsHelp($this->allOptions(), '', 'Legend: [optional]'); + ->showOptionsHelp($this->allOptions(), '', 'Legend: [optional] variadic...'); if ($this->_usage) { $io->eol(); From 381f45e38e3d33e188d4fc2fb86fb2ed8968cebf Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Wed, 25 Jul 2018 19:17:40 +0700 Subject: [PATCH 2/4] feat(app): assign alias to command if available --- src/Application.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Application.php b/src/Application.php index e4ec81d..56f30df 100644 --- a/src/Application.php +++ b/src/Application.php @@ -159,6 +159,7 @@ public function add(Command $command, string $alias = '', bool $default = false) } if ($alias) { + $command->alias($alias); $this->aliases[$alias] = $name; } From a8c626b85cb742866dd71644e7fcc70aa87f288e Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Wed, 25 Jul 2018 19:19:59 +0700 Subject: [PATCH 3/4] feat(helper): improve help msg, add cmd alias show variad --- src/Helper/OutputHelper.php | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Helper/OutputHelper.php b/src/Helper/OutputHelper.php index 2d65b9a..2a6bfe4 100644 --- a/src/Helper/OutputHelper.php +++ b/src/Helper/OutputHelper.php @@ -35,7 +35,7 @@ public function __construct(Writer $writer = null) */ public function showArgumentsHelp(array $arguments, string $header = '', string $footer = ''): self { - $this->showHelp('Arguments', $arguments, 6, $header, $footer); + $this->showHelp('Arguments', $arguments, $header, $footer); return $this; } @@ -49,7 +49,7 @@ public function showArgumentsHelp(array $arguments, string $header = '', string */ public function showOptionsHelp(array $options, string $header = '', string $footer = ''): self { - $this->showHelp('Options', $options, 13, $header, $footer); + $this->showHelp('Options', $options, $header, $footer); return $this; } @@ -63,7 +63,7 @@ public function showOptionsHelp(array $options, string $header = '', string $foo */ public function showCommandsHelp(array $commands, string $header = '', string $footer = ''): self { - $this->showHelp('Commands', $commands, 4, $header, $footer); + $this->showHelp('Commands', $commands, $header, $footer); return $this; } @@ -73,13 +73,12 @@ public function showCommandsHelp(array $commands, string $header = '', string $f * * @param string $for * @param array $items - * @param int $space * @param string $header * @param string $footer * * @return void */ - protected function showHelp(string $for, array $items, int $space, string $header = '', string $footer = '') + protected function showHelp(string $for, array $items, string $header = '', string $footer = '') { if ($header) { $this->writer->bold($header, true); @@ -93,6 +92,7 @@ protected function showHelp(string $for, array $items, int $space, string $heade return; } + $space = 4; foreach ($this->sortItems($items, $padLen) as $item) { $name = $this->getName($item); $this->writer->bold(' ' . \str_pad($name, $padLen + $space)); @@ -120,7 +120,7 @@ protected function sortItems(array $items, &$max = 0): array \uasort($items, function ($a, $b) use (&$max) { /** @var Parameter $b */ /** @var Parameter $a */ - $max = \max(\strlen($a->name()), \strlen($b->name()), $max); + $max = \max(\strlen($this->getName($a)), \strlen($this->getName($b)), $max); return $a->name() <=> $b->name(); }); @@ -140,17 +140,33 @@ protected function getName($item): string $name = $item->name(); if ($item instanceof Command) { - return $name; + return \trim($item->alias() . '|' . $name, '|'); } + return $this->label($item); + } + + /** + * Get parameter label for humans. + * + * @param Parameter $item + * + * @return string + */ + protected function label(Parameter $item) + { + $name = $item->name(); + if ($item instanceof Option) { $name = $item->short() . '|' . $item->long(); } + $variad = $item->variadic() ? '...' : ''; + if ($item->required()) { - return "<$name>"; + return "<$name$variad>"; } - return "[$name]"; + return "[$name$variad]"; } } From 35dc6be6f9f3ceebf9ddf826537c9f12bcdaf49b Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Wed, 25 Jul 2018 19:20:16 +0700 Subject: [PATCH 4/4] test: fix command test --- tests/Helper/OutputHelperTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Helper/OutputHelperTest.php b/tests/Helper/OutputHelperTest.php index bc03642..eb53f74 100644 --- a/tests/Helper/OutputHelperTest.php +++ b/tests/Helper/OutputHelperTest.php @@ -27,7 +27,7 @@ public static function tearDownAfterClass() public function test_show_arguments() { $this->newHelper()->showArgumentsHelp([ - new Argument(''), + new Argument('', 'The path'), new Argument('[config:defaultConfig]'), ], 'Arg Header', 'Arg Footer'); @@ -36,7 +36,7 @@ public function test_show_arguments() '', 'Arguments:', ' [config] ', - ' ', + ' The path', '', 'Arg Footer', ], $this->output()); @@ -53,8 +53,8 @@ public function test_show_options() 'Opt Header', '', 'Options:', - ' <-n|--full-name> Full name', - ' [-h|--help] Show help', + ' <-n|--full-name> Full name', + ' [-h|--help] Show help', '', 'Opt Footer', ], $this->output());