Skip to content

Commit 99966d7

Browse files
committed
up: update some output format and input parse
Signed-off-by: inhere <[email protected]>
1 parent 3dc6e4e commit 99966d7

15 files changed

+281
-191
lines changed

src/AbstractApplication.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,41 @@ public function stop(int $code = 0)
293293
*/
294294
public function subRun(string $command, InputInterface $input, OutputInterface $output)
295295
{
296-
$app = clone $this;
296+
$app = $this->copy();
297297
$app->setInput($input);
298298
$app->setOutput($output);
299299

300+
$this->debugf('copy application and run command(%s) with new input, output', $command);
301+
300302
return $app->dispatch($command);
301303
}
302304

305+
/**
306+
* @param InputInterface $input
307+
* @param OutputInterface $output
308+
*/
309+
public function runWithIO(InputInterface $input, OutputInterface $output): void
310+
{
311+
$app = $this->copy();
312+
$app->setInput($input);
313+
$app->setOutput($output);
314+
315+
$this->debugf('copy application and run with new input, output');
316+
$app->run(false);
317+
}
318+
319+
/**
320+
* @return $this
321+
*/
322+
public function copy(): self
323+
{
324+
$app = clone $this;
325+
// reset something
326+
$app->groupObjects = [];
327+
328+
return $app;
329+
}
330+
303331
/**********************************************************
304332
* helper method for the application
305333
**********************************************************/

src/Application.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,12 @@ protected function runCommand(string $name, $handler, array $options)
355355
/**
356356
* Execute an action in a group command(controller)
357357
*
358-
* @param array $info Matched route info
359-
* @param array $options
360-
* @param bool $detachedRun
358+
* @param array $info Matched route info
359+
* @param array $options
360+
* @param bool $detachedRun
361361
*
362362
* @return mixed
363+
* @throws ReflectionException
363364
*/
364365
protected function runAction(array $info, array $options, bool $detachedRun = false)
365366
{

src/Component/CompletionDumper.php

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Inhere\Console\Component;
44

5+
use Inhere\Console\Application;
56
use Toolkit\Stdlib\Obj;
67

78
/**
@@ -12,4 +13,9 @@
1213
*/
1314
class CompletionDumper extends Obj\AbstractObj
1415
{
16+
/**
17+
* @var Application
18+
*/
19+
public $app;
20+
1521
}

src/Component/ConsoleAppIShell.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\Console\Component;
4+
5+
use Inhere\Console\Application;
6+
use Toolkit\Stdlib\Obj\AbstractObj;
7+
8+
/**
9+
* Class ConsoleAppIShell
10+
*
11+
* @package Inhere\Console
12+
*/
13+
class ConsoleAppIShell extends AbstractObj
14+
{
15+
/**
16+
* @var Application
17+
*/
18+
public $app;
19+
20+
// TODO start an shell for run app.
21+
}

src/Component/Interact/Confirm.php

-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public static function ask(string $question, bool $default = true): bool
3737

3838
while (true) {
3939
$answer = Console::readChar($message);
40-
4140
if ('' === $answer) {
4241
return $default;
4342
}
@@ -50,7 +49,5 @@ public static function ask(string $question, bool $default = true): bool
5049
return false;
5150
}
5251
}
53-
54-
return false;
5552
}
5653
}

src/Concern/ApplicationHelpTrait.php

+2
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ public function showHelpInfo(string $command = ''): void
9393

9494
// display help for a special command
9595
if ($command) {
96+
$this->debugf('display command help by use help: help COMMAND');
9697
$in->setCommand($command);
9798
$in->setSOpt('h', true);
9899
$in->clearArgs();
99100
$this->dispatch($command);
100101
return;
101102
}
102103

104+
$this->debugf('display application help by input -h, --help');
103105
$delimiter = $this->delimiter;
104106
$binName = $in->getScriptName();
105107

src/Concern/ControllerHelpTrait.php

+168
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,179 @@
22

33
namespace Inhere\Console\Concern;
44

