Skip to content

Commit 80d4bc4

Browse files
committed
update some logic for group options
1 parent ca8a820 commit 80d4bc4

File tree

6 files changed

+66
-32
lines changed

6 files changed

+66
-32
lines changed

examples/Controller/HomeController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ protected function init(): void
4747
$this->addCommentsVar('internalFonts', implode(',', ArtFont::getInternalFonts()));
4848
}
4949

50+
/**
51+
* @return array
52+
*/
53+
protected function groupOptions(): array
54+
{
55+
return [
56+
'-c, --common' => 'This is a common option for all sub-commands',
57+
];
58+
}
59+
5060
protected function disabledCommands(): array
5161
{
5262
return ['disabled'];

examples/Controller/ShowController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public static function commandAliases(): array
3939
/**
4040
* @return array
4141
*/
42-
protected function commonOptions(): array
42+
protected function groupOptions(): array
4343
{
44-
return \array_merge(parent::commonOptions(), [
45-
'-c, --command' => 'This is a common option for all sub-commands',
46-
]);
44+
return [
45+
'-c, --common' => 'This is a common option for all sub-commands',
46+
];
4747
}
4848

4949
/**

src/AbstractApplication.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ abstract class AbstractApplication implements ApplicationInterface
6262
'publishAt' => '2017.03.24',
6363
'updateAt' => '2019.01.01',
6464
'rootPath' => '',
65+
'strictMode' => false,
6566
'hideRootPath' => true,
6667

6768
// 'timeZone' => 'Asia/Shanghai',
@@ -481,7 +482,7 @@ public function getConfig(): array
481482
}
482483

483484
/**
484-
* get config param value
485+
* Get config param value
485486
* @param null|string $name
486487
* @param null|string $default
487488
* @return array|string
@@ -491,6 +492,14 @@ public function getParam(string $name, $default = null)
491492
return $this->config[$name] ?? $default;
492493
}
493494

495+
/**
496+
* @return bool
497+
*/
498+
public function isStrictMode(): bool
499+
{
500+
return (bool)$this->config['strictMode'];
501+
}
502+
494503
/**
495504
* get current debug level value
496505
* @return int

src/AbstractHandler.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ abstract class AbstractHandler implements CommandHandlerInterface
6464
/** @var Application */
6565
protected $app;
6666

67-
/** @var array common options for all sub-commands */
68-
private $commonOptions;
69-
7067
/** @var InputDefinition|null */
7168
private $definition;
7269

@@ -110,7 +107,6 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
110107
$this->definition = $definition;
111108
}
112109

113-
$this->commonOptions = $this->commonOptions();
114110
$this->commentsVars = $this->annotationVars();
115111

116112
$this->init();
@@ -142,17 +138,6 @@ protected function createDefinition(): InputDefinition
142138
return $this->definition;
143139
}
144140

145-
/**
146-
* you can set common options for all sub-commands
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-
156141
/**
157142
* Provides parsable substitution variables for command annotations. Can be used in comments in commands
158143
* 为命令注解提供可解析的替换变量. 可以在命令的注释中使用
@@ -574,9 +559,11 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
574559
unset($help['Description:']);
575560
}
576561

577-
$help['Global Options:'] = FormatUtil::alignOptions(
578-
\array_merge(Application::getGlobalOptions(), $this->commonOptions)
579-
);
562+
$help['Group Options:'] = null;
563+
$help['Global Options:'] = FormatUtil::alignOptions(Application::getGlobalOptions());
564+
565+
$this->beforeRenderCommandHelp($help);
566+
580567
$this->output->mList($help, [
581568
'sepChar' => ' ',
582569
'lastNewline' => 0,
@@ -585,6 +572,10 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
585572
return 0;
586573
}
587574

575+
protected function beforeRenderCommandHelp(array &$help): void
576+
{
577+
}
578+
588579
/**************************************************************************
589580
* getter/setter methods
590581
**************************************************************************/
@@ -707,14 +698,6 @@ public function setDefinition(InputDefinition $definition): void
707698
$this->definition = $definition;
708699
}
709700

710-
/**
711-
* @return array
712-
*/
713-
public function getCommonOptions(): array
714-
{
715-
return $this->commonOptions;
716-
}
717-
718701
/**
719702
* @return array
720703
*/

src/Command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ abstract class Command extends AbstractHandler implements CommandInterface
5252
*/
5353
protected function showHelp(): bool
5454
{
55+
// help info has been build by input definition.
5556
if (true === parent::showHelp()) {
5657
return true;
5758
}

src/Controller.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
4949
/** @var string */
5050
protected $notFoundCallback = 'notFound';
5151

52+
/** @var array Common options for all sub-commands in the group */
53+
private $groupOptions = [];
54+
5255
/** @var array From disabledCommands() */
53-
protected $disabledCommands = [];
56+
private $disabledCommands = [];
5457

5558
/**
5659
* define command alias map
@@ -70,12 +73,26 @@ protected function init(): void
7073
// save to property
7174
$this->disabledCommands = $list ? \array_flip($list) : [];
7275
self::$commandAliases = static::commandAliases();
76+
$this->groupOptions = $this->groupOptions();
7377

7478
if (!$this->actionSuffix) {
7579
$this->actionSuffix = 'Command';
7680
}
7781
}
7882

83+
/**
84+
* Options for the group all commands.
85+
* you can set common options for all sub-commands
86+
*
87+
* @return array
88+
*/
89+
protected function groupOptions(): array
90+
{
91+
return [
92+
// '--skip-invalid' => 'Whether ignore invalid arguments and options, when use input definition',
93+
];
94+
}
95+
7996
/**
8097
* define disabled command list.
8198
* @return array
@@ -184,13 +201,27 @@ final public function execute($input, $output)
184201
*/
185202
protected function showHelp(): bool
186203
{
204+
// help info has been build by input definition.
187205
if (true === parent::showHelp()) {
188206
return true;
189207
}
190208

191209
return $this->helpCommand();
192210
}
193211

212+
protected function beforeRenderCommandHelp(array &$help): void
213+
{
214+
$help['Group Options:'] = FormatUtil::alignOptions($this->groupOptions);
215+
}
216+
217+
/**
218+
* @return array
219+
*/
220+
public function getGroupOptions(): array
221+
{
222+
return $this->groupOptions;
223+
}
224+
194225
/**
195226
* Show help of the controller command group or specified command action
196227
* @usage <info>{name}:[command] -h</info> OR <info>{command} [command]</info> OR <info>{name} [command] -h</info>

0 commit comments

Comments
 (0)