Skip to content

Commit 1ca9024

Browse files
committed
some modify for help display
1 parent bd8af73 commit 1ca9024

File tree

6 files changed

+98
-35
lines changed

6 files changed

+98
-35
lines changed

src/Base/AbstractApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public function showCommandList($quit = true)
527527
$internalOptions = FormatUtil::alignmentOptions(self::$internalOptions);
528528

529529
$this->output->mList([
530-
'Usage:' => "$script <info>{command}</info> [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
530+
'Usage:' => "$script <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]",
531531
'Options:' => $internalOptions,
532532
'Internal Commands:' => $internalCommands,
533533
'Available Commands:' => \array_merge($controllerArr, $commandArr),

src/Base/AbstractCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function annotationVars(): array
153153
'group' => self::getName(),
154154
'workDir' => $this->input->getPwd(),
155155
'script' => $this->input->getScript(), // bin/app
156+
'binName' => $this->input->getScript(), // bin/app
156157
'command' => $this->input->getCommand(), // demo OR home:test
157158
'fullCommand' => $this->input->getScript() . ' ' . $this->input->getCommand(),
158159
];
@@ -251,11 +252,11 @@ protected function showHelp(): bool
251252

252253
// 创建了 InputDefinition , 则使用它的信息(此时不会再解析和使用命令的注释)
253254
$help = $definition->getSynopsis();
254-
$help['usage'] = sprintf('%s %s %s', $this->input->getScript(), $this->input->getCommand(), $help['usage']);
255+
$help['usage:'] = \sprintf('%s %s %s', $this->input->getScript(), $this->input->getCommand(), $help['usage:']);
255256
$help['global options:'] = FormatUtil::alignmentOptions(Application::getInternalOptions());
256257

257-
if (empty($help['description']) && $this->isAlone()) {
258-
$help['description'] = self::getDescription();
258+
if (empty($help[0]) && $this->isAlone()) {
259+
$help[0] = self::getDescription();
259260
}
260261

261262
$this->output->mList($help, ['sepChar' => ' ']);

src/BuiltIn/GenShellAutoComplete.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,32 @@
88

99
namespace Inhere\Console\BuiltIn;
1010

11+
use Inhere\Console\Command;
12+
use Inhere\Console\IO\Input;
13+
use Inhere\Console\IO\Output;
14+
1115
/**
1216
* Class GenShellAutoComplete
1317
* @package Inhere\Console\BuiltIn
1418
*/
15-
class GenShellAutoComplete
19+
class GenShellAutoComplete extends Command
1620
{
21+
protected static $name = 'gen:ac';
22+
protected static $description = 'Start a php built-in http server for development';
23+
24+
public static function aliases(): array
25+
{
26+
return ['genac', 'gen-ac'];
27+
}
1728

18-
}
29+
/**
30+
* do execute
31+
* @param Input $input
32+
* @param Output $output
33+
* @return int|mixed
34+
*/
35+
protected function execute($input, $output)
36+
{
37+
// TODO: Implement execute() method.
38+
}
39+
}

src/IO/Input.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Input implements InputInterface
7878
public function __construct($argv = null, $parsing = true)
7979
{
8080
if (null === $argv) {
81-
$argv = $_SERVER['argv'];
81+
$argv = (array)$_SERVER['argv'];
8282
}
8383

8484
$this->pwd = $this->getPwd();
@@ -600,6 +600,14 @@ public function getScriptName(): string
600600
return $this->script;
601601
}
602602

603+
/**
604+
* @return string
605+
*/
606+
public function getBinName(): string
607+
{
608+
return $this->script;
609+
}
610+
603611
/**
604612
* @return string
605613
*/

src/IO/InputDefinition.php

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public function __construct(array $arguments = [], array $options = [])
6464
$this->setOptions($options);
6565
}
6666