5+
use Inhere\Console\Application;
6+
use Inhere\Console\Console;
7+
use Inhere\Console\Util\FormatUtil;
8+
use ReflectionClass;
9+
use Toolkit\Cli\ColorTag;
10+
use Toolkit\Stdlib\Str;
11+
use Toolkit\Stdlib\Util\PhpDoc;
12+
use function array_merge;
13+
use function implode;
14+
use function ksort;
15+
use function lcfirst;
16+
use function sprintf;
17+
use function strpos;
18+
use function ucfirst;
19+
use const PHP_EOL;
20+
521
/**
622
* Trait ControllerHelpTrait
723
*
824
* @package Inhere\Console\Concern
925
*/
1026
trait ControllerHelpTrait
1127
{
28+
/**
29+
* Show help of the controller command group or specified command action
30+
* @usage <info>{name}:[command] -h</info> OR <info>{command} [command]</info> OR <info>{name} [command] -h</info>
31+
*
32+
* @options
33+
* -s, --search Search command by input keywords
34+
* --format Set the help information dump format(raw, xml, json, markdown)
35+
* @return int
36+
* @example
37+
* {script} {name} -h
38+
* {script} {name}:help
39+
* {script} {name}:help index
40+
* {script} {name}:index -h
41+
* {script} {name} index
42+
*/
43+
public function helpCommand(): int
44+
{
45+
$action = $this->action;
46+
47+
// Not input action, for all sub-commands of the controller
48+
if (!$action) {
49+
$this->showCommandList();
50+
return 0;
51+
}
52+
53+
$action = Str::camelCase($action);
54+
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
55+
$aliases = $this->getCommandAliases($action);
56+
57+
// up: find global aliases from app
58+
if ($this->app) {
59+
$commandId = $this->input->getCommandId();
60+
$gAliases = $this->app->getAliases($commandId);
61+
62+
if ($gAliases) {
63+
$aliases = array_merge($aliases, $gAliases);
64+
}
65+
}
66+
67+
$this->log(Console::VERB_DEBUG, "display help for the controller method: $method", [
68+
'group' => static::$name,
69+
'action' => $action,
70+
]);
71+
72+
// For a specified sub-command.
73+
return $this->showHelpByMethodAnnotations($method, $action, $aliases);
74+
}
75+
76+
protected function beforeShowCommandList(): void
77+
{
78+
// do something ...
79+
}
80+
81+
/**
82+
* Display all sub-commands list of the controller class
83+
*/
84+
public function showCommandList(): void
85+
{
86+
$this->logf(Console::VERB_DEBUG, 'display all sub-commands list of the group: %s', static::$name);
87+
88+
$this->beforeShowCommandList();
89+
90+
$ref = new ReflectionClass($this);
91+
$sName = lcfirst(self::getName() ?: $ref->getShortName());
92+
93+
if (!($classDes = self::getDescription())) {
94+
$classDes = PhpDoc::description($ref->getDocComment()) ?: 'No description for the command group';
95+
}
96+
97+
$commands = [];
98+
$showDisabled = (bool)$this->getOpt('show-disabled', false);
99+
$defaultDes = 'No description message';
100+
101+
/**
102+
* @var $cmd string The command name.
103+
*/
104+
foreach ($this->getAllCommandMethods($ref) as $cmd => $m) {
105+
if (!$cmd) {
106+
continue;
107+
}
108+
109+
$desc = $this->getCommandMeta('desc', $defaultDes, $cmd);
110+
if ($phpDoc = $m->getDocComment()) {
111+
$desc = PhpDoc::firstLine($phpDoc);
112+
}
113+
114+
// is a annotation tag
115+
if (strpos($desc, '@') === 0) {
116+
$desc = $defaultDes;
117+
}
118+
119+
if ($this->isDisabled($cmd)) {
120+
if (!$showDisabled) {
121+
continue;
122+
}
123+
124+
$desc .= '(<red>DISABLED</red>)';
125+
}
126+
127+
$aliases = $this->getCommandAliases($cmd);
128+
$desc .= $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
129+
130+
$commands[$cmd] = $desc;
131+
}
132+
133+
// sort commands
134+
ksort($commands);
135+
136+
// move 'help' to last.
137+
if ($helpCmd = $commands['help'] ?? null) {
138+
unset($commands['help']);
139+
$commands['help'] = $helpCmd;
140+
}
141+
142+
$script = $this->getScriptName();
143+
144+
// if is alone running.
145+
if ($detached = $this->isDetached()) {
146+
$name = $sName . ' ';
147+
$usage = "$script <info>{command}</info> [--options ...] [arguments ...]";
148+
} else {
149+
$name = $sName . $this->delimiter;
150+
// $usage = "$script {$name}<info>{command}</info> [--options ...] [arguments ...]";
151+
$usage = [
152+
"$script $name<info>{command}</info> [--options ...] [arguments ...]",
153+
"$script $sName <info>{command}</info> [--options ...] [arguments ...]",
154+
];
155+
}
156+
157+
$globalOptions = array_merge(Application::getGlobalOptions(), static::$globalOptions);
158+
159+
$this->output->startBuffer();
160+
$this->output->write(ucfirst($classDes) . PHP_EOL);
161+
162+
if ($aliases = $this->getAliases()) {
163+
$this->output->writef("<comment>Alias:</comment> %s\n", implode(',', $aliases));
164+
}
165+
166+
$this->output->mList([
167+
'Usage:' => $usage,
168+
//'Group Name:' => "<info>$sName</info>",
169+
'Global Options:' => FormatUtil::alignOptions($globalOptions),
170+
'Available Commands:' => $commands,
171+
], [
172+
'sepChar' => ' ',
173+
]);
174+
175+
$msgTpl = 'More information about a command, please see: <cyan>%s %s {command} -h</cyan>';
176+
$this->output->write(sprintf($msgTpl, $script, $detached ? '' : $sName));
177+
$this->output->flush();
178+
}
179+
12180
}

src/Concern/FormatOutputAwareTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ public function writeln($text, $quit = false, array $opts = []): int
7171
*
7272
* @return int
7373
*/
74-
public function println($text, $quit = false, array $opts = []): int
74+
public function println($text, bool $quit = false, array $opts = []): int
7575
{
7676
return Console::writeln($text, $quit, $opts);
7777
}
7878

7979
/**
8080
* @param string|mixed $text
8181
* @param bool $nl
82-
* @param bool $quit
82+
* @param bool|int $quit
8383
* @param array $opts
8484
*
8585
* @return int

src/Concern/StyledOutputAwareTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function progressBar($total, array $opts = []): Generator
205205
* @return int
206206
* @throws LogicException
207207
*/
208-
public function __call($method, array $args = [])
208+
public function __call(string $method, array $args = [])
209209
{
210210
$map = Show::getBlockMethods(false);
211211

0 commit comments

Comments
 (0)