File tree Expand file tree Collapse file tree 3 files changed +75
-7
lines changed Expand file tree Collapse file tree 3 files changed +75
-7
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,21 @@ $script = \array_shift($argv);
31
31
list($args, $shortOpts, $longOpts) = Flags::parseArgv($argv);
32
32
```
33
33
34
+ ## Build CLI application
35
+
36
+ You can quickly build an simple CLI application:
37
+
38
+ ``` php
39
+ use Toolkit\Cli\App;
40
+
41
+ // create app instance
42
+ $app = new App([
43
+ 'desc' => 'this is my cli application',
44
+ ]);
45
+
46
+
47
+ ```
48
+
34
49
## PHP file highlight
35
50
36
51
> This is inspire jakub-onderka/php-console-highlighter
Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env php
2
2
<?php
3
3
4
+ use Toolkit \Cli \App ;
5
+
4
6
define ('BASE_PATH ' , dirname (__DIR__ ));
5
7
6
8
require dirname (__DIR__ ) . '/test/boot.php ' ;
7
9
8
10
// create app instance
9
- $ app = new Toolkit \Cli \App ;
11
+ $ app = new App ([
12
+ 'desc ' => 'this is my cli application ' ,
13
+ ]);
10
14
11
15
// register commands
16
+
17
+ // use closure
12
18
$ app ->addCommand ('test ' , function ($ app ) {
13
19
echo "args: \n" ;
14
20
/** @var Toolkit\Cli\App $app */
21
+ /** @noinspection ForgottenDebugOutputInspection */
15
22
print_r ($ app ->getArgs ());
16
23
17
- }, 'the description text for the command: test ' );
24
+ }, [
25
+ 'desc ' => 'the description text for the command: test ' ,
26
+ ]);
27
+
28
+ // Use an object
29
+ $ app ->addObject (new class
30
+ {
31
+ public function getHelpConfig (): array
32
+ {
33
+ $ help = <<<STR
34
+ Options:
35
+ --info Output some information
36
+
37
+ Example:
38
+ {{command}}
39
+
40
+ STR ;
41
+
42
+ return [
43
+ 'name ' => 'list ' ,
44
+ 'desc ' => 'list all swoft components in src/ dir ' ,
45
+ 'help ' => $ help ,
46
+ ];
47
+ }
18
48
19
- $ app ->addCommand ('test1 ' , function () {
20
- echo "hello \n" ;
21
- }, 'the description text for the command: test1 ' );
49
+ public function __invoke (App $ app )
50
+ {
51
+ echo "hello \n" ;
52
+ }
53
+ });
22
54
23
55
// run
24
56
$ app ->run ();
Original file line number Diff line number Diff line change 23
23
use function is_string ;
24
24
use function ksort ;
25
25
use function method_exists ;
26
+ use function rtrim ;
26
27
use function str_pad ;
27
28
use function strlen ;
28
29
use function strpos ;
@@ -252,6 +253,25 @@ protected function handleException(Throwable $e): int
252
253
return $ code ;
253
254
}
254
255
256
+ /**
257
+ * @param callable $handler
258
+ * @param array $config
259
+ */
260
+ public function addObject (callable $ handler , array $ config = []): void
261
+ {
262
+ if (is_object ($ handler ) && method_exists ($ handler , '__invoke ' )) {
263
+ // has config method
264
+ if (method_exists ($ handler , 'getHelpConfig ' )) {
265
+ $ config = $ handler ->getHelpConfig ();
266
+ }
267
+
268
+ $ this ->addByConfig ($ handler , $ config );
269
+ return ;
270
+ }
271
+
272
+ throw new InvalidArgumentException ('Command handler must be an object and has method: __invoke ' );
273
+ }
274
+
255
275
/**
256
276
* @param callable $handler
257
277
* @param array $config
@@ -377,11 +397,12 @@ public function displayCommandHelp(string $name): void
377
397
];
378
398
} else {
379
399
$ checkVar = true ;
380
- $ userHelp = $ config ['help ' ];
400
+ $ userHelp = rtrim ( $ config ['help ' ], "\n" ) ;
381
401
402
+ $ usage = $ config ['usage ' ] ?: $ usage ;
382
403
$ nodes = [
383
404
ucfirst ($ config ['desc ' ]),
384
- "<comment>Usage:</comment> \n " . ( $ config [ ' usage ' ] ?: $ usage ) ,
405
+ "<comment>Usage:</comment> \n $ usage\n" ,
385
406
$ userHelp ? $ userHelp . "\n" : ''
386
407
];
387
408
}
You can’t perform that action at this time.
0 commit comments