Skip to content

Commit d4b73b5

Browse files
committed
fix some error, update some
1 parent 6ac9a8c commit d4b73b5

File tree

10 files changed

+137
-95
lines changed

10 files changed

+137
-95
lines changed

examples/Controllers/ShowController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public static function commandAliases(): array
3434
];
3535
}
3636

37+
/**
38+
* @return array
39+
*/
40+
protected function commonOptions(): array
41+
{
42+
return \array_merge(parent::commonOptions(), [
43+
'-c, --command' => 'This is a common option for all sub-commands',
44+
]);
45+
}
46+
3747
/**
3848
* output format message: title
3949
*/

src/Base/AbstractApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Console\Base;
1010

1111
use Inhere\Console\BuiltIn\ErrorHandler;
12+
use Inhere\Console\Face\CommandInterface;
1213
use Inhere\Console\Face\ErrorHandlerInterface;
1314
use Inhere\Console\IO\Input;
1415
use Inhere\Console\IO\InputInterface;

src/Base/AbstractCommand.php

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ abstract class AbstractCommand implements BaseCommandInterface
4747
*/
4848
protected static $coroutine = false;
4949

50-
/** @var array */
51-
protected static $commandOptions = [
52-
'--skip-invalid' => 'Whether ignore invalid arguments and options, when use input definition',
53-
];
54-
5550
/**
5651
* Allow display message tags in the command annotation
5752
* @var array
@@ -63,16 +58,20 @@ abstract class AbstractCommand implements BaseCommandInterface
6358
'arguments' => true,
6459
'options' => true,
6560
'example' => true,
61+
'help' => true,
6662
];
6763

6864
/** @var Application */
6965
protected $app;
7066

67+
/** @var array common options for all sub-commands */
68+
private $commonOptions;
69+
7170
/** @var InputDefinition|null */
7271
private $definition;
7372

7473
/** @var string */
75-
private $processTitle;
74+
private $processTitle = '';
7675

7776
/** @var array */
7877
private $annotationVars;
@@ -111,6 +110,7 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
111110
$this->definition = $definition;
112111
}
113112

113+
$this->commonOptions = $this->commonOptions();
114114
$this->annotationVars = $this->annotationVars();
115115

116116
$this->init();
@@ -143,11 +143,21 @@ protected function createDefinition(): InputDefinition
143143
return $this->definition;
144144
}
145145

146+
/**
147+
* @return array
148+
*/
149+
protected function commonOptions(): array
150+
{
151+
return [
152+
'--skip-invalid' => 'Whether ignore invalid arguments and options, when use input definition',
153+
];
154+
}
155+
146156
/**
147157
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
148158
* @return array
149159
*/
150-
public function annotationVars(): array
160+
protected function annotationVars(): array
151161
{
152162
// e.g: `more info see {name}:index`
153163
return [
@@ -157,7 +167,7 @@ public function annotationVars(): array
157167
'script' => $this->input->getScript(), // bin/app
158168
'binName' => $this->input->getScript(), // bin/app
159169
'command' => $this->input->getCommand(), // demo OR home:test
160-
'fullCommand' => $this->input->getScript() . ' ' . $this->input->getCommand(),
170+
'fullCommand' => $this->input->getFullCommand(),
161171
];
162172
}
163173

@@ -256,48 +266,23 @@ protected function afterExecute()
256266
{
257267
}
258268

