Skip to content

Commit 43e0495

Browse files
committed
feat: support render attached subcommand help
1 parent d1f9829 commit 43e0495

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

examples/Command/TestCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Inhere\Console\Examples\Command;
1111

1212
use Inhere\Console\Command;
13+
use Inhere\Console\Handler\CommandWrapper;
1314
use Inhere\Console\IO\Input;
1415
use Inhere\Console\IO\Output;
1516

@@ -26,9 +27,11 @@ class TestCommand extends Command
2627
protected function subCommands(): array
2728
{
2829
return [
29-
'sub' => static function ($fs, $out): void {
30+
'sub' => CommandWrapper::new(static function ($fs, $out): void {
3031
$out->println('hello, this is an sub command of test.');
31-
},
32+
}, [
33+
'desc' => 'sub command of test command'
34+
]),
3235
];
3336
}
3437

src/Decorate/CommandHelpTrait.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Toolkit\PFlag\FlagsParser;
1515
use Toolkit\PFlag\FlagUtil;
1616
use function implode;
17+
use function ksort;
1718
use function sprintf;
1819
use function strtr;
1920
use function ucfirst;
@@ -81,7 +82,7 @@ protected function addCommentsVars(array $map): void
8182
*/
8283
protected function setCommentsVar(string $name, array|string $value): void
8384
{
84-
$this->commentsVars[$name] = is_array($value) ? implode(',', $value) : (string)$value;
85+
$this->commentsVars[$name] = is_array($value) ? implode(',', $value) : $value;
8586
}
8687

8788
/**
@@ -148,8 +149,14 @@ public function showHelpByFlagsParser(FlagsParser $fs, array $aliases = [], stri
148149

149150
$help['Options:'] = FlagUtil::alignOptions($fs->getOptsHelpLines());
150151
$help['Argument:'] = $fs->getArgsHelpLines();
151-
$help['Example:'] = $fs->getExampleHelp();
152152

153+
if ($subCmds = $this->getSubsForHelp()) {
154+
// sort commands
155+
ksort($subCmds);
156+
$help['Commands:'] = $subCmds;
157+
}
158+
159+
$help['Example:'] = $fs->getExampleHelp();
153160
$help['More Help:'] = $fs->getMoreHelp();
154161

155162
// no group options. only set key position.

src/Decorate/ControllerHelpTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public function showCommandList(): void
139139
$commands[$cmd] = $desc;
140140
}
141141

142+
if ($subCmds = $this->getSubsForHelp()) {
143+
$commands = array_merge($commands, $subCmds);
144+
}
145+
142146
// sort commands
143147
ksort($commands);
144148

src/Decorate/SubCommandsWareTrait.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ protected function createSubCommand(array $subInfo): Command
132132
* Register a app independent console command
133133
*
134134
* @param string|class-string $name
135-
* @param string|Closure|CommandInterface|null $handler
135+
* @param class-string|CommandInterface|null $handler
136136
* @param array $config
137137
*/
138-
public function addSub(string $name, string|Closure|CommandInterface $handler = null, array $config = []): void
138+
public function addSub(string $name, string|CommandInterface $handler = null, array $config = []): void
139139
{
140140
if (!$handler && class_exists($name)) {
141141
/** @var Command $name name is an command class */
@@ -171,9 +171,9 @@ public function addSub(string $name, string|Closure|CommandInterface $handler =
171171
if ($aliases = $handler::aliases()) {
172172
$config['aliases'] = array_merge($config['aliases'], $aliases);
173173
}
174-
} elseif (!is_object($handler) || !ConsoleUtil::isValidCmdObject($handler)) {
174+
} elseif (!is_object($handler) || !$handler instanceof Command) {
175175
Helper::throwInvalidArgument(
176-
'The command handler must is an subclass of %s OR a Closure OR a sub-object of %s',
176+
'The subcommand handler must be an subclass of %s OR a sub-object of %s',
177177
Command::class,
178178
Command::class,
179179
);
@@ -317,8 +317,38 @@ public function setBlocked(array $blocked): void
317317
/**
318318
* @return array
319319
*/
320-
public function getCommandNames(): array
320+
public function getSubNames(): array
321321
{
322322
return array_keys($this->commands);
323323
}
324+
325+
/**
326+
* @return array
327+
*/
328+
public function getCommands(): array
329+
{
330+
return $this->commands;
331+
}
332+
333+
/**
334+
* @return array
335+
*/
336+
public function getSubsForHelp(): array
337+
{
338+
$subs = [];
339+
foreach ($this->commands as $name => $subInfo) {
340+
$sub = $subInfo['handler'];
341+
if ($sub instanceof Command) {
342+
$subs[$name] = $sub->getRealDesc();
343+
} elseif (is_string($sub)) {
344+
$subs[$name] = $sub::getDesc();
345+
} else {
346+
$subConf = $subInfo['config'];
347+
348+
$subs[$name] = $subConf['desc'] ?? 'no description';
349+
}
350+
}
351+
352+
return $subs;
353+
}
324354
}

0 commit comments

Comments
 (0)