Skip to content

Commit 4ed2ae8

Browse files
committed
optimize some logic for command help
1 parent 2490844 commit 4ed2ae8

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ $script = \array_shift($argv);
3131
list($args, $shortOpts, $longOpts) = Flags::parseArgv($argv);
3232
```
3333

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+
3449
## PHP file highlight
3550

3651
> This is inspire jakub-onderka/php-console-highlighter

example/liteApp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
11
#!/usr/bin/env php
22
<?php
33

4+
use Toolkit\Cli\App;
5+
46
define('BASE_PATH', dirname(__DIR__));
57

68
require dirname(__DIR__) . '/test/boot.php';
79

810
// create app instance
9-
$app = new Toolkit\Cli\App;
11+
$app = new App([
12+
'desc' => 'this is my cli application',
13+
]);
1014

1115
// register commands
16+
17+
// use closure
1218
$app->addCommand('test', function ($app) {
1319
echo "args:\n";
1420
/** @var Toolkit\Cli\App $app */
21+
/** @noinspection ForgottenDebugOutputInspection */
1522
print_r($app->getArgs());
1623

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+
}
1848

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+
});
2254

2355
// run
2456
$app->run();

src/App.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function is_string;
2424
use function ksort;
2525
use function method_exists;
26+
use function rtrim;
2627
use function str_pad;
2728
use function strlen;
2829
use function strpos;
@@ -252,6 +253,25 @@ protected function handleException(Throwable $e): int
252253
return $code;
253254
}
254255

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+
255275
/**
256276
* @param callable $handler
257277
* @param array $config
@@ -377,11 +397,12 @@ public function displayCommandHelp(string $name): void
377397
];
378398
} else {
379399
$checkVar = true;
380-
$userHelp = $config['help'];
400+
$userHelp = rtrim($config['help'], "\n");
381401

402+
$usage = $config['usage'] ?: $usage;
382403
$nodes = [
383404
ucfirst($config['desc']),
384-
"<comment>Usage:</comment> \n " . ($config['usage'] ?: $usage),
405+
"<comment>Usage:</comment> \n $usage\n",
385406
$userHelp ? $userHelp . "\n" : ''
386407
];
387408
}

0 commit comments

Comments
 (0)