@@ -226,13 +226,132 @@ $arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
226
226
227
227
-----------
228
228
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
230
349
231
350
The options/arguments rules. Use rule can quick define an option or argument.
232
351
233
352
- string value is rule(` type;desc;required;default;shorts ` ).
234
353
- array is define item ` SFlags::DEFINE_ITEM `
235
- - supportted type see ` FlagType::* `
354
+ - supported type see ` FlagType::* `
236
355
237
356
``` php
238
357
use Toolkit\PFlag\FlagType;
@@ -258,7 +377,7 @@ $rules = [
258
377
259
378
** For arguments**
260
379
261
- - arguemnt no alias/shorts
380
+ - argument no alias/shorts
262
381
- array value only allow defined at last
263
382
264
383
** Definition item**
@@ -283,7 +402,7 @@ public const DEFINE_ITEM = [
283
402
284
403
-----------
285
404
286
- ## Costom settings
405
+ ## Custom settings
287
406
288
407
### Settings for parse
289
408
0 commit comments