Skip to content

Commit fd81308

Browse files
committed
some bug fixed for formet message output. display command alias in help info.
1 parent ec208f6 commit fd81308

File tree

15 files changed

+527
-307
lines changed

15 files changed

+527
-307
lines changed

examples/Controllers/HomeController.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ class HomeController extends Controller
2222
/**
2323
* @return array
2424
*/
25-
protected static function commandMap()
25+
protected static function commandAliases()
2626
{
2727
return [
28+
// now, 'home:i' is equals to 'home:index'
2829
'i' => 'index',
2930
'prg' => 'progress',
31+
'l' => 'list',
32+
'h' => 'helpPanel',
33+
'hp' => 'helpPanel',
3034
];
3135
}
3236

@@ -39,7 +43,7 @@ protected static function commandMap()
3943
* arg2 argument description 2
4044
* @options
4145
* -s, --long option description 1
42-
* --opt option description 2
46+
* --opt option description 2
4347
* @example example text one
4448
* the second line example
4549
*/
@@ -60,7 +64,7 @@ public function colorCommand()
6064
return 0;
6165
}
6266

63-
$this->write('color text output:');
67+
$this->write('color style text output:');
6468
$styles = $this->output->getStyle()->getStyleNames();
6569

6670
foreach ($styles as $style) {
@@ -218,7 +222,7 @@ public function helpPanelCommand()
218222
/**
219223
* output format message: aList
220224
*/
221-
public function aListCommand()
225+
public function listCommand()
222226
{
223227
$list = [
224228
'The is a list line 0',
@@ -521,8 +525,8 @@ public function downCommand()
521525
return 0;
522526
}
523527

524-
$d = Download::down($url, $saveAs, $type);
525-
528+
Download::down($url, $saveAs, $type);
529+
// $d = Download::down($url, $saveAs, $type);
526530
// echo Helper::dumpVars($d);
527531

528532
return 0;

src/Base/AbstractApplication.php

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Inhere\Console\Traits\SimpleEventTrait;
1515
use Inhere\Console\Style\Style;
1616
use Inhere\Console\Utils\FormatUtil;
17+
use Inhere\Console\Utils\Helper;
1718

1819
/**
1920
* Class AbstractApplication
@@ -25,16 +26,21 @@ abstract class AbstractApplication implements ApplicationInterface
2526

2627
/**
2728
* @var array
29+
* [
30+
* 0 => logo text,
31+
* 1 => color style, // 'info'
32+
* ]
2833
*/
34+
private static $logoInfo;
35+
36+
/** @var array */
2937
protected static $internalCommands = [
3038
'version' => 'Show application version information',
3139
'help' => 'Show application help information',
3240
'list' => 'List all group and independent commands',
3341
];
3442

35-
/**
36-
* @var array
37-
*/
43+
/** @var array */
3844
protected static $internalOptions = [
3945
'--debug' => 'Setting the application runtime debug level',
4046
'--profile' => 'Display timing and memory usage information',
@@ -44,7 +50,7 @@ abstract class AbstractApplication implements ApplicationInterface
4450
];
4551

4652
/**
47-
* app meta config
53+
* application meta info
4854
* @var array
4955
*/
5056
private $meta = [
@@ -121,7 +127,8 @@ protected function prepareRun()
121127
}
122128

123129
protected function beforeRun()
124-
{}
130+
{
131+
}
125132

126133
/**
127134
* run app
@@ -398,7 +405,10 @@ public function showCommandList($quit = true)
398405
foreach ($controllers as $name => $controller) {
399406
$hasGroup = true;
400407
/** @var AbstractCommand $controller */
401-
$controllerArr[$name] = $controller::getDescription() ?: $desPlaceholder;
408+
$desc = $controller::getDescription() ?: $desPlaceholder;
409+
$aliases = $this->getCommandAliases($name);
410+
$extra = $aliases ? Helper::wrapTag(' [alias: ' . implode(',', $aliases) . ']', 'info') : '';
411+
$controllerArr[$name] = $desc . $extra;
402412
}
403413

404414
if (!$hasGroup) {
@@ -417,15 +427,17 @@ public function showCommandList($quit = true)
417427
/** @var AbstractCommand $command */
418428
if (is_subclass_of($command, CommandInterface::class)) {
419429
$desc = $command::getDescription() ?: $desPlaceholder;
420-
} else if ($msg = $this->getCommandMessage($name)) {
430+
} elseif ($msg = $this->getCommandMessage($name)) {
421431
$desc = $msg;
422-
} else if (\is_string($command)) {
432+
} elseif (\is_string($command)) {
423433
$desc = 'A handler : ' . $command;
424-
} else if (\is_object($command)) {
434+
} elseif (\is_object($command)) {
425435
$desc = 'A handler by ' . \get_class($command);
426436
}
427437

428-
$commandArr[$name] = $desc;
438+
$aliases = $this->getCommandAliases($name);
439+
$extra = $aliases ? Helper::wrapTag(' [alias: ' . implode(',', $aliases) . ']', 'info') : '';
440+
$commandArr[$name] = $desc . $extra;
429441
}
430442

431443
if (!$hasCommand) {
@@ -435,7 +447,6 @@ public function showCommandList($quit = true)
435447
// built in commands
436448
$internalCommands = static::$internalCommands;
437449
ksort($internalCommands);
438-
// array_unshift($internalCommands, "\n- <cyan>Internal Commands</cyan>");
439450

440451
// built in options
441452
$internalOptions = FormatUtil::commandOptions(self::$internalOptions);
@@ -445,6 +456,8 @@ public function showCommandList($quit = true)
445456
'Options:' => $internalOptions,
446457
'Internal Commands:' => $internalCommands,
447458
'Available Commands:' => array_merge($controllerArr, $commandArr),
459+
], [
460+
'sepChar' => ' ',
448461
]);
449462

