@@ -39,7 +39,7 @@ class App
39
39
{
40
40
/** @var self */
41
41
public static $ i ;
42
-
42
+
43
43
private const COMMAND_CONFIG = [
44
44
'desc ' => '' ,
45
45
'usage ' => '' ,
@@ -49,6 +49,14 @@ class App
49
49
/** @var string Current dir */
50
50
private $ pwd ;
51
51
52
+ /**
53
+ * @var array
54
+ */
55
+ private $ metas = [
56
+ 'name ' => 'My application ' ,
57
+ 'desc ' => 'My command line application ' ,
58
+ ];
59
+
52
60
/**
53
61
* @var array Parsed from `arg0 name=val var2=val2`
54
62
*/
@@ -87,18 +95,22 @@ class App
87
95
/**
88
96
* Class constructor.
89
97
*
90
- * @param array|null $argv
98
+ * @param array $config
99
+ * @param array $argv
91
100
*/
92
- public function __construct (array $ argv = null )
101
+ public function __construct (array $ config = [], array $ argv = null )
93
102
{
94
103
// save self
95
104
self ::$ i = $ this ;
96
-
105
+
97
106
// get current dir
98
107
$ this ->pwd = getcwd ();
99
108
100
109
// parse cli argv
101
110
$ argv = $ argv ?? (array )$ _SERVER ['argv ' ];
111
+ if ($ config ) {
112
+ $ this ->setMetas ($ config );
113
+ }
102
114
103
115
// get script file
104
116
$ this ->script = array_shift ($ argv );
@@ -188,7 +200,7 @@ public function stop($code = 0): void
188
200
189
201
/**
190
202
* @param string $command
191
- * @param $handler
203
+ * @param mixed $handler
192
204
*
193
205
* @return mixed
194
206
* @throws InvalidArgumentException
@@ -246,6 +258,10 @@ protected function handleException(Throwable $e): int
246
258
*/
247
259
public function addByConfig (callable $ handler , array $ config ): void
248
260
{
261
+ if (empty ($ config ['name ' ]) || !$ handler ) {
262
+ throw new InvalidArgumentException ('Invalid arguments for add command ' );
263
+ }
264
+
249
265
$ this ->addCommand ($ config ['name ' ], $ handler , $ config );
250
266
}
251
267
@@ -324,17 +340,21 @@ public function displayHelp(string $err = ''): void
324
340
}
325
341
326
342
// help
327
- $ len = $ this ->keyWidth ;
328
- $ help = "Welcome to the Lite Console Application. \n\n<comment>Available Commands:</comment> \n" ;
343
+ $ desc = ucfirst ($ this ->metas ['desc ' ]);
344
+ $ usage = "<cyan> {$ this ->script } COMMAND -h</cyan> " ;
345
+
346
+ $ help = "$ desc \n<comment>Usage:</comment> $ usage \n<comment>Commands:</comment> \n" ;
329
347
$ data = $ this ->messages ;
330
348
ksort ($ data );
331
349
332
350
foreach ($ data as $ command => $ item ) {
333
- $ command = str_pad ($ command , $ len , ' ' );
351
+ $ command = str_pad ($ command , $ this -> keyWidth , ' ' );
334
352
$ desc = $ item ['desc ' ] ? ucfirst ($ item ['desc ' ]) : 'No description for the command ' ;
335
- $ help .= " $ command $ desc \n" ;
353
+ $ help .= " <green> $ command</green> $ desc \n" ;
336
354
}
337
355
356
+ $ help .= "\nFor command usage please run: <cyan> {$ this ->script } COMMAND -h</cyan> " ;
357
+
338
358
echo Color::render ($ help ) . PHP_EOL ;
339
359
exit (0 );
340
360
}
@@ -550,14 +570,6 @@ public function getMessages(): array
550
570
return $ this ->messages ;
551
571
}
552
572
553
- /**
554
- * @param array $messages
555
- */
556
- public function setMessages (array $ messages ): void
557
- {
558
- $ this ->messages = $ messages ;
559
- }
560
-
561
573
/**
562
574
* @return int
563
575
*/
@@ -582,4 +594,19 @@ public function getPwd(): string
582
594
return $ this ->pwd ;
583
595
}
584
596
597
+ /**
598
+ * @return array
599
+ */
600
+ public function getMetas (): array
601
+ {
602
+ return $ this ->metas ;
603
+ }
604
+
605
+ /**
606
+ * @param array $metas
607
+ */
608
+ public function setMetas (array $ metas ): void
609
+ {
610
+ $ this ->metas = array_merge ($ this ->metas , $ metas );
611
+ }
585
612
}
0 commit comments