99namespace Inhere \Console ;
1010
1111use Closure ;
12+ use Inhere \Console \Contract \ApplicationInterface ;
1213use Inhere \Console \Contract \ControllerInterface ;
1314use Inhere \Console \IO \Input ;
1415use Inhere \Console \IO \Output ;
1718use RuntimeException ;
1819use SplFileInfo ;
1920use Throwable ;
21+ use Toolkit \PFlag \SFlags ;
2022use Toolkit \Stdlib \Helper \DataHelper ;
2123use function array_unshift ;
2224use function class_exists ;
@@ -40,8 +42,8 @@ class Application extends AbstractApplication
4042 /**
4143 * Class constructor.
4244 *
43- * @param array $config
44- * @param Input|null $input
45+ * @param array $config
46+ * @param Input|null $input
4547 * @param Output|null $output
4648 */
4749 public function __construct (array $ config = [], Input $ input = null , Output $ output = null )
@@ -58,46 +60,40 @@ public function __construct(array $config = [], Input $input = null, Output $out
5860 /**
5961 * {@inheritdoc}
6062 */
61- public function controller (string $ name , $ class = null , $ option = null )
63+ public function controller (string $ name , $ class = null , array $ config = []): ApplicationInterface
6264 {
63- if (is_string ($ option )) {
64- $ option = [
65- 'description ' => $ option ,
66- ];
67- }
68-
69- $ this ->logf (Console::VERB_CRAZY , 'load group controller: %s ' , $ name );
70- $ this ->router ->addGroup ($ name , $ class , (array )$ option );
65+ $ this ->logf (Console::VERB_CRAZY , 'register group controller: %s ' , $ name );
66+ $ this ->router ->addGroup ($ name , $ class , $ config );
7167
7268 return $ this ;
7369 }
7470
7571 /**
7672 * Add group/controller
7773 *
78- * @param string $name
74+ * @param string $name
7975 * @param string|ControllerInterface|null $class The controller class
80- * @param null| array|string $option
76+ * @param array $config
8177 *
8278 * @return Application|Contract\ApplicationInterface
8379 * @see controller()
8480 */
85- public function addGroup (string $ name , $ class = null , $ option = null )
81+ public function addGroup (string $ name , $ class = null , array $ config = []): ApplicationInterface
8682 {
87- return $ this ->controller ($ name , $ class , $ option );
83+ return $ this ->controller ($ name , $ class , $ config );
8884 }
8985
9086 /**
91- * @param string $name
87+ * @param string $name
9288 * @param string|ControllerInterface|null $class The controller class
93- * @param null| array|string $option
89+ * @param array $config
9490 *
9591 * @return Application|Contract\ApplicationInterface
9692 * @see controller()
9793 */
98- public function addController (string $ name , $ class = null , $ option = null )
94+ public function addController (string $ name , $ class = null , array $ config = []): ApplicationInterface
9995 {
100- return $ this ->controller ($ name , $ class , $ option );
96+ return $ this ->controller ($ name , $ class , $ config );
10197 }
10298
10399 /**
@@ -134,16 +130,10 @@ public function addControllers(array $controllers): void
134130 /**
135131 * {@inheritdoc}
136132 */
137- public function command (string $ name , $ handler = null , $ option = null )
133+ public function command (string $ name , $ handler = null , array $ config = [] )
138134 {
139- if (is_string ($ option )) {
140- $ option = [
141- 'description ' => $ option ,
142- ];
143- }
144-
145- $ this ->logf (Console::VERB_CRAZY , 'load application command: %s ' , $ name );
146- $ this ->router ->addCommand ($ name , $ handler , (array )$ option );
135+ $ this ->logf (Console::VERB_CRAZY , 'register alone command: %s ' , $ name );
136+ $ this ->router ->addCommand ($ name , $ handler , $ config );
147137
148138 return $ this ;
149139 }
@@ -152,15 +142,15 @@ public function command(string $name, $handler = null, $option = null)
152142 * add command
153143 *
154144 * @param string $name
155- * @param null $handler
156- * @param null $option
145+ * @param null|mixed $handler
146+ * @param array $config
157147 *
158148 * @return Application
159149 * @see command()
160150 */
161- public function addCommand (string $ name , $ handler = null , $ option = null ): self
151+ public function addCommand (string $ name , $ handler = null , array $ config = [] ): self
162152 {
163- return $ this ->command ($ name , $ handler , $ option );
153+ return $ this ->command ($ name , $ handler , $ config );
164154 }
165155
166156 /**
@@ -260,7 +250,7 @@ protected function getFileFilter(): callable
260250
261251 /**
262252 * @param string $name command name or command ID or command path.
263- * @param array $args
253+ * @param array $args
264254 *
265255 * @return int|mixed
266256 * @throws Throwable
@@ -322,21 +312,27 @@ public function dispatch(string $name, array $args = [])
322312 /**
323313 * run a independent command
324314 *
325- * @param string $name Command name
315+ * @param string $name Command name
326316 * @param Closure|string $handler Command class or handler func
327- * @param array $options
328- * @param array $args
317+ * @param array $options
318+ * @param array $args
329319 *
330320 * @return mixed
331321 * @throws Throwable
332322 */
333323 protected function runCommand (string $ name , $ handler , array $ options , array $ args )
334324 {
335325 if (is_object ($ handler ) && method_exists ($ handler , '__invoke ' )) {
336- if ($ this ->input ->getSameOpt (['h ' , 'help ' ])) {
337- $ desc = $ options ['description ' ] ?? 'No command description message ' ;
326+ $ fs = SFlags::new ();
327+ $ fs ->addOptsByRules (GlobalOption::getAloneOptions ());
328+ $ desc = $ options ['desc ' ] ?? 'No command description message ' ;
329+ $ fs ->setDesc ($ desc );
338330
339- return $ this ->output ->write ($ desc );
331+ // save to input object
332+ $ this ->input ->setFs ($ fs );
333+
334+ if (!$ fs ->parse ($ args )) {
335+ return 0 ; // render help
340336 }
341337
342338 $ result = $ handler ($ this ->input , $ this ->output );
@@ -364,18 +360,20 @@ protected function runCommand(string $name, $handler, array $options, array $arg
364360 * Execute an action in a group command(controller)
365361 *
366362 * @param array $info Matched route info
363+ *
367364 * @psalm-param array{action: string} $info Matched route info
365+ *
368366 * @param array $options
369367 * @param array $args
370- * @param bool $detachedRun
368+ * @param bool $detachedRun
371369 *
372370 * @return mixed
373371 * @throws Throwable
374372 */
375373 protected function runAction (array $ info , array $ options , array $ args , bool $ detachedRun = false )
376374 {
377375 $ controller = $ this ->createController ($ info );
378- $ controller ::setDesc ($ options ['description ' ] ?? '' );
376+ $ controller ::setDesc ($ options ['desc ' ] ?? '' );
379377
380378 if ($ detachedRun ) {
381379 $ controller ->setDetached ();
0 commit comments