259-
/**
260-
* display help information
261-
* @return bool
262-
*/
263-
protected function showHelp(): bool
264-
{
265-
if (!$definition = $this->getDefinition()) {
266-
return false;
267-
}
268-
269-
// 创建了 InputDefinition , 则使用它的信息(此时不会再解析和使用命令的注释)
270-
$help = $definition->getSynopsis();
271-
$help['usage:'] = \sprintf('%s %s %s', $this->getScriptName(), $this->getCommandName(), $help['usage:']);
272-
$help['global options:'] = FormatUtil::alignOptions(Application::getInternalOptions());
273-
274-
if (empty($help[0]) && $this->isAlone()) {
275-
$help[0] = self::getDescription();
276-
}
277-
278-
$this->output->mList($help, ['sepChar' => ' ']);
279-
return true;
280-
}
281-
282269
/**
283270
* prepare run
284271
* @throws \InvalidArgumentException
285272
* @throws \RuntimeException
286273
*/
287274
protected function prepare(): bool
288275
{
289-
if ($this->processTitle) {
276+
if ($this->processTitle && 'Darwin' !== \PHP_OS) {
290277
if (\function_exists('cli_set_process_title')) {
291-
if (false === @cli_set_process_title($this->processTitle)) {
292-
$error = \error_get_last();
293-
294-
if ($error && 'Darwin' !== PHP_OS) {
295-
throw new \RuntimeException($error['message']);
296-
}
297-
}
278+
\cli_set_process_title($this->processTitle);
298279
} elseif (\function_exists('setproctitle')) {
299280
\setproctitle($this->processTitle);
300281
}
282+
283+
if ($error = \error_get_last()) {
284+
throw new \RuntimeException($error['message']);
285+
}
301286
}
302287

303288
return $this->validateInput();
@@ -379,7 +364,8 @@ public function validateInput(): bool
379364

380365
if (\count($missingOpts) > 0) {
381366
$out->liteError(
382-
\sprintf('Not enough options parameters (missing: "%s").', \implode(', ', $missingOpts))
367+
\sprintf('Not enough options parameters (missing: "%s").',
368+
\implode(', ', $missingOpts))
383369
);
384370

385371
return false;
@@ -458,12 +444,39 @@ public function isAlone(): bool
458444
return $this instanceof CommandInterface;
459445
}
460446

447+
/**********************************************************
448+
* display help information
449+
**********************************************************/
450+
461451
/**
452+
* display help information
462453
* @return bool
463454
*/
464-
public function isDebug(): bool
455+
protected function showHelp(): bool
465456
{
466-
return $this->input->boolOpt('debug');
457+
if (!$definition = $this->getDefinition()) {
458+
return false;
459+
}
460+
461+
// 创建了 InputDefinition , 则使用它的信息(此时不会再解析和使用命令的注释)
462+
$help = $definition->getSynopsis();
463+
$help['usage:'] = \sprintf('%s %s %s', $this->getScriptName(), $this->getCommandName(), $help['usage:']);
464+
$help['global options:'] = FormatUtil::alignOptions(Application::getInternalOptions());
465+
466+
if (empty($help[0]) && $this->isAlone()) {
467+
$help[0] = self::getDescription();
468+
}
469+
470+
if (empty($help[0])) {
471+
$help[0] = 'No description message for the command';
472+
}
473+
474+
// output description
475+
$this->write(\ucfirst($help[0]) . \PHP_EOL);
476+
unset($help[0]);
477+
478+
$this->output->mList($help, ['sepChar' => ' ']);
479+
return true;
467480
}
468481

469482
/**
@@ -497,7 +510,7 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
497510

498511
if ($aliases) {
499512
$realName = $action ?: self::getName();
500-
$help['Command:'] = sprintf('%s(alias: <info>%s</info>)', $realName, implode(',', $aliases));
513+
$help['Command:'] = \sprintf('%s(alias: <info>%s</info>)', $realName, \implode(',', $aliases));
501514
}
502515

503516
foreach (\array_keys(self::$annotationTags) as $tag) {
@@ -531,11 +544,11 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
531544

532545
if (isset($help['Description:'])) {
533546
$description = $help['Description:'] ?: 'No description message for the command';
534-
$this->write(ucfirst($description) . PHP_EOL);
547+
$this->write(\ucfirst($description) . \PHP_EOL);
535548
unset($help['Description:']);
536549
}
537550

538-
$help['Global Options:'] = FormatUtil::alignOptions(\array_merge(Application::getInternalOptions(), static::$commandOptions));
551+
$help['Global Options:'] = FormatUtil::alignOptions(\array_merge(Application::getInternalOptions(), $this->commonOptions));
539552

540553
$this->output->mList($help, [
541554
'sepChar' => ' ',
@@ -640,6 +653,14 @@ public function setDefinition(InputDefinition $definition)
640653
$this->definition = $definition;
641654
}
642655

656+
/**
657+
* @return array
658+
*/
659+
public function getCommonOptions(): array
660+
{
661+
return $this->commonOptions;
662+
}
663+
643664
/**
644665
* @return array
645666
*/

src/BuiltIn/ErrorHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(\Throwable $e, AbstractApplication $app)
3131
\n<error> Error </error> <mga>%s</mga>
3232
3333
At File <cyan>%s</cyan> line <bold>%d</bold>
34-
Exception $class
34+
Exception class is <magenta>$class</magenta>
3535
<comment>Code View:</comment>\n\n%s
3636
<comment>Code Trace:</comment>\n\n%s\n
3737
ERR;

src/Components/Style/Color.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function toStyle(): string
213213
$values[] = static::$knownOptions[$option];
214214
}
215215

216-
return implode(';', $values);
216+
return \implode(';', $values);
217217
}
218218

