@@ -93,10 +93,14 @@ public function __construct(array $argv = null)
9393
9494 // parse cli argv
9595 $ argv = $ argv ?? (array )$ _SERVER ['argv ' ];
96+
9697 // get script file
9798 $ this ->script = array_shift ($ argv );
99+
98100 // parse flags
99- [$ this ->args , $ this ->opts ] = Flags::parseArgv ($ argv , ['mergeOpts ' => true ]);
101+ [$ this ->args , $ this ->opts ] = Flags::parseArgv ($ argv , [
102+ 'mergeOpts ' => true
103+ ]);
100104 }
101105
102106 /**
@@ -106,14 +110,35 @@ public function __construct(array $argv = null)
106110 */
107111 public function run (bool $ exit = true ): void
108112 {
109- if (isset ($ this ->args [0 ])) {
110- $ this ->command = $ this ->args [0 ];
111- unset($ this ->args [0 ]);
112- }
113+ $ this ->findCommand ();
113114
114115 $ this ->dispatch ($ exit );
115116 }
116117
118+ /**
119+ * find command name. it is first argument.
120+ */
121+ protected function findCommand (): void
122+ {
123+ if (!isset ($ this ->args [0 ])) {
124+ return ;
125+ }
126+
127+ $ newArgs = [];
128+
129+ foreach ($ this ->args as $ key => $ value ) {
130+ if ($ key === 0 ) {
131+ $ this ->command = trim ($ value );
132+ } elseif (is_int ($ key )) {
133+ $ newArgs [] = $ value ;
134+ } else {
135+ $ newArgs [$ key ] = $ value ;
136+ }
137+ }
138+
139+ $ this ->args = $ newArgs ;
140+ }
141+
117142 /**
118143 * @param bool $exit
119144 *
@@ -127,7 +152,7 @@ public function dispatch(bool $exit = true): void
127152 }
128153
129154 if (!isset ($ this ->commands [$ command ])) {
130- $ this ->displayHelp ("The command {$ command } not exists! " );
155+ $ this ->displayHelp ("The command ' {$ command }' is not exists! " );
131156 return ;
132157 }
133158
@@ -201,9 +226,10 @@ protected function handleException(Throwable $e): int
201226 }
202227
203228 $ code = $ e ->getCode () !== 0 ? $ e ->getCode () : -1 ;
229+ $ eTpl = "Exception(%d): %s \nFile: %s(Line %d) \nTrace: \n%s \n" ;
204230
205- printf ( " Exception(%d): %s \n File: %s(Line %d) \n Trace: \n %s \n" , $ code , $ e -> getMessage (), $ e -> getFile (),
206- $ e ->getLine (), $ e ->getTraceAsString ());
231+ // print exception message
232+ printf ( $ eTpl , $ code , $ e -> getMessage (), $ e -> getFile (), $ e ->getLine (), $ e ->getTraceAsString ());
207233
208234 return $ code ;
209235 }
0 commit comments