Skip to content

Commit 40b4cc8

Browse files
committed
up: update some logic for run sub commands
1 parent 5647598 commit 40b4cc8

10 files changed

+115
-80
lines changed

Diff for: src/AbstractApplication.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
use ErrorException;
1313
use Inhere\Console\Component\ErrorHandler;
14-
use Inhere\Console\Component\Router;
1514
use Inhere\Console\Component\Formatter\Title;
16-
use Inhere\Console\Decorate\SimpleEventAwareTrait;
15+
use Inhere\Console\Component\Router;
1716
use Inhere\Console\Contract\ApplicationInterface;
1817
use Inhere\Console\Contract\ErrorHandlerInterface;
19-
use Inhere\Console\Decorate\InputOutputAwareTrait;
2018
use Inhere\Console\Decorate\ApplicationHelpTrait;
19+
use Inhere\Console\Decorate\InputOutputAwareTrait;
20+
use Inhere\Console\Decorate\SimpleEventAwareTrait;
2121
use Inhere\Console\Decorate\StyledOutputAwareTrait;
2222
use Inhere\Console\IO\Input;
2323
use Inhere\Console\IO\Output;
@@ -276,7 +276,7 @@ protected function beforeRun(): bool
276276
$this->commandName = $command;
277277
$this->flags->popFirstRawArg();
278278
$this->input->setCommand($command);
279-
$this->logf(Console::VERB_DEBUG, 'found the application command: %s', $command);
279+
$this->logf(Console::VERB_DEBUG, 'app - match and found the command: %s', $command);
280280

281281
// like: help, version, list
282282
if (!$this->handleGlobalCommand($command)) {

Diff for: src/Application.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function dispatch(string $name, array $args = []): mixed
232232
}
233233

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