450463
unset($controllerArr, $commandArr, $internalCommands);
@@ -590,6 +603,54 @@ public function isCommand($name)
590603
return isset($this->commands[$name]);
591604
}
592605

606+
/**
607+
* @return string|null
608+
*/
609+
public static function getLogoTxt()
610+
{
611+
return self::$logoInfo[0] ?? null;
612+
}
613+
614+
/**
615+
* @param string $logoTxt
616+
*/
617+
public static function setLogoTxt(string $logoTxt)
618+
{
619+
self::$logoInfo[0] = $logoTxt;
620+
}
621+
622+
/**
623+
* @return string|null
624+
*/
625+
public static function getLogoStyle()
626+
{
627+
return self::$logoInfo[1] ?? 'info';
628+
}
629+
630+
/**
631+
* @param string $style
632+
*/
633+
public static function setLogoStyle(string $style)
634+
{
635+
self::$logoInfo[1] = $style;
636+
}
637+
638+
/**
639+
* @return array
640+
*/
641+
public static function getLogoInfo(): array
642+
{
643+
return self::$logoInfo;
644+
}
645+
646+
/**
647+
* @param array $logoInfo
648+
*/
649+
public static function setLogoInfo(array $logoInfo)
650+
{
651+
self::$logoInfo = $logoInfo;
652+
}
653+
593654
/**
594655
* @return array
595656
*/

src/Base/AbstractCommand.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ abstract class AbstractCommand implements BaseCommandInterface
5757
/** @var Application */
5858
protected $app;
5959

60-
/** @var InputDefinition|null */
60+
/** @var InputDefinition|null */
6161
private $definition;
6262

6363
/** @var string */
@@ -190,7 +190,7 @@ protected function showHelp()
190190
$spt = $this->input->getScript();
191191

192192
$info = $def->getSynopsis();
193-
$info['usage'] = "$spt $cmd ". $info['usage'];
193+
$info['usage'] = "$spt $cmd " . $info['usage'];
194194

