Skip to content

Commit 045e0dd

Browse files
committed
up: update group command help render
1 parent ee2d8a1 commit 045e0dd

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

examples/Controller/HomeController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class HomeController extends Controller
3636

3737
protected static string $description = 'This is a demo command controller. there are some command usage examples(2)';
3838

39+
public static function aliases(): array
40+
{
41+
return ['h'];
42+
}
43+
3944
/**
4045
* @return array
4146
*/

examples/alone

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ define('BASE_PATH', dirname(__DIR__));
1313

1414
require dirname(__DIR__) . '/test/bootstrap.php';
1515

16+
// run: php examples/alone
1617
$input = new Input();
1718
$ctrl = new HomeController($input, new Output());
18-
$ctrl->setDetached();
1919

2020
try {
21-
$ctrl->run([$input->getCommand()]);
21+
$ctrl->setDetached();
22+
$ctrl->run($input->getFlags());
2223
} catch (Throwable $e) {
2324
$message = Color::apply('error', $e->getMessage());
2425

src/Concern/ControllerHelpTrait.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
use Inhere\Console\Console;
1313
use Inhere\Console\GlobalOption;
14-
use Inhere\Console\Util\FormatUtil;
1514
use ReflectionClass;
1615
use Toolkit\Cli\Color\ColorTag;
16+
use Toolkit\PFlag\FlagUtil;
1717
use Toolkit\Stdlib\Str;
1818
use Toolkit\Stdlib\Util\PhpDoc;
1919
use function array_merge;
@@ -96,7 +96,7 @@ public function showCommandList(): void
9696
$this->beforeShowCommandList();
9797

9898
$ref = new ReflectionClass($this);
99-
$sName = lcfirst(self::getName() ?: $ref->getShortName());
99+
$sName = self::getName() ?: lcfirst($ref->getShortName());
100100

101101
if (!($classDes = self::getDesc())) {
102102
$classDes = PhpDoc::description($ref->getDocComment()) ?: 'No description for the command group';
@@ -124,6 +124,7 @@ public function showCommandList(): void
124124
$desc = $defaultDes;
125125
}
126126

127+
$desc = ucfirst($desc);
127128
if ($this->isDisabled($cmd)) {
128129
if (!$showDisabled) {
129130
continue;
@@ -151,7 +152,6 @@ public function showCommandList(): void
151152

152153
// if is alone running.
153154
if ($detached = $this->isDetached()) {
154-
// $name = $sName . ' ';
155155
$usage = "$script <info>COMMAND</info> [--options ...] [arguments ...]";
156156
} else {
157157
$name = $sName . $this->delimiter;
@@ -162,24 +162,25 @@ public function showCommandList(): void
162162
];
163163
}
164164

165-
$globalOptions = [];
166-
if ($app = $this->app) {
167-
$globalOptions = $app->getFlags()->getOptsHelpLines();
168-
}
165+
// $globalOptions = [];
166+
// if ($app = $this->app) {
167+
// $globalOptions = $app->getFlags()->getOptsHelpLines();
168+
// }
169169

170170
$this->output->startBuffer();
171171
$this->output->write(ucfirst($classDes) . PHP_EOL);
172172

173+
$alias = '';
173174
if ($aliases = $this->getAliases()) {
174-
$this->output->writef("<comment>Alias:</comment> %s\n", implode(',', $aliases));
175+
$alias = ' (alias: <info>' . implode(',', $aliases) . '</info>)';
175176
}
177+
$this->output->writef("<comment>Name :</comment> %s%s\n", $sName, $alias);
176178

177179
$groupOptions = $this->flags->getOptsHelpLines();
178180
$this->output->mList([
179181
'Usage:' => $usage,
180-
//'Group Name:' => "<info>$sName</info>",
181-
'Group Options:' => FormatUtil::alignOptions($groupOptions),
182-
'Global Options:' => FormatUtil::alignOptions($globalOptions),
182+
'Group Options:' => FlagUtil::alignOptions($groupOptions),
183+
// 'Global Options:' => FlagUtil::alignOptions($globalOptions),
183184
'Available Commands:' => $commands,
184185
], [
185186
'sepChar' => ' ',

src/Controller.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use ReflectionObject;
2525
use RuntimeException;
2626
use Throwable;
27-
use Toolkit\Cli\Helper\FlagHelper;
2827
use Toolkit\PFlag\FlagsParser;
2928
use Toolkit\PFlag\FlagUtil;
3029
use Toolkit\PFlag\SFlags;

src/Handler/AbstractHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ protected function afterInitFlagsParser(FlagsParser $fs): void
290290
/**
291291
* @param array $args
292292
*
293-
* @return bool|int|mixed
293+
* @return mixed
294294
* @throws Throwable
295295
*/
296296
public function run(array $args): mixed
@@ -593,9 +593,10 @@ public function getCommandId(bool $useReal = true): string
593593
*/
594594
public function getAliases(): array
595595
{
596-
$aliases = [];
597596
if ($this->app) {
598597
$aliases = $this->app->getAliases(self::getName());
598+
} else {
599+
$aliases = static::aliases();
599600
}
600601

601602
return $aliases;

src/Util/FormatUtil.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static function wrapText(string $text, int $indent = 0, int $width = 0):
141141
* @param array $options
142142
*
143143
* @return array
144-
* @deprecated Please use {@see FlagUtil::alignOptions()}
144+
* @deprecated Please use FlagUtil::alignOptions {@see FlagUtil::alignOptions()}
145145
*/
146146
public static function alignOptions(array $options): array
147147
{

0 commit comments

Comments
 (0)