Skip to content

Commit

Permalink
up: update some logic for run sub commands
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 25, 2022
1 parent 5647598 commit 40b4cc8
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 80 deletions.
8 changes: 4 additions & 4 deletions src/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

use ErrorException;
use Inhere\Console\Component\ErrorHandler;
use Inhere\Console\Component\Router;
use Inhere\Console\Component\Formatter\Title;
use Inhere\Console\Decorate\SimpleEventAwareTrait;
use Inhere\Console\Component\Router;
use Inhere\Console\Contract\ApplicationInterface;
use Inhere\Console\Contract\ErrorHandlerInterface;
use Inhere\Console\Decorate\InputOutputAwareTrait;
use Inhere\Console\Decorate\ApplicationHelpTrait;
use Inhere\Console\Decorate\InputOutputAwareTrait;
use Inhere\Console\Decorate\SimpleEventAwareTrait;
use Inhere\Console\Decorate\StyledOutputAwareTrait;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
Expand Down Expand Up @@ -276,7 +276,7 @@ protected function beforeRun(): bool
$this->commandName = $command;
$this->flags->popFirstRawArg();
$this->input->setCommand($command);
$this->logf(Console::VERB_DEBUG, 'found the application command: %s', $command);
$this->logf(Console::VERB_DEBUG, 'app - match and found the command: %s', $command);

// like: help, version, list
if (!$this->handleGlobalCommand($command)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function dispatch(string $name, array $args = []): mixed
}

$cmdId = $name;
$this->debugf('begin dispatch the input command: %s, args: %s', $name, DataHelper::toString($args));
$this->debugf('app - begin dispatch the input command: %s, args: %s', $name, DataHelper::toString($args));

