1111use Generator ;
1212use Inhere \Console \Contract \ControllerInterface ;
1313use Inhere \Console \IO \Input ;
14+ use Inhere \Console \IO \InputDefinition ;
1415use Inhere \Console \IO \Output ;
1516use Inhere \Console \Util \FormatUtil ;
1617use Inhere \Console \Util \Helper ;
2021use ReflectionObject ;
2122use RuntimeException ;
2223use Toolkit \Cli \ColorTag ;
23- use Toolkit \Stdlib \Util \PhpDoc ;
2424use Toolkit \Stdlib \Str ;
25+ use Toolkit \Stdlib \Util \PhpDoc ;
2526use function array_flip ;
2627use function array_keys ;
2728use function array_merge ;
@@ -67,6 +68,7 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
6768
6869 /**
6970 * Action name, no suffix.
71+ * eg: updateCommand() -> action: 'update'
7072 *
7173 * @var string
7274 */
@@ -104,6 +106,21 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
104106 */
105107 private $ disabledCommands = [];
106108
109+ /**
110+ * Metadata for sub-commands. such as: desc, alias
111+ * Notice: you must add metadata on `init()`
112+ *
113+ * [
114+ * 'command real name' => [
115+ * 'desc' => 'sub command description',
116+ * 'alias' => [],
117+ * ],
118+ * ],
119+ *
120+ * @var array
121+ */
122+ protected $ commandMetas = [];
123+
107124 /**
108125 * Define command alias mapping. please rewrite it on sub-class.
109126 *
@@ -177,11 +194,11 @@ public function run(string $command = '')
177194 }
178195
179196 $ this ->action = Str::camelCase ($ this ->getRealCommandName ($ command ));
180-
181197 if (!$ this ->action ) {
182198 return $ this ->showHelp ();
183199 }
184200
201+ // do running
185202 return parent ::run ($ command );
186203 }
187204
@@ -190,14 +207,32 @@ public function run(string $command = '')
190207 */
191208 protected function configure (): void
192209 {
193- // eg. indexConfigure() for indexCommand()
210+ // eg. use ` indexConfigure()` for ` indexCommand()`
194211 $ method = $ this ->action . self ::CONFIGURE_SUFFIX ;
195212
196213 if (method_exists ($ this , $ method )) {
197214 $ this ->$ method ($ this ->input );
198215 }
199216 }
200217
218+ /**
219+ * @return InputDefinition
220+ */
221+ protected function createDefinition (): InputDefinition
222+ {
223+ if (!$ this ->definition ) {
224+ $ this ->definition = new InputDefinition ();
225+
226+ // if have been set desc for the sub-command
227+ $ cmdDesc = $ this ->commandMetas [$ this ->action ]['desc ' ] ?? '' ;
228+ if ($ cmdDesc ) {
229+ $ this ->definition ->setDescription ($ cmdDesc );
230+ }
231+ }
232+
233+ return $ this ->definition ;
234+ }
235+
201236 /**
202237 * Run command action in the group
203238 *
@@ -271,6 +306,9 @@ protected function showHelp(): bool
271306 return $ this ->helpCommand () === 0 ;
272307 }
273308
309+ /**
310+ * @param array $help
311+ */
274312 protected function beforeRenderCommandHelp (array &$ help ): void
275313 {
276314 $ help ['Group Options: ' ] = FormatUtil::alignOptions ($ this ->groupOptions );
@@ -305,7 +343,7 @@ final public function helpCommand(): int
305343 {
306344 $ action = $ this ->action ;
307345
308- // For all sub-commands of the controller
346+ // Not input action, for all sub-commands of the controller
309347 if (!$ action && !($ action = $ this ->getFirstArg ())) {
310348 $ this ->showCommandList ();
311349 return 0 ;
@@ -354,12 +392,15 @@ final public function showCommandList(): void
354392 $ showDisabled = (bool )$ this ->getOpt ('show-disabled ' , false );
355393 $ defaultDes = 'No description message ' ;
356394
395+ /**
396+ * @var $cmd string The command name.
397+ */
357398 foreach ($ this ->getAllCommandMethods ($ ref ) as $ cmd => $ m ) {
358399 if (!$ cmd ) {
359400 continue ;
360401 }
361402
362- $ desc = $ defaultDes ;
403+ $ desc = $ this -> getCommandMeta ( ' desc ' , $ defaultDes, $ cmd ) ;
363404 if ($ phpDoc = $ m ->getDocComment ()) {
364405 $ desc = PhpDoc::firstLine ($ phpDoc );
365406 }
@@ -630,4 +671,37 @@ public function setDelimiter(string $delimiter): void
630671 {
631672 $ this ->delimiter = $ delimiter ;
632673 }
674+
675+ /**
676+ * @return array
677+ */
678+ public function getCommandMetas (): array
679+ {
680+ return $ this ->commandMetas ;
681+ }
682+
683+ /**
684+ * @param string $command
685+ * @param array $meta eg: ['desc' => '', 'alias' => []]
686+ */
687+ public function setCommandMeta (string $ command , array $ meta ): void
688+ {
689+ if ($ command ) {
690+ $ this ->commandMetas [$ command ] = $ meta ;
691+ }
692+ }
693+
694+ /**
695+ * @param string $key
696+ * @param null $default
697+ * @param string $command if not set, will use $this->action
698+ *
699+ * @return mixed|null
700+ */
701+ public function getCommandMeta (string $ key , $ default = null , string $ command = '' )
702+ {
703+ $ action = $ command ?: $ this ->action ;
704+
705+ return $ this ->commandMetas [$ action ][$ key ] ?? $ default ;
706+ }
633707}
0 commit comments