237237
// format is: `group action`
238238
if (strpos($name, ' ') > 0) {

Diff for: src/Command.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function beforeInitFlagsParser(FlagsParser $fs): void
6363
*/
6464
protected function afterInitFlagsParser(FlagsParser $fs): void
6565
{
66-
$this->debugf('load flags configure for command: %s', $this->getRealCName());
66+
$this->debugf('cmd: %s - load command flags configure', $this->getRealCName());
6767
$this->configure();
6868
$this->configFlags($fs);
6969

Diff for: src/Concern/AbstractInput.php

-37
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,6 @@ abstract class AbstractInput implements InputInterface
7070
*/
7171
protected string $command = '';
7272

73-
/**
74-
* the command name(Is first argument)
75-
* e.g `subcommand` in the `./app group subcommand`
76-
*
77-
* @var string
78-
*/
79-
protected string $subCommand = '';
80-
8173
/**
8274
* eg `./examples/app home:useArg status=2 name=john arg0 -s=test --page=23`
8375
*
@@ -165,19 +157,6 @@ protected function collectInfo(array $rawFlags): void
165157
$this->fullScript = implode(' ', $rawFlags);
166158
}
167159

168-
/**
169-
* @return string
170-
*/
171-
public function getCommandPath(): string
172-
{
173-
$path = $this->command;
174-
if ($this->subCommand) {
175-
$path .= ' ' . $this->subCommand;
176-
}
177-
178-
return $path;
179-
}
180-
181160
/**
182161
* @return bool
183162
*/
@@ -335,22 +314,6 @@ public function setTokens(array $tokens): void
335314
$this->collectInfo($tokens);
336315
}
337316

338-
/**
339-
* @return string
340-
*/
341-
public function getSubCommand(): string
342-
{
343-
return $this->subCommand;
344-
}
345-
346-
/**
347-
* @param string $subCommand
348-
*/
349-
public function setSubCommand(string $subCommand): void
350-
{
351-
$this->subCommand = $subCommand;
352-
}
353-
354317
/**
355318
* @return FlagsParser
356319
*/

Diff for: src/Controller.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,13 @@ public function doRun(array $args): mixed
265265

266266
// and not default command
267267
if (!$command) {
268-
$this->debugf('run args is empty, display help for the group: %s', $name);
268+
$this->debugf('cmd: %s - run args is empty, display help for the group', $name);
269269
return $this->showHelp();
270270
}
271271
} else {
272272
$first = $args[0];
273273
if (!FlagUtil::isValidName($first)) {
274-
$this->debugf('not input subcommand, display help for the group: %s', $name);
274+
$this->debugf('cmd: %s - not input subcommand, display help for the group', $name);
275275
return $this->showHelp();
276276
}
277277

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

282282
// update subcommand
283283
$this->commandName = $command;
284-
$this->input->setSubCommand($command);
285284

286285
// update some comment vars
287-
$fullCmd = $this->input->getFullCommand();
286+
$fullCmd = $this->input->buildFullCmd($name, $command);
288287
$this->setCommentsVar('fullCmd', $fullCmd);
289288
$this->setCommentsVar('fullCommand', $fullCmd);
290-
$this->setCommentsVar('binWithCmd', $this->input->getBinWithCommand());
289+
$this->setCommentsVar('binWithCmd', $this->input->buildCmdPath($name, $command));
291290

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

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

303302
// fire event

Diff for: src/Decorate/CommandHelpTrait.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace Inhere\Console\Decorate;
1111

12-
use Inhere\Console\Handler\AbstractHandler;
1312
use Inhere\Console\Console;
13+
use Inhere\Console\Handler\AbstractHandler;
1414
use Toolkit\PFlag\FlagsParser;
1515
use Toolkit\PFlag\FlagUtil;
1616
use function implode;
@@ -125,8 +125,8 @@ public function showHelpByFlagsParser(FlagsParser $fs, array $aliases = [], stri
125125
$name = $this->getCommandName();
126126

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

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

137137
$binName = $this->input->getBinName();
138-
139-
$path = $binName . ' ' . $name;
140-
if ($action) {
141-
$group = $this->getGroupName();
142-
$path = "$binName $group $action";
138+
$cmdPath = $binName . ' ' . $this->getPath();
139+
// if ($action) {
140+
// $group = $this->getGroupName();
141+
// $cmdPath = "$binName $group $action";
142+
// }
143+
144+
if ($this->hasSubs()) {
145+
$cmdPath .= ' <cyan>SUBCOMMAND</cyan>';
143146
}
144147

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

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

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

Diff for: src/Decorate/ControllerHelpTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function beforeShowCommandList(): void
9292
public function showCommandList(): void
9393
{
9494
$name = self::getName();
95-
$this->logf(Console::VERB_DEBUG, 'display all sub-commands list of the group: %s', $name);
95+
$this->logf(Console::VERB_DEBUG, 'cmd: %s - display all sub-commands list of the group', $name);
9696
$this->beforeShowCommandList();
9797

9898
$refCls = new ReflectionClass($this);

Diff for: src/Decorate/SubCommandsWareTrait.php

+53-7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
use Inhere\Console\Handler\CommandWrapper;
1717
use Inhere\Console\Util\Helper;
1818
use InvalidArgumentException;
19+
use Toolkit\Cli\Color\ColorTag;
1920
use Toolkit\Stdlib\Helper\Assert;
2021
use Toolkit\Stdlib\Obj\Traits\NameAliasTrait;
2122
use function array_keys;
2223
use function array_merge;
2324
use function class_exists;
25+
use function count;
26+
use function explode;
27+
use function implode;
2428
use function in_array;
2529
use function is_int;
2630
use function is_object;
@@ -54,6 +58,13 @@ trait SubCommandsWareTrait
5458
*/
5559
protected string $path = '';
5660

61+
/**
62+
* Command full path nodes. eg: ['git', 'remote', 'set-url']
63+
*
64+
* @var string[]
65+
*/
66+
protected array $pathNodes = [];
67+
5768
/**
5869
* The sub-commands of the command
5970
*
@@ -64,6 +75,7 @@ trait SubCommandsWareTrait
6475
* 'config' => [
6576
* 'name' => 'string',
6677
* 'desc' => 'string',
78+
* 'aliases' => [],
6779
* 'options' => [],
6880
* 'arguments' => [],
6981
* ]
@@ -103,11 +115,12 @@ protected function subCommands(): array
103115
protected function dispatchSub(string $name, array $args): mixed
104116
{
105117
$subInfo = $this->commands[$name];
106-
$this->debugf('dispatch the attached subcommand: %s', $name);
118+
$this->debugf('cmd: %s - dispatch the attached subcommand: %s', $this->getRealName(), $name);
107119

108120
// create and init sub-command
109121
$subCmd = $this->createSubCommand($subInfo);
110122
$subCmd->setParent($this);
123+
$subCmd->setPath($this->path);
111124
$subCmd->setInputOutput($this->input, $this->output);
112125

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

289+
/**
290+
* @param string $sep
291+
*
292+
* @return string
293+
*/
294+
public function getPath(string $sep = ''): string
295+
{
296+
return $sep ? implode($sep, $this->pathNodes) : $this->path;
297+
}
298+
276299
/**
277300
* @param string $path
278301
*/
279302
public function setPath(string $path): void
280303
{
281304
$this->path = $path;
305+
// set path nodes
306+
$this->pathNodes = explode(' ', $path);
282307
}
283308

284309
/**
285310
* @param string $name
286311
*/
287-
public function addPath(string $name): void
312+
public function addPathNode(string $name): void
288313
{
289314
if ($this->path) {
290315
$this->path .= ' ' . $name;
316+
// add path nodes
317+
$this->pathNodes[] = $name;
291318
} else {
292319
$this->path = $name;
320+
// set path nodes
321+
$this->pathNodes = [$name];
293322
}
294323
}
295324

@@ -339,6 +368,14 @@ public function setBlocked(array $blocked): void
339368
$this->blocked = $blocked;
340369
}
341370

371+
/**
372+
* @return bool
373+
*/
374+
public function hasSubs(): bool
375+
{
376+
return count($this->commands) > 0;
377+
}
378+
342379
/**
343380
* @return array
344381
*/
@@ -364,15 +401,24 @@ public function getSubsForHelp(): array
364401
foreach ($this->commands as $name => $subInfo) {
365402
$sub = $subInfo['handler'];
366403
if ($sub instanceof Command) {
367-
$subs[$name] = $sub->getRealDesc();
404+
$desc = $sub->getRealDesc();
405+
// alias names
406+
$aliases = $sub::aliases();
368407
} elseif (is_string($sub)) {
369408
/** @var Command $sub */
370-
$subs[$name] = $sub::getDesc();
409+
$desc = $sub::getDesc();
410+
// alias names
411+
$aliases = $sub::aliases();
371412
} else {
372-
$subConf = $subInfo['config'];
373-
374-
$subs[$name] = $subConf['desc'] ?? 'no description';
413+
$conf = $subInfo['config'];
414+
$desc = $conf['desc'] ?? 'no description';
415+
// alias names
416+
$aliases = $conf['aliases'] ?? [];
375417
}
418+
419+
$extra = $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
420+
// add help desc
421+
$subs[$name] = $desc . $extra;
376422
}
377423

378424
return $subs;

0 commit comments

Comments
 (0)