1313use Inhere \Console \Handler \AbstractHandler ;
1414use ReflectionException ;
1515use Toolkit \PFlag \FlagsParser ;
16+ use function array_shift ;
1617
1718/**
1819 * Class Command
@@ -38,22 +39,17 @@ abstract class Command extends AbstractHandler implements CommandInterface
3839 */
3940 protected ?Controller $ group = null ;
4041
41- /**
42- * @var Command|null
43- */
44- protected ?Command $ parent = null ;
45-
4642 protected function init (): void
4743 {
48- $ this ->commandName = self :: getName ();
44+ $ this ->commandName = $ this -> getRealName ();
4945
5046 parent ::init ();
5147 }
5248
5349 /**
5450 * @return array
5551 */
56- public function getArguments (): array
52+ protected function getArguments (): array
5753 {
5854 return [];
5955 }
@@ -63,12 +59,13 @@ public function getArguments(): array
6359 */
6460 protected function beforeInitFlagsParser (FlagsParser $ fs ): void
6561 {
62+ $ fs ->addArgsByRules ($ this ->getArguments ());
6663 $ fs ->setStopOnFistArg (false );
6764
6865 // old mode: options and arguments at method annotations
69- if ($ this ->compatible ) {
70- $ fs ->setSkipOnUndefined (true );
71- }
66+ // if ($this->compatible) {
67+ // $fs->setSkipOnUndefined(true);
68+ // }
7269 }
7370
7471 /**
@@ -93,31 +90,24 @@ protected function afterInitFlagsParser(FlagsParser $fs): void
9390 }
9491
9592 /**
96- * @param Command $parent
97- */
98- public function setParent (Command $ parent ): void
99- {
100- $ this ->parent = $ parent ;
101- }
102-
103- /**
104- * @return $this
93+ * @param array $args
94+ *
95+ * @return mixed
10596 */
106- public function getRoot ( ): Command
97+ protected function doRun ( array $ args ): mixed
10798 {
108- if ($ this ->parent ) {
109- return $ this ->parent ->getRoot ();
99+ // if input sub-command name
100+ if (isset ($ args [0 ])) {
101+ $ first = $ args [0 ];
102+ $ rName = $ this ->resolveAlias ($ first );
103+
104+ if ($ this ->isSub ($ rName )) {
105+ array_shift ($ args );
106+ return $ this ->dispatchSub ($ rName , $ args );
107+ }
110108 }
111109
112- return $ this ;
113- }
114-
115- /**
116- * @return Command|null
117- */
118- public function getParent (): ?Command
119- {
120- return $ this ->parent ;
110+ return parent ::doRun ($ args );
121111 }
122112
123113 /**
0 commit comments