Skip to content

Commit

Permalink
feat: support render attached subcommand help
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 26, 2021
1 parent d1f9829 commit 43e0495
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
7 changes: 5 additions & 2 deletions examples/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Inhere\Console\Examples\Command;

use Inhere\Console\Command;
use Inhere\Console\Handler\CommandWrapper;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;

Expand All @@ -26,9 +27,11 @@ class TestCommand extends Command
protected function subCommands(): array
{
return [
'sub' => static function ($fs, $out): void {
'sub' => CommandWrapper::new(static function ($fs, $out): void {
$out->println('hello, this is an sub command of test.');
},
}, [
'desc' => 'sub command of test command'
]),
];
}

Expand Down
11 changes: 9 additions & 2 deletions src/Decorate/CommandHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\FlagUtil;
use function implode;
use function ksort;
use function sprintf;
use function strtr;
use function ucfirst;
Expand Down Expand Up @@ -81,7 +82,7 @@ protected function addCommentsVars(array $map): void
*/
protected function setCommentsVar(string $name, array|string $value): void
{
$this->commentsVars[$name] = is_array($value) ? implode(',', $value) : (string)$value;
$this->commentsVars[$name] = is_array($value) ? implode(',', $value) : $value;
}

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

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

if ($subCmds = $this->getSubsForHelp()) {
// sort commands
ksort($subCmds);
$help['Commands:'] = $subCmds;
}

$help['Example:'] = $fs->getExampleHelp();
$help['More Help:'] = $fs->getMoreHelp();

// no group options. only set key position.
Expand Down
4 changes: 4 additions & 0 deletions src/Decorate/ControllerHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public function showCommandList(): void
$commands[$cmd] = $desc;
}

if ($subCmds = $this->getSubsForHelp()) {
$commands = array_merge($commands, $subCmds);
}

// sort commands
ksort($commands);

Expand Down
40 changes: 35 additions & 5 deletions src/Decorate/SubCommandsWareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ protected function createSubCommand(array $subInfo): Command
* Register a app independent console command
*
* @param string|class-string $name
* @param string|Closure|CommandInterface|null $handler
* @param class-string|CommandInterface|null $handler
* @param array $config
*/
public function addSub(string $name, string|Closure|CommandInterface $handler = null, array $config = []): void
public function addSub(string $name, string|CommandInterface $handler = null, array $config = []): void
{
if (!$handler && class_exists($name)) {
/** @var Command $name name is an command class */
Expand Down Expand Up @@ -171,9 +171,9 @@ public function addSub(string $name, string|Closure|CommandInterface $handler =
if ($aliases = $handler::aliases()) {
$config['aliases'] = array_merge($config['aliases'], $aliases);
}
} elseif (!is_object($handler) || !ConsoleUtil::isValidCmdObject($handler)) {
} elseif (!is_object($handler) || !$handler instanceof Command) {
Helper::throwInvalidArgument(
'The command handler must is an subclass of %s OR a Closure OR a sub-object of %s',
'The subcommand handler must be an subclass of %s OR a sub-object of %s',
Command::class,
Command::class,
);
Expand Down Expand Up @@ -317,8 +317,38 @@ public function setBlocked(array $blocked): void
/**
* @return array
*/
public function getCommandNames(): array
public function getSubNames(): array
{
return array_keys($this->commands);
}

/**
* @return array
*/
public function getCommands(): array
{
return $this->commands;
}

/**
* @return array
*/
public function getSubsForHelp(): array
{
$subs = [];
foreach ($this->commands as $name => $subInfo) {
$sub = $subInfo['handler'];
if ($sub instanceof Command) {
$subs[$name] = $sub->getRealDesc();
} elseif (is_string($sub)) {
$subs[$name] = $sub::getDesc();
} else {
$subConf = $subInfo['config'];

$subs[$name] = $subConf['desc'] ?? 'no description';
}
}

return $subs;
}
}

0 comments on commit 43e0495

Please sign in to comment.