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; } 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]"; } } 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(); 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());