67+
/***************************************************************************
68+
* some methods for the arguments
69+
***************************************************************************/
70+
6771
/**
6872
* @param array $arguments
6973
* @return InputDefinition
@@ -91,6 +95,19 @@ public function addArguments(array $arguments): self
9195
return $this;
9296
}
9397

98+
/**
99+
* alias of the addArgument
100+
* @param string $name
101+
* @param int|null $mode
102+
* @param string $description
103+
* @param null $default
104+
* @return InputDefinition
105+
*/
106+
public function addArg(string $name, int $mode = null, string $description = '', $default = null): self
107+
{
108+
return $this->addArgument($name, $mode, $description, $default);
109+
}
110+
94111
/**
95112
* Adds an argument.
96113
*
@@ -101,7 +118,7 @@ public function addArguments(array $arguments): self
101118
* @return $this
102119
* @throws \LogicException
103120
*/
104-
public function addArgument($name, $mode = null, $description = '', $default = null): self
121+
public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self
105122
{
106123
if (null === $mode) {
107124
$mode = Input::ARG_OPTIONAL;
@@ -158,16 +175,17 @@ public function addArgument($name, $mode = null, $description = '', $default = n
158175

159176
/**
160177
* @param int|string $name
161-
* @return array
162-
* @throws \InvalidArgumentException
178+
* @param null $default
179+
* @return string|int|null
163180
*/
164-
public function getArgument($name): array
181+
public function getArgument($name, $default = null)
165182
{
166-
if (!$this->hasArgument($name)) {
167-
throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
168-
}
183+
$arguments = \is_int($name) ? \array_values($this->arguments) : $this->arguments;
169184

170-
$arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
185+
if (!isset($arguments[$name])) {
186+
return $default;
187+
// throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
188+
}
171189

172190
return $arguments[$name];
173191
}
@@ -178,7 +196,7 @@ public function getArgument($name): array
178196
*/
179197
public function hasArgument($name): bool
180198
{
181-
$arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
199+
$arguments = \is_int($name) ? \array_values($this->arguments) : $this->arguments;
182200

183201
return isset($arguments[$name]);
184202
}
@@ -208,6 +226,10 @@ public function getArgumentRequiredCount(): int
208226
return $this->requiredCount;
209227
}
210228

229+
/***************************************************************************
230+
* some methods for the options
231+
***************************************************************************/
232+
211233
/**
212234
* Sets the options
213235
*
@@ -236,6 +258,16 @@ public function addOptions(array $options = [])
236258
}
237259
}
238260

261+
/**
262+
* alias of the addOption
263+
* {@inheritdoc}
264+
* @return InputDefinition
265+
*/
266+
public function addOpt(string $name, string $shortcut = null, int $mode = null, string $description = '', $default = null): self
267+
{
268+
return $this->addOption($name, $shortcut, $mode, $description, $default);
269+
}
270+
239271
/**
240272
* Adds an option.
241273
*
@@ -249,10 +281,10 @@ public function addOptions(array $options = [])
249281
* @throws \InvalidArgumentException
250282
* @throws \LogicException
251283
*/
252-
public function addOption(string $name, string $shortcut = null, $mode = null, $description = '', $default = null): self
284+
public function addOption(string $name, string $shortcut = null, int $mode = null, string $description = '', $default = null): self
253285
{
254-
if (0 === strpos($name, '-')) {
255-
$name = trim($name, '-');
286+
if (0 === \strpos($name, '-')) {
287+
$name = \trim($name, '-');
256288
}
257289

258290
if (empty($name)) {
@@ -329,7 +361,7 @@ public function addOption(string $name, string $shortcut = null, $mode = null, $
329361
* @return array
330362
* @throws \InvalidArgumentException
331363
*/
332-
public function getOption($name): array
364+
public function getOption(string $name): array
333365
{
334366
if (!$this->hasOption($name)) {
335367
throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
@@ -342,7 +374,7 @@ public function getOption($name): array
342374
* @param string $name
343375
* @return bool
344376
*/
345-
public function hasOption($name): bool
377+
public function hasOption(string $name): bool
346378
{
347379
return isset($this->options[$name]);
348380
}
@@ -397,7 +429,7 @@ private function shortcutToName(string $shortcut)
397429
*/
398430
private function mergeArgOptConfig(array $map): array
399431
{
400-
return $map ? array_merge(self::$defaultArgOptConfig, $map) : self::$defaultArgOptConfig;
432+
return $map ? \array_merge(self::$defaultArgOptConfig, $map) : self::$defaultArgOptConfig;
401433
}
402434

403435
/**
@@ -416,16 +448,16 @@ public function getSynopsis(bool $short = false): array
416448
$value = '';
417449

418450
if ($this->optionIsAcceptValue($option['mode'])) {
419-
$value = sprintf(
451+
$value = \sprintf(
420452
' %s%s%s',
421453
$option['optional'] ? '[' : '',
422-
strtoupper($name),
454+
\strtoupper($name),
423455
$option['optional'] ? ']' : ''
424456
);
425457
}
426458

427-
$shortcut = $option['shortcut'] ? sprintf('-%s, ', $option['shortcut']) : '';
428-
$elements[] = sprintf('[%s--%s%s]', $shortcut, $name, $value);
459+
$shortcut = $option['shortcut'] ? \sprintf('-%s, ', $option['shortcut']) : ' ';
460+
$elements[] = \sprintf('[%s--%s%s]', $shortcut, $name, $value);
429461

430462
$key = "{$shortcut}--{$name}";
431463
$opts[$key] = ($option['required'] ? '<red>*</red>' : '') . $option['description'];
@@ -455,11 +487,12 @@ public function getSynopsis(bool $short = false): array
455487
}
456488

457489
return [
458-
'description' => $this->description,
459-
'usage' => implode(' ', $elements),
460-
'arguments' => $args,
461-
'options' => $opts,
462-
'example' => $this->example,
490+
$this->description,
491+
'usage:' => \implode(' ', $elements),
492+
'options:' => $opts,
493+
'arguments:' => $args,
494+
'example:' => $this->example,
495+
'global options:' => '',
463496
];
464497
}
465498

@@ -480,7 +513,7 @@ public function argumentIsRequired($name): bool
480513
* @param int $mode
481514
* @return bool
482515
*/
483-
protected function argumentIsAcceptValue($mode): bool
516+
protected function argumentIsAcceptValue(int $mode): bool
484517
{
485518
return $mode === Input::ARG_REQUIRED || $mode === Input::ARG_OPTIONAL;
486519
}
@@ -489,7 +522,7 @@ protected function argumentIsAcceptValue($mode): bool
489522
* @param int $mode
490523
* @return bool
491524
*/
492-
protected function optionIsAcceptValue($mode): bool
525+
protected function optionIsAcceptValue(int $mode): bool
493526
{
494527
return $mode === Input::OPT_REQUIRED || $mode === Input::OPT_OPTIONAL;
495528
}

src/Utils/Interact.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public static function confirm(string $question, $default = true): bool
256256
* @param bool|null $default
257257
* @return bool
258258
*/
259-
public static function answerIsYes(bool $default = true): bool
259+
public static function answerIsYes(bool $default = null): bool
260260
{
261261
$mark = ' [yes|no]: ';
262262

@@ -280,7 +280,7 @@ public static function answerIsYes(bool $default = true): bool
280280
}
281281

282282
print 'Please try again';
283-
return self::answerIsYes($default);
283+
return self::answerIsYes();
284284
}
285285

286286
/**

0 commit comments

Comments
 (0)