99namespace Toolkit \Cli ;
1010
1111/**
12- * Class LiteApp - A lite CLI Application
12+ * Class App - A lite CLI Application
1313 * @package Inhere\Console
1414 */
15- class LiteApp
15+ class App
1616{
17- /****************************************************************************
18- * simple cli support
19- ****************************************************************************/
17+ /** @var string Current dir */
18+ private $ pwd ;
2019
2120 /**
22- * parse from `name=val var2=val2`
23- * @var array
21+ * @var array Parsed from `arg0 name=val var2=val2`
2422 */
2523 private $ args = [];
2624
2725 /**
28- * parse from `--name=val --var2=val2 -d`
29- * @var array
26+ * @var array Parsed from `--name=val --var2=val2 -d`
3027 */
3128 private $ opts = [];
3229
@@ -37,25 +34,42 @@ class LiteApp
3734 private $ command = '' ;
3835
3936 /**
40- * user add commands
41- * @var array
37+ * @var array User add commands
4238 */
4339 private $ commands = [];
4440
4541 /**
46- * description message for the command
47- * @var array
42+ * @var array Description message for the commands
4843 */
4944 private $ messages = [];
5045
46+ /** @var int */
47+ private $ keyWidth = 12 ;
48+
49+ /**
50+ * Class constructor.
51+ * @param array|null $argv
52+ */
53+ public function __construct (array $ argv = null )
54+ {
55+ // get current dir
56+ $ this ->pwd = \getcwd ();
57+
58+ // parse cli argv
59+ $ argv = $ argv ?? (array )$ _SERVER ['argv ' ];
60+ // get script file
61+ $ this ->script = \array_shift ($ argv );
62+ // parse flags
63+ [$ this ->args , $ this ->opts ] = Flags::simpleParseArgv ($ argv );
64+
65+ }
66+
5167 /**
5268 * @param bool $exit
5369 * @throws \InvalidArgumentException
5470 */
5571 public function run (bool $ exit = true )
5672 {
57- $ this ->parseCliArgv ();
58-
5973 if (isset ($ this ->args [0 ])) {
6074 $ this ->command = $ this ->args [0 ];
6175 unset($ this ->args [0 ]);
@@ -71,7 +85,8 @@ public function run(bool $exit = true)
7185 public function dispatch (bool $ exit = true )
7286 {
7387 if (!$ command = $ this ->command ) {
74- $ this ->showCommands ();
88+ $ this ->displayHelp ();
89+ return ;
7590 }
7691
7792 $ status = 0 ;
@@ -80,7 +95,7 @@ public function dispatch(bool $exit = true)
8095 if (isset ($ this ->commands [$ command ])) {
8196 $ status = $ this ->runHandler ($ command , $ this ->commands [$ command ]);
8297 } else {
83- $ this ->showCommands ("The command {$ command } not exists! " );
98+ $ this ->displayHelp ("The command {$ command } not exists! " );
8499 }
85100 } catch (\Throwable $ e ) {
86101 $ status = $ this ->handleException ($ e );
@@ -152,52 +167,19 @@ protected function handleException(\Throwable $e): int
152167 }
153168
154169 /**
155- * parseCliArgv
156- */
157- public function parseCliArgv ()
158- {
159- /** @var array $argv */
160- $ argv = $ _SERVER ['argv ' ];
161- $ this ->script = array_shift ($ argv );
162-
163- foreach ($ argv as $ key => $ value ) {
164- // opts
165- if (strpos ($ value , '- ' ) === 0 ) {
166- $ value = trim ($ value , '- ' );
167-
168- if (!$ value ) {
169- continue ;
170- }
171-
172- if (strpos ($ value , '= ' )) {
173- list ($ n , $ v ) = explode ('= ' , $ value );
174- $ this ->opts [$ n ] = $ v ;
175- } else {
176- $ this ->opts [$ value ] = true ;
177- }
178- } elseif (strpos ($ value , '= ' )) {
179- list ($ n , $ v ) = explode ('= ' , $ value );
180- $ this ->args [$ n ] = $ v ;
181- } else {
182- $ this ->args [] = $ value ;
183- }
184- }
185- }
186-
187- /**
188- * @param string $command
189- * @param string|\Closure $handler
190- * @param string $description
170+ * @param string $command
171+ * @param callable $handler
172+ * @param string $description
191173 * @throws \InvalidArgumentException
192174 */
193- public function addCommand (string $ command , $ handler , $ description = '' )
175+ public function addCommand (string $ command , callable $ handler , string $ description = '' )
194176 {
195177 if (!$ command || !$ handler ) {
196178 throw new \InvalidArgumentException ('Invalid arguments ' );
197179 }
198180
199181 $ this ->commands [$ command ] = $ handler ;
200- $ this ->messages [$ command ] = trim ($ description );
182+ $ this ->messages [$ command ] = \ trim ($ description );
201183 }
202184
203185 /**
@@ -210,9 +192,9 @@ public function commands(array $commands)
210192 $ des = '' ;
211193
212194 if (\is_array ($ handler )) {
213- $ conf = array_values ($ handler );
195+ $ conf = \ array_values ($ handler );
214196 $ handler = $ conf [0 ];
215- $ des = $ conf [1 ] ?? '' ;
197+ $ des = $ conf [1 ] ?? '' ;
216198 }
217199
218200 $ this ->addCommand ($ command , $ handler , $ des );
@@ -226,28 +208,29 @@ public function commands(array $commands)
226208 /**
227209 * @param string $err
228210 */
229- public function showCommands ( $ err = '' )
211+ public function displayHelp ( string $ err = '' )
230212 {
231213 if ($ err ) {
232214 echo Color::render ("<red>ERROR</red>: $ err \n\n" );
233215 }
234216
235217 $ commandWidth = 12 ;
218+ // help
236219 $ help = "Welcome to the Lite Console Application. \n\n<comment>Available Commands:</comment> \n" ;
237220
238221 foreach ($ this ->messages as $ command => $ desc ) {
239222 $ command = str_pad ($ command , $ commandWidth , ' ' );
240- $ desc = $ desc ?: 'No description for the command ' ;
241- $ help .= " $ command $ desc \n" ;
223+ $ desc = $ desc ?: 'No description for the command ' ;
224+ $ help .= " $ command $ desc \n" ;
242225 }
243226
244227 echo Color::render ($ help ) . PHP_EOL ;
245228 exit (0 );
246229 }
247230
248231 /**
249- * @param string $name
250- * @param mixed $default
232+ * @param string|int $name
233+ * @param mixed $default
251234 * @return mixed|null
252235 */
253236 public function getArg ($ name , $ default = null )
@@ -260,7 +243,7 @@ public function getArg($name, $default = null)
260243 * @param mixed $default
261244 * @return mixed|null
262245 */
263- public function getOpt ($ name , $ default = null )
246+ public function getOpt (string $ name , $ default = null )
264247 {
265248 return $ this ->opts [$ name ] ?? $ default ;
266249 }
@@ -344,7 +327,7 @@ public function getCommands(): array
344327 /**
345328 * @param array $commands
346329 */
347- public function setCommands (array $ commands )
330+ public function setCommands (array $ commands ): void
348331 {
349332 $ this ->commands = $ commands ;
350333 }
@@ -360,9 +343,33 @@ public function getMessages(): array
360343 /**
361344 * @param array $messages
362345 */
363- public function setMessages (array $ messages )
346+ public function setMessages (array $ messages ): void
364347 {
365348 $ this ->messages = $ messages ;
366349 }
367350
351+ /**
352+ * @return int
353+ */
354+ public function getKeyWidth (): int
355+ {
356+ return $ this ->keyWidth ;
357+ }
358+
359+ /**
360+ * @param int $keyWidth
361+ */
362+ public function setKeyWidth (int $ keyWidth ): void
363+ {
364+ $ this ->keyWidth = $ keyWidth > 1 ? $ keyWidth : 12 ;
365+ }
366+
367+ /**
368+ * @return string
369+ */
370+ public function getPwd (): string
371+ {
372+ return $ this ->pwd ;
373+ }
374+
368375}
0 commit comments