// format is: `group action`
if (strpos($name, ' ') > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function beforeInitFlagsParser(FlagsParser $fs): void
*/
protected function afterInitFlagsParser(FlagsParser $fs): void
{
$this->debugf('load flags configure for command: %s', $this->getRealCName());
$this->debugf('cmd: %s - load command flags configure', $this->getRealCName());
$this->configure();
$this->configFlags($fs);

Expand Down
37 changes: 0 additions & 37 deletions src/Concern/AbstractInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ abstract class AbstractInput implements InputInterface
*/
protected string $command = '';

/**
* the command name(Is first argument)
* e.g `subcommand` in the `./app group subcommand`
*
* @var string
*/
protected string $subCommand = '';

/**
* eg `./examples/app home:useArg status=2 name=john arg0 -s=test --page=23`
*
Expand Down Expand Up @@ -165,19 +157,6 @@ protected function collectInfo(array $rawFlags): void
$this->fullScript = implode(' ', $rawFlags);
}

/**
* @return string
*/
public function getCommandPath(): string
{
$path = $this->command;
if ($this->subCommand) {
$path .= ' ' . $this->subCommand;
}

return $path;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -335,22 +314,6 @@ public function setTokens(array $tokens): void
$this->collectInfo($tokens);
}

/**
* @return string
*/
public function getSubCommand(): string
{
return $this->subCommand;
}

/**
* @param string $subCommand
*/
public function setSubCommand(string $subCommand): void
{
$this->subCommand = $subCommand;
}

/**
* @return FlagsParser
*/
Expand Down
11 changes: 5 additions & 6 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@ public function doRun(array $args): mixed

// and not default command
if (!$command) {
$this->debugf('run args is empty, display help for the group: %s', $name);
$this->debugf('cmd: %s - run args is empty, display help for the group', $name);
return $this->showHelp();
}
} else {
$first = $args[0];
if (!FlagUtil::isValidName($first)) {
$this->debugf('not input subcommand, display help for the group: %s', $name);
$this->debugf('cmd: %s - not input subcommand, display help for the group', $name);
return $this->showHelp();
}

Expand All @@ -281,13 +281,12 @@ public function doRun(array $args): mixed

// update subcommand
$this->commandName = $command;
$this->input->setSubCommand($command);

// update some comment vars
$fullCmd = $this->input->getFullCommand();
$fullCmd = $this->input->buildFullCmd($name, $command);
$this->setCommentsVar('fullCmd', $fullCmd);
$this->setCommentsVar('fullCommand', $fullCmd);
$this->setCommentsVar('binWithCmd', $this->input->getBinWithCommand());
$this->setCommentsVar('binWithCmd', $this->input->buildCmdPath($name, $command));

// get real sub-command name
$command = $this->resolveAlias($command);
Expand All @@ -297,7 +296,7 @@ public function doRun(array $args): mixed

// convert 'boo-foo' to 'booFoo'
$this->action = $action = Str::camelCase($command);
$this->debugf("will run the '%s' group action: %s, subcommand: %s", $name, $action, $command);
$this->debugf("cmd: %s - will run the subcommand: %s(action: %s)", $name, $command, $action);
$method = $this->getMethodName($action);

// fire event
Expand Down
21 changes: 12 additions & 9 deletions src/Decorate/CommandHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace Inhere\Console\Decorate;

use Inhere\Console\Handler\AbstractHandler;
use Inhere\Console\Console;
use Inhere\Console\Handler\AbstractHandler;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\FlagUtil;
use function implode;
Expand Down Expand Up @@ -125,8 +125,8 @@ public function showHelpByFlagsParser(FlagsParser $fs, array $aliases = [], stri
$name = $this->getCommandName();

// $isCommand = $this->isCommand();
$commandId = $this->input->getCommandId();
$this->logf(Console::VERB_DEBUG, 'render help for the command: %s', $commandId);
// $commandId = $this->input->getCommandId();
$this->logf(Console::VERB_DEBUG, 'cmd: %s - begin render help for the command', $name);

if ($aliases) {
$realName = $action ?: $this->getRealName();
Expand All @@ -135,17 +135,20 @@ public function showHelpByFlagsParser(FlagsParser $fs, array $aliases = [], stri
}

$binName = $this->input->getBinName();

$path = $binName . ' ' . $name;
if ($action) {
$group = $this->getGroupName();
$path = "$binName $group $action";
$cmdPath = $binName . ' ' . $this->getPath();
// if ($action) {
// $group = $this->getGroupName();
// $cmdPath = "$binName $group $action";
// }

if ($this->hasSubs()) {
$cmdPath .= ' <cyan>SUBCOMMAND</cyan>';
}

$desc = $fs->getDesc();
$this->writeln(ucfirst($this->parseCommentsVars($desc)));

$help['Usage:'] = "$path [--options ...] [arguments ...]";
$help['Usage:'] = "$cmdPath [--options ...] [arguments ...]";

$help['Options:'] = FlagUtil::alignOptions($fs->getOptsHelpLines());
$help['Argument:'] = $fs->getArgsHelpLines();
Expand Down
2 changes: 1 addition & 1 deletion src/Decorate/ControllerHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function beforeShowCommandList(): void
public function showCommandList(): void
{
$name = self::getName();
$this->logf(Console::VERB_DEBUG, 'display all sub-commands list of the group: %s', $name);
$this->logf(Console::VERB_DEBUG, 'cmd: %s - display all sub-commands list of the group', $name);
$this->beforeShowCommandList();

$refCls = new ReflectionClass($this);
Expand Down
60 changes: 53 additions & 7 deletions src/Decorate/SubCommandsWareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
use Inhere\Console\Handler\CommandWrapper;
use Inhere\Console\Util\Helper;
use InvalidArgumentException;
use Toolkit\Cli\Color\ColorTag;
use Toolkit\Stdlib\Helper\Assert;
use Toolkit\Stdlib\Obj\Traits\NameAliasTrait;
use function array_keys;
use function array_merge;
use function class_exists;
use function count;
use function explode;
use function implode;
use function in_array;
use function is_int;
use function is_object;
Expand Down Expand Up @@ -54,6 +58,13 @@ trait SubCommandsWareTrait
*/
protected string $path = '';

/**
* Command full path nodes. eg: ['git', 'remote', 'set-url']
*
* @var string[]
*/
protected array $pathNodes = [];

/**
* The sub-commands of the command
*
Expand All @@ -64,6 +75,7 @@ trait SubCommandsWareTrait
* 'config' => [
* 'name' => 'string',
* 'desc' => 'string',
* 'aliases' => [],
* 'options' => [],
* 'arguments' => [],
* ]
Expand Down Expand Up @@ -103,11 +115,12 @@ protected function subCommands(): array
protected function dispatchSub(string $name, array $args): mixed
{
$subInfo = $this->commands[$name];
$this->debugf('dispatch the attached subcommand: %s', $name);
$this->debugf('cmd: %s - dispatch the attached subcommand: %s', $this->getRealName(), $name);

// create and init sub-command
$subCmd = $this->createSubCommand($subInfo);
$subCmd->setParent($this);
$subCmd->setPath($this->path);
$subCmd->setInputOutput($this->input, $this->output);

return $subCmd->run($args);
Expand Down Expand Up @@ -273,23 +286,39 @@ public function isSub(string $name): bool
return isset($this->commands[$name]);
}

/**
* @param string $sep
*
* @return string
*/
public function getPath(string $sep = ''): string
{
return $sep ? implode($sep, $this->pathNodes) : $this->path;
}

/**
* @param string $path
*/
public function setPath(string $path): void
{
$this->path = $path;
// set path nodes
$this->pathNodes = explode(' ', $path);
}

/**
* @param string $name
*/
public function addPath(string $name): void
public function addPathNode(string $name): void
{
if ($this->path) {
$this->path .= ' ' . $name;
// add path nodes
$this->pathNodes[] = $name;
} else {
$this->path = $name;
// set path nodes
$this->pathNodes = [$name];
}
}

Expand Down Expand Up @@ -339,6 +368,14 @@ public function setBlocked(array $blocked): void
$this->blocked = $blocked;
}

/**
* @return bool
*/
public function hasSubs(): bool
{
return count($this->commands) > 0;
}

/**
* @return array
*/
Expand All @@ -364,15 +401,24 @@ public function getSubsForHelp(): array
foreach ($this->commands as $name => $subInfo) {
$sub = $subInfo['handler'];
if ($sub instanceof Command) {
$subs[$name] = $sub->getRealDesc();
$desc = $sub->getRealDesc();
// alias names
$aliases = $sub::aliases();
} elseif (is_string($sub)) {
/** @var Command $sub */
$subs[$name] = $sub::getDesc();
$desc = $sub::getDesc();
// alias names
$aliases = $sub::aliases();
} else {
$subConf = $subInfo['config'];

$subs[$name] = $subConf['desc'] ?? 'no description';
$conf = $subInfo['config'];
$desc = $conf['desc'] ?? 'no description';
// alias names
$aliases = $conf['aliases'] ?? [];
}

$extra = $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
// add help desc
$subs[$name] = $desc . $extra;
}

return $subs;
Expand Down
Loading

0 comments on commit 40b4cc8

Please sign in to comment.