@@ -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 */
0 commit comments