Skip to content

Commit 36903c1

Browse files
committed
feat: add built in simple app and cmd builder
1 parent 2ad6fd5 commit 36903c1

14 files changed

+577
-313
lines changed

README.md

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,132 @@ $arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
226226

227227
-----------
228228

229-
## Flag Rule
229+
## Create simple cmd or app
230+
231+
In the pflag, built in `CliApp` and `CliCmd` for quick create and run an simple console application.
232+
233+
### Create simple alone command
234+
235+
Build and run a simple command handler. see example file [example/clicmd.php](example/clicmd.php)
236+
237+
```php
238+
use Toolkit\Cli\Cli;
239+
use Toolkit\PFlag\CliCmd;
240+
use Toolkit\PFlag\FlagsParser;
241+
242+
CliCmd::new()
243+
->config(function (CliCmd $cmd) {
244+
$cmd->name = 'demo';
245+
$cmd->desc = 'description for demo command';
246+
247+
// config flags
248+
$cmd->options = [
249+
'age, a' => 'int;the option age, is int',
250+
'name, n' => 'the option name, is string and required;true',
251+
'tags, t' => 'array;the option tags, is array',
252+
];
253+
// or use property
254+
// $cmd->arguments = [...];
255+
})
256+
->withArguments([
257+
'arg1' => 'this is arg1, is string'
258+
])
259+
->setHandler(function (FlagsParser $fs) {
260+
Cli::info('options:');
261+
vdump($fs->getOpts());
262+
Cli::info('arguments:');
263+
vdump($fs->getArgs());
264+
})
265+
->run();
266+
```
267+
268+
**Usage:**
269+
270+
```php
271+
# show help
272+
php example/clicmd.php -h
273+
# run command
274+
php example/clicmd.php --age 23 --name inhere value1
275+
```
276+
277+
- Display help:
278+
279+
![cmd-demo-help](example/images/cli-cmd-help.png)
280+
281+
- Run command:
282+
283+
![cmd-demo-run](example/images/cli-cmd-run.png)
284+
285+
### Create an multi commands app
286+
287+
Build and run a simple command handler. see example file [example/cliapp.php](example/cliapp.php)
288+
289+
```php
290+
use Toolkit\Cli\Cli;
291+
use Toolkit\PFlag\CliApp;
292+
use Toolkit\PFlag\FlagsParser;
293+
294+
$app = new CliApp();
295+
296+
$app->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
297+
'desc' => 'the test 1 command',
298+
'options' => [
299+
'opt1' => 'opt1 for command test1',
300+
'opt2' => 'int;opt2 for command test1',
301+
],
302+
]);
303+
304+
$app->add('test2', function (FlagsParser $fs) {
305+
Cli::info('options:');
306+
vdump($fs->getOpts());
307+
Cli::info('arguments:');
308+
vdump($fs->getArgs());
309+
}, [
310+
// 'desc' => 'the test2 command',
311+
'options' => [
312+
'opt1' => 'a string opt1 for command test2',
313+
'opt2' => 'int;a int opt2 for command test2',
314+
],
315+
'arguments' => [
316+
'arg1' => 'required arg1 for command test2;true',
317+
]
318+
]);
319+
320+
$app->add('show-err', fn() => throw new RuntimeException('test show exception'));
321+
322+
$app->run();
323+
```
324+
325+
**Usage:**
326+
327+
```php
328+
# show help
329+
php example/cliapp.php -h
330+
# run command
331+
php example/cliapp.php test2 --opt1 val1 --opt2 23 value1
332+
```
333+
334+
- Display commands:
335+
336+
![cli-app-help](example/images/cli-app-help.png)
337+
338+
- Command help:
339+
340+
![cli-app-cmd-help](example/images/cli-app-cmd-help.png)
341+
342+
- Run command:
343+
344+
![cli-app-cmd-run](example/images/cli-app-cmd-run.png)
345+
346+
-----------
347+
348+
## Flag rule
230349

231350
The options/arguments rules. Use rule can quick define an option or argument.
232351

233352
- string value is rule(`type;desc;required;default;shorts`).
234353
- array is define item `SFlags::DEFINE_ITEM`
235-
- supportted type see `FlagType::*`
354+
- supported type see `FlagType::*`
236355

237356
```php
238357
use Toolkit\PFlag\FlagType;
@@ -258,7 +377,7 @@ $rules = [
258377
259378
**For arguments**
260379

261-
- arguemnt no alias/shorts
380+
- argument no alias/shorts
262381
- array value only allow defined at last
263382

264383
**Definition item**
@@ -283,7 +402,7 @@ public const DEFINE_ITEM = [
283402

284403
-----------
285404

286-
## Costom settings
405+
## Custom settings
287406

288407
### Settings for parse
289408

example/cliapp.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Toolkit\Cli\Cli;
4+
use Toolkit\PFlag\CliApp;
5+
use Toolkit\PFlag\FlagsParser;
6+
7+
require dirname(__DIR__) . '/test/bootstrap.php';
8+
9+
// run demo:
10+
// php example/cliapp.php
11+
12+
$app = new CliApp();
13+
14+
$app->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
15+
'desc' => 'the test 1 command',
16+
'options' => [
17+
'opt1' => 'opt1 for command test1',
18+
'opt2' => 'int;opt2 for command test1',
19+
],
20+
]);
21+
22+
$app->add('test2', function (FlagsParser $fs) {
23+
Cli::info('options:');
24+
vdump($fs->getOpts());
25+
Cli::info('arguments:');
26+
vdump($fs->getArgs());
27+
}, [
28+
// 'desc' => 'the test2 command',
29+
'options' => [
30+
'opt1' => 'string;a string opt1 for command test2, and is required;true',
31+
'opt2' => 'int;a int opt2 for command test2',
32+
],
33+
'arguments' => [
34+
'arg1' => 'required arg1 for command test2;true',
35+
]
36+
]);
37+
38+
$app->add('show-err', fn() => throw new RuntimeException('test show exception'));
39+
40+
$app->run();
41+

example/clicmd.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Toolkit\Cli\Cli;
4+
use Toolkit\PFlag\CliCmd;
5+
use Toolkit\PFlag\FlagsParser;
6+
7+
require dirname(__DIR__) . '/test/bootstrap.php';
8+
9+
// run demo:
10+
// php example/clicmd.php
11+
// php example/clicmd.php --name inhere value1
12+
// php example/clicmd.php --age 23 --name inhere value1
13+
14+
CliCmd::new()
15+
->config(function (CliCmd $cmd) {
16+
$cmd->name = 'demo';
17+
$cmd->desc = 'description for demo command';
18+
19+
// config flags
20+
$cmd->options = [
21+
'age, a' => 'int;the option age, is int',
22+
'name, n' => 'the option name, is string and required;true',
23+
'tags, t' => 'array;the option tags, is array',
24+
];
25+
// or use property
26+
// $cmd->arguments = [...];
27+
})
28+
->withArguments([
29+
'arg1' => 'this is arg1, is string'
30+
])
31+
->setHandler(function (FlagsParser $fs) {
32+
Cli::info('options:');
33+
vdump($fs->getOpts());
34+
Cli::info('arguments:');
35+
vdump($fs->getArgs());
36+
})
37+
->run();
38+

example/images/cli-app-cmd-help.png

26.6 KB
Loading

example/images/cli-app-cmd-run.png

31.8 KB
Loading

example/images/cli-app-help.png

32.9 KB
Loading

example/images/cli-cmd-help.png

32.2 KB
Loading

example/images/cli-cmd-run.png

31.3 KB
Loading

0 commit comments

Comments
 (0)