Skip to content

Commit aafc8c7

Browse files
authored
Merge pull request #27 from adhocore/develop
Develop
2 parents ea055ae + 35dc6be commit aafc8c7

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

src/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public function add(Command $command, string $alias = '', bool $default = false)
159159
}
160160

161161
if ($alias) {
162+
$command->alias($alias);
162163
$this->aliases[$alias] = $name;
163164
}
164165

src/Helper/OutputHelper.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(Writer $writer = null)
3535
*/
3636
public function showArgumentsHelp(array $arguments, string $header = '', string $footer = ''): self
3737
{
38-
$this->showHelp('Arguments', $arguments, 6, $header, $footer);
38+
$this->showHelp('Arguments', $arguments, $header, $footer);
3939

4040
return $this;
4141
}
@@ -49,7 +49,7 @@ public function showArgumentsHelp(array $arguments, string $header = '', string
4949
*/
5050
public function showOptionsHelp(array $options, string $header = '', string $footer = ''): self
5151
{
52-
$this->showHelp('Options', $options, 13, $header, $footer);
52+
$this->showHelp('Options', $options, $header, $footer);
5353

5454
return $this;
5555
}
@@ -63,7 +63,7 @@ public function showOptionsHelp(array $options, string $header = '', string $foo
6363
*/
6464
public function showCommandsHelp(array $commands, string $header = '', string $footer = ''): self
6565
{
66-
$this->showHelp('Commands', $commands, 4, $header, $footer);
66+
$this->showHelp('Commands', $commands, $header, $footer);
6767

6868
return $this;
6969
}
@@ -73,13 +73,12 @@ public function showCommandsHelp(array $commands, string $header = '', string $f
7373
*
7474
* @param string $for
7575
* @param array $items
76-
* @param int $space
7776
* @param string $header
7877
* @param string $footer
7978
*
8079
* @return void
8180
*/
82-
protected function showHelp(string $for, array $items, int $space, string $header = '', string $footer = '')
81+
protected function showHelp(string $for, array $items, string $header = '', string $footer = '')
8382
{
8483
if ($header) {
8584
$this->writer->bold($header, true);
@@ -93,6 +92,7 @@ protected function showHelp(string $for, array $items, int $space, string $heade
9392
return;
9493
}
9594

95+
$space = 4;
9696
foreach ($this->sortItems($items, $padLen) as $item) {
9797
$name = $this->getName($item);
9898
$this->writer->bold(' ' . \str_pad($name, $padLen + $space));
@@ -120,7 +120,7 @@ protected function sortItems(array $items, &$max = 0): array
120120
\uasort($items, function ($a, $b) use (&$max) {
121121
/** @var Parameter $b */
122122
/** @var Parameter $a */
123-
$max = \max(\strlen($a->name()), \strlen($b->name()), $max);
123+
$max = \max(\strlen($this->getName($a)), \strlen($this->getName($b)), $max);
124124

125125
return $a->name() <=> $b->name();
126126
});
@@ -140,17 +140,33 @@ protected function getName($item): string
140140
$name = $item->name();
141141

142142
if ($item instanceof Command) {
143-
return $name;
143+
return \trim($item->alias() . '|' . $name, '|');
144144
}
145145

146+
return $this->label($item);
147+
}
148+
149+
/**
150+
* Get parameter label for humans.
151+
*
152+
* @param Parameter $item
153+
*
154+
* @return string
155+
*/
156+
protected function label(Parameter $item)
157+
{
158+
$name = $item->name();
159+
146160
if ($item instanceof Option) {
147161
$name = $item->short() . '|' . $item->long();
148162
}
149163

164+
$variad = $item->variadic() ? '...' : '';
165+
150166
if ($item->required()) {
151-
return "<$name>";
167+
return "<$name$variad>";
152168
}
153169

154-
return "[$name]";
170+
return "[$name$variad]";
155171
}
156172
}

src/Input/Command.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ class Command extends Parser
3434
/** @var string */
3535
protected $_desc;
3636

37-
/** @var string */
37+
/** @var string Usage examples */
3838
protected $_usage;
3939

40+
/** @var string Command alias */
41+
protected $_alias;
42+
4043
/** @var App The cli app this command is bound to */
4144
protected $_app;
4245

@@ -244,6 +247,24 @@ public function usage(string $usage = null)
244247
return $this;
245248
}
246249

250+
/**
251+
* Gets or sets alias.
252+
*
253+
* @param string|null $alias
254+
*
255+
* @return string|self
256+
*/
257+
public function alias(string $alias = null)
258+
{
259+
if (\func_num_args() === 0) {
260+
return $this->_alias;
261+
}
262+
263+
$this->_alias = $alias;
264+
265+
return $this;
266+
}
267+
247268
/**
248269
* Sets event handler for last (or given) option.
249270
*
@@ -313,7 +334,7 @@ public function showHelp()
313334

314335
$helper
315336
->showArgumentsHelp($this->allArguments())
316-
->showOptionsHelp($this->allOptions(), '', 'Legend: <required> [optional]');
337+
->showOptionsHelp($this->allOptions(), '', 'Legend: <required> [optional] variadic...');
317338

318339
if ($this->_usage) {
319340
$io->eol();

tests/Helper/OutputHelperTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function tearDownAfterClass()
2727
public function test_show_arguments()
2828
{
2929
$this->newHelper()->showArgumentsHelp([
30-
new Argument('<path>'),
30+
new Argument('<path>', 'The path'),
3131
new Argument('[config:defaultConfig]'),
3232
], 'Arg Header', 'Arg Footer');
3333

@@ -36,7 +36,7 @@ public function test_show_arguments()
3636
'',
3737
'Arguments:',
3838
' [config] ',
39-
' <path> ',
39+
' <path> The path',
4040
'',
4141
'Arg Footer',
4242
], $this->output());
@@ -53,8 +53,8 @@ public function test_show_options()
5353
'Opt Header',
5454
'',
5555
'Options:',
56-
' <-n|--full-name> Full name',
57-
' [-h|--help] Show help',
56+
' <-n|--full-name> Full name',
57+
' [-h|--help] Show help',
5858
'',
5959
'Opt Footer',
6060
], $this->output());

0 commit comments

Comments
 (0)