13
13
use Inhere \Console \Handler \AbstractHandler ;
14
14
use ReflectionException ;
15
15
use Toolkit \PFlag \FlagsParser ;
16
+ use function array_shift ;
16
17
17
18
/**
18
19
* Class Command
@@ -38,22 +39,17 @@ abstract class Command extends AbstractHandler implements CommandInterface
38
39
*/
39
40
protected ?Controller $ group = null ;
40
41
41
- /**
42
- * @var Command|null
43
- */
44
- protected ?Command $ parent = null ;
45
-
46
42
protected function init (): void
47
43
{
48
- $ this ->commandName = self :: getName ();
44
+ $ this ->commandName = $ this -> getRealName ();
49
45
50
46
parent ::init ();
51
47
}
52
48
53
49
/**
54
50
* @return array
55
51
*/
56
- public function getArguments (): array
52
+ protected function getArguments (): array
57
53
{
58
54
return [];
59
55
}
@@ -63,12 +59,13 @@ public function getArguments(): array
63
59
*/
64
60
protected function beforeInitFlagsParser (FlagsParser $ fs ): void
65
61
{
62
+ $ fs ->addArgsByRules ($ this ->getArguments ());
66
63
$ fs ->setStopOnFistArg (false );
67
64
68
65
// 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
+ // }
72
69
}
73
70
74
71
/**
@@ -93,31 +90,24 @@ protected function afterInitFlagsParser(FlagsParser $fs): void
93
90
}
94
91
95
92
/**
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
105
96
*/
106
- public function getRoot ( ): Command
97
+ protected function doRun ( array $ args ): mixed
107
98
{
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
+ }
110
108
}
111
109
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 );
121
111
}
122
112
123
113
/**
0 commit comments