195195
$this->output->mList($info);
196196

@@ -289,7 +289,8 @@ public function validateInput()
289289
}
290290

291291
if (\count($missingOpts) > 0) {
292-
$this->output->liteError(sprintf('Not enough options parameters (missing: "%s").', implode(', ', $missingOpts)));
292+
$this->output->liteError(sprintf('Not enough options parameters (missing: "%s").',
293+
implode(', ', $missingOpts)));
293294

294295
return false;
295296
}
@@ -348,9 +349,12 @@ protected function showHelpByMethodAnnotation($method, $action = null, array $al
348349
}
349350

350351
$doc = $ref->getMethod($method)->getDocComment();
351-
$tags = Annotation::tagList($this->handleAnnotationVars($doc));
352+
$tags = Annotation::getTags($this->handleAnnotationVars($doc));
353+
$comments = [];
352354

353-
$this->output->startBuffer();
355+
if ($aliases) {
356+
$comments[] = sprintf("<comment>Alias Name:</comment> %s\n", implode(',', $aliases));
357+
}
354358

355359
foreach ($tags as $tag => $msg) {
356360
if (!$msg || !\is_string($msg)) {
@@ -371,11 +375,11 @@ protected function showHelpByMethodAnnotation($method, $action = null, array $al
371375
// }
372376

373377
$tag = ucfirst($tag);
374-
$this->write("<comment>$tag:</comment>\n $msg\n");
378+
$comments[] = "<comment>$tag:</comment>\n $msg\n";
375379
}
376380
}
377381

378-
$this->output->flush();
382+
$this->output->write(implode("\n", $comments), false);
379383

380384
return 0;
381385
}

src/Components/AryBuffer.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-24
6+
* Time: 9:17
7+
*/
8+
9+
namespace Inhere\Console\Components;
10+
11+
/**
12+
* Class AryBuffer
13+
* @package Inhere\Console\Components
14+
*/
15+
final class AryBuffer
16+
{
17+
/**
18+
* @var string[]
19+
*/
20+
private $body = [];
21+
22+
/** @var string */
23+
private $delimiter = ''; // '/' ':'
24+
25+
/**
26+
* constructor.
27+
* @param string $content
28+
*/
29+
public function __construct($content = '')
30+
{
31+
if ($content) {
32+
$this->body[] = $content;
33+
}
34+
}
35+
36+
/**
37+
* @param string $content
38+
*/
39+
public function write(string $content)
40+
{
41+
$this->body[] = $content;
42+
}
43+
44+
/**
45+
* @param string $content
46+
*/
47+
public function append(string $content)
48+
{
49+
$this->write($content);
50+
}
51+
52+
/**
53+
* @param string $content
54+
*/
55+
public function prepend(string $content)
56+
{
57+
array_unshift($this->body, $content);
58+
}
59+
60+
/**
61+
* clear
62+
*/
63+
public function clear()
64+
{
65+
$this->body = [];
66+
}
67+
68+
/**
69+
* @return string[]
70+
*/
71+
public function getBody(): array
72+
{
73+
return $this->body;
74+
}
75+
76+
/**
77+
* @param string[] $body
78+
*/
79+
public function setBody(array $body)
80+
{
81+
$this->body = $body;
82+
}
83+
84+
/**
85+
* @return string
86+
*/
87+
public function toString()
88+
{
89+
return implode($this->delimiter, $this->body);
90+
}
91+
92+
/**
93+
* @return string
94+
*/
95+
public function __toString()
96+
{
97+
return $this->toString();
98+
}
99+
100+
/**
101+
* @return string
102+
*/
103+
public function getDelimiter(): string
104+
{
105+
return $this->delimiter;
106+
}
107+
108+
/**
109+
* @param string $delimiter
110+
*/
111+
public function setDelimiter(string $delimiter)
112+
{
113+
$this->delimiter = $delimiter;
114+
}
115+
}

0 commit comments

Comments
 (0)