219219
/**

src/Components/Style/Style.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ public static function create(): Style
9393
public function __construct($fg = '', $bg = '', array $options = [])
9494
{
9595
if ($fg || $bg || $options) {
96-
$this->add('base', [
97-
'fg' => $fg,
98-
'bg' => $bg,
99-
'options' => $options
100-
]);
96+
$this->add('base', $fg, $bg, $options);
10197
}
10298

10399
$this->loadDefaultStyles();
@@ -112,7 +108,7 @@ public function __construct($fg = '', $bg = '', array $options = [])
112108
public function __call($method, array $args)
113109
{
114110
if (isset($args[0]) && $this->hasStyle($method)) {
115-
return $this->format(sprintf('<%s>%s</%s>', $method, $args[0], $method));
111+
return $this->format(\sprintf('<%s>%s</%s>', $method, $args[0], $method));
116112
}
117113

118114
throw new \InvalidArgumentException("You called method is not exists: $method");
@@ -138,7 +134,7 @@ protected function loadDefaultStyles()
138134
->add(self::COMMENT, ['fg' => 'yellow',])//'options' => ['bold']
139135
->add(self::QUESTION, ['fg' => 'black', 'bg' => 'cyan'])
140136
->add(self::DANGER, ['fg' => 'red',])// 'bg' => 'magenta', 'options' => ['bold']
141-
->add(self::ERROR, ['fg' => 'black', 'bg' => 'red'])
137+
->add(self::ERROR, ['fg' => 'white', 'bg' => 'red', 'options' => ['bold']])
142138
->add('underline', ['fg' => 'normal', 'options' => ['underscore']])
143139
->add('blue', ['fg' => 'blue'])
144140
->add('cyan', ['fg' => 'cyan'])
@@ -176,7 +172,7 @@ public function render($text)
176172
*/
177173
public function format(string $text)
178174
{
179-
if (!$text || false === strpos($text, '<')) {
175+
if (!$text || false === \strpos($text, '</')) {
180176
return $text;
181177
}
182178

@@ -192,11 +188,11 @@ public function format(string $text)
192188
foreach ((array)$matches[0] as $i => $m) {
193189
$key = $matches[1][$i];
194190

195-
if (array_key_exists($key, $this->styles)) {
191+
if (\array_key_exists($key, $this->styles)) {
196192
$text = $this->replaceColor($text, $key, $matches[2][$i], (string)$this->styles[$key]);
197193

198194
/** Custom style format @see Color::makeByString() */
199-
} elseif (strpos($key, '=')) {
195+
} elseif (\strpos($key, '=')) {
200196
$text = $this->replaceColor($text, $key, $matches[2][$i], (string)Color::makeByString($key));
201197
}
202198
}
@@ -216,7 +212,7 @@ protected function replaceColor($text, $tag, $match, $style): string
216212
{
217213
$replace = self::$noColor ? $match : sprintf("\033[%sm%s\033[0m", $style, $match);
218214

219-
return str_replace("<$tag>$match</$tag>", $replace, $text);
215+
return \str_replace("<$tag>$match</$tag>", $replace, $text);
220216
// return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
221217
}
222218

@@ -228,7 +224,7 @@ protected function replaceColor($text, $tag, $match, $style): string
228224
public static function stripColor(string $string)
229225
{
230226
// $text = strip_tags($text);
231-
return preg_replace(self::STRIP_TAG, '', $string);
227+
return \preg_replace(self::STRIP_TAG, '', $string);
232228
}
233229

234230
/****************************************************************************
@@ -282,8 +278,8 @@ public function addByArray(string $name, array $styleConfig): self
282278
'options' => []
283279
];
284280

285-
$config = array_merge($style, $styleConfig);
286-
list($fg, $bg, $extra, $options) = array_values($config);
281+
$config = \array_merge($style, $styleConfig);
282+
list($fg, $bg, $extra, $options) = \array_values($config);
287283

288284
$this->styles[$name] = Color::make($fg, $bg, $options, $extra);
289285

@@ -295,15 +291,15 @@ public function addByArray(string $name, array $styleConfig): self
295291
*/
296292
public function getStyleNames(): array
297293
{
298-
return array_keys($this->styles);
294+
return \array_keys($this->styles);
299295
}
300296

301297
/**
302298
* @return array
303299
*/
304300
public function getNames(): array
305301
{
306-
return array_keys($this->styles);
302+
return \array_keys($this->styles);
307303
}
308304

309305
/**

0 commit comments

Comments
 (0)