Skip to content

Commit 46ab963

Browse files
committed
up: fix command aliaes not display on help
1 parent bd4f38a commit 46ab963

File tree

7 files changed

+106
-31
lines changed

7 files changed

+106
-31
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
[![License](https://img.shields.io/packagist/l/inhere/console.svg?style=flat-square)](LICENSE)
44
[![Php Version](https://img.shields.io/badge/php-%3E=7.3.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/console)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/console.svg)](https://packagist.org/packages/inhere/console)
6+
[![Github Actions Status](https://github.com/inhere/console/workflows/Unit-tests/badge.svg)](https://github.com/inhere/console/actions)
67

78
A simple, full-featured php command line application library.
89
Provide console parameter parsing, command run, color style output, user information interaction, and special format information display.
910

10-
> **[中文README](./README_cn.md)**
11+
> **[中文README](./README.zh-CN.md)**
1112
1213
## Command line preview
1314

README_cn.md renamed to README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![License](https://img.shields.io/packagist/l/inhere/console.svg?style=flat-square)](LICENSE)
44
[![Php Version](https://img.shields.io/badge/php-%3E=7.1.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/console)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/console.svg)](https://packagist.org/packages/inhere/console)
6+
[![Github Actions Status](https://github.com/inhere/console/workflows/Unit-tests/badge.svg)](https://github.com/inhere/console/actions)
67

78
简洁、功能全面的php命令行应用库。提供控制台参数解析, 命令运行,颜色风格输出, 用户信息交互, 特殊格式信息显示。
89

examples/Controller/HomeController.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
/**
2020
* default command controller. there are some command usage examples(1)
2121
* Class HomeController
22+
*
2223
* @package Inhere\Console\Examples\Controller
2324
*/
2425
class HomeController extends Controller
@@ -34,15 +35,15 @@ protected static function commandAliases(): array
3435
{
3536
return [
3637
// now, 'home:i' is equals to 'home:index'
37-
'i' => 'index',
38-
'prg' => 'progress',
39-
'pgb' => 'progressBar',
40-
'l' => 'list',
41-
'af' => 'artFont',
42-
'ml' => 'multiList',
43-
'sl' => 'splitLine',
44-
'dt' => 'dynamicText',
45-
'da' => 'defArg',
38+
'i' => 'index',
39+
'prg' => 'progress',
40+
'pgb' => 'progressBar',
41+
'l' => 'list',
42+
'af' => 'artFont',
43+
'ml' => 'multiList',
44+
'sl' => 'splitLine',
45+
'dt' => 'dynamicText',
46+
'defArg' => ['da', 'defarg'],
4647
];
4748
}
4849

@@ -108,6 +109,7 @@ public function disabledCommand(): void
108109

109110
/**
110111
* command `defArgCommand` config
112+
*
111113
* @throws LogicException
112114
*/
113115
protected function defArgConfigure(): void
@@ -127,6 +129,7 @@ public function defArgCommand(): void
127129

128130
/**
129131
* a command for test throw exception
132+
*
130133
* @throws RuntimeException
131134
*/
132135
public function exCommand(): void
@@ -181,6 +184,7 @@ public function colorCheckCommand(): void
181184

182185
/**
183186
* output art font text
187+
*
184188
* @options
185189
* --font Set the art font name(allow: {internalFonts}).
186190
* --italic Set the art font type is italic.
@@ -205,9 +209,10 @@ public function artFontCommand(): int
205209

206210
/**
207211
* dynamic notice message show: counterTxt. It is like progress txt, but no max value.
212+
*
213+
* @return int
208214
* @example
209215
* {script} {command}
210-
* @return int
211216
*/
212217
public function counterCommand(): int
213218
{
@@ -293,16 +298,19 @@ public function dynamicTextCommand(): void
293298

294299
/**
295300
* a progress bar example show, by Show::progressBar()
301+
*
296302
* @options
297303
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
298304
* --done-char the done show char. <info>=</info>
299305
* --wait-char the waiting show char. <info>-</info>
300306
* --sign-char the sign char show. <info>></info>
307+
*
308+
* @param Input $input
309+
*
310+
* @return int
301311
* @example
302312
* {script} {command}
303313
* {script} {command} --done-char '#' --wait-char ' '
304-
* @param Input $input
305-
* @return int
306314
*/
307315
public function progressCommand($input): int
308316
{
@@ -333,6 +341,7 @@ public function progressCommand($input): int
333341

334342
/**
335343
* a progress bar example show, by class ProgressBar
344+
*
336345
* @throws LogicException
337346
*/
338347
public function progressBarCommand(): void
@@ -474,6 +483,7 @@ public function paddingCommand(): void
474483

475484
/**
476485
* a example for use arguments on command
486+
*
477487
* @usage home:useArg [arg1=val1 arg2=arg2] [options]
478488
* @example
479489
* home:useArg status=2 name=john arg0 -s=test --page=23 -d -rf --debug --test=false -a v1 --ab -c -g --cd val -h '' -i stat=online
@@ -513,6 +523,7 @@ public function envCommand(): void
513523
/**
514524
* This is a demo for download a file to local
515525
* @usage {command} url=url saveTo=[saveAs] type=[bar|text]
526+
*
516527
* @example {command} url=https://github.com/inhere/php-console/archive/master.zip type=bar
517528
*/
518529
public function downCommand(): int

src/AbstractHandler.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,15 @@ public function isAlone(): bool
477477
*
478478
* @return bool
479479
*/
480-
protected function showHelp(): bool
481-
{
482-
if (!$definition = $this->getDefinition()) {
483-
return false;
484-
}
480+
abstract protected function showHelp(): bool;
485481

486-
$this->log(Console::VERB_DEBUG, 'display help information by definition');
482+
/**
483+
* @param InputDefinition $definition
484+
* @param array $aliases
485+
*/
486+
protected function showHelpByDefinition(InputDefinition $definition, array $aliases = []): void
487+
{
488+
$this->log(Console::VERB_DEBUG, 'display help information by input definition');
487489

488490
// if has InputDefinition object. (The comment of the command will not be parsed and used at this time.)
489491
$help = $definition->getSynopsis();
@@ -514,7 +516,8 @@ protected function showHelp(): bool
514516
// align global options
515517
$help['global options:'] = FormatUtil::alignOptions(Application::getGlobalOptions());
516518

517-
if (empty($help[0]) && $this->isAlone()) {
519+
$isAlone = $this->isAlone();
520+
if ($isAlone && empty($help[0])) {
518521
$help[0] = self::getDescription();
519522
}
520523

@@ -524,10 +527,14 @@ protected function showHelp(): bool
524527

525528
// output description
526529
$this->write(ucfirst($help[0]) . PHP_EOL);
530+
531+
if ($aliases) {
532+
$this->output->writef('<comment>Alias:</comment> %s', implode(',', $aliases));
533+
}
534+
527535
unset($help[0]);
528536

529537
$this->output->mList($help, ['sepChar' => ' ']);
530-
return true;
531538
}
532539

533540
/**
@@ -546,7 +553,8 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
546553
$name = $this->input->getCommand();
547554

548555
if (!$ref->hasMethod($method)) {
549-
$this->write("The command [<info>$name</info>] don't exist in the group: " . static::getName());
556+
$subCmd = $this->input->getSubCommand();
557+
$this->write("The command '<info>$subCmd</info>' dont exist in the group: " . static::getName());
550558
return 0;
551559
}
552560

@@ -635,6 +643,19 @@ protected function beforeRenderCommandHelp(array &$help): void
635643
* getter/setter methods
636644
**************************************************************************/
637645

646+
/**
647+
* @return array
648+
*/
649+
public function getAliases(): array
650+
{
651+
$aliases = [];
652+
if ($this->app) {
653+
$aliases = $this->app->getAliases(self::getName());
654+
}
655+
656+
return $aliases;
657+
}
658+
638659
/**
639660
* @param string $name
640661
*/

src/Command.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,18 @@ abstract class Command extends AbstractHandler implements CommandInterface
6767
*/
6868
protected function showHelp(): bool
6969
{
70-
// help info has been build by input definition.
71-
if (true === parent::showHelp()) {
70+
$aliases = $this->getAliases();
71+
72+
// render help by input definition.
73+
if ($definition = $this->getDefinition()) {
74+
$this->showHelpByDefinition($definition, $aliases);
7275
return true;
7376
}
7477

7578
$execMethod = 'execute';
76-
$cmdAliases = static::aliases();
7779

78-
$this->logf(Console::VERB_CRAZY, "display help info for the command: %s", static::$name);
80+
$this->logf(Console::VERB_CRAZY, "display help info for the command: %s", self::getName());
7981

80-
return $this->showHelpByMethodAnnotations($execMethod, '', $cmdAliases) !== 0;
82+
return $this->showHelpByMethodAnnotations($execMethod, '', $aliases) !== 0;
8183
}
8284
}

src/Controller.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ protected function onNotFound(string $action): bool
193193
}
194194

195195
/**
196-
* @param string $command
196+
* @param string $command command in the group
197197
*
198198
* @return int|mixed
199199
* @throws ReflectionException
@@ -221,6 +221,9 @@ public function run(string $command = '')
221221
return $this->showHelp();
222222
}
223223

224+
$this->input->setSubCommand($command);
225+
226+
// get real sub-command name
224227
$command = $this->getRealCommandName($command);
225228

226229
// convert 'boo-foo' to 'booFoo'
@@ -339,8 +342,15 @@ final public function execute($input, $output)
339342
*/
340343
protected function showHelp(): bool
341344
{
342-
// help info has been build by input definition.
343-
if (true === parent::showHelp()) {
345+
// render help by Definition
346+
if ($definition = $this->getDefinition()) {
347+
if ($action = $this->action) {
348+
$aliases = $this->getCommandAliases($action);
349+
} else {
350+
$aliases = $this->getAliases();
351+
}
352+
353+
$this->showHelpByDefinition($definition, $aliases);
344354
return true;
345355
}
346356

@@ -500,6 +510,11 @@ final public function showCommandList(): void
500510

501511
$this->output->startBuffer();
502512
$this->output->write(ucfirst($classDes) . PHP_EOL);
513+
514+
if ($aliases = $this->getAliases()) {
515+
$this->output->writef('<comment>Alias:</comment> %s', implode(',', $aliases));
516+
}
517+
503518
$this->output->mList([
504519
'Usage:' => $usage,
505520
//'Group Name:' => "<info>$sName</info>",

src/IO/AbstractInput.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,20 @@ abstract class AbstractInput implements InputInterface
4747

4848
/**
4949
* the command name(Is first argument)
50-
* e.g `start` OR `start`
50+
* e.g `git` OR `start`
5151
*
5252
* @var string
5353
*/
5454
protected $command = '';
5555

56+
/**
57+
* the command name(Is first argument)
58+
* e.g `subcmd` in the `./app group subcmd`
59+
*
60+
* @var string
61+
*/
62+
protected $subCommand = '';
63+
5664
/**
5765
* eg `./examples/app home:useArg status=2 name=john arg0 -s=test --page=23`
5866
*
@@ -227,4 +235,20 @@ public function setTokens(array $tokens): void
227235
{
228236
$this->tokens = $tokens;
229237
}
238+
239+
/**
240+
* @return string
241+
*/
242+
public function getSubCommand(): string
243+
{
244+
return $this->subCommand;
245+
}
246+
247+
/**
248+
* @param string $subCommand
249+
*/
250+
public function setSubCommand(string $subCommand): void
251+
{
252+
$this->subCommand = $subCommand;
253+
}
230254
}

0 commit comments

Comments
 (0)