Skip to content

Commit

Permalink
Merge pull request #27 from adhocore/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
adhocore authored Jul 25, 2018
2 parents ea055ae + 35dc6be commit aafc8c7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function add(Command $command, string $alias = '', bool $default = false)
}

if ($alias) {
$command->alias($alias);
$this->aliases[$alias] = $name;
}

Expand Down
34 changes: 25 additions & 9 deletions src/Helper/OutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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();
});
Expand All @@ -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]";
}
}
25 changes: 23 additions & 2 deletions src/Input/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -313,7 +334,7 @@ public function showHelp()

$helper
->showArgumentsHelp($this->allArguments())
->showOptionsHelp($this->allOptions(), '', 'Legend: <required> [optional]');
->showOptionsHelp($this->allOptions(), '', 'Legend: <required> [optional] variadic...');

if ($this->_usage) {
$io->eol();
Expand Down
8 changes: 4 additions & 4 deletions tests/Helper/OutputHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function tearDownAfterClass()
public function test_show_arguments()
{
$this->newHelper()->showArgumentsHelp([
new Argument('<path>'),
new Argument('<path>', 'The path'),
new Argument('[config:defaultConfig]'),
], 'Arg Header', 'Arg Footer');

Expand All @@ -36,7 +36,7 @@ public function test_show_arguments()
'',
'Arguments:',
' [config] ',
' <path> ',
' <path> The path',
'',
'Arg Footer',
], $this->output());
Expand All @@ -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());
Expand Down

0 comments on commit aafc8c7

Please sign in to comment.