Skip to content

Commit fd71985

Browse files
committed
update readme. update example demo
1 parent 191ed2b commit fd71985

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@
88

99
Generic PHP command line flags parse library
1010

11+
## Features
12+
13+
- Generic parse CLI options and arguments.
14+
- Support set value data type(`int,string,bool,array`), will auto format input value.
15+
- Support set default value for option/argument.
16+
- Support set option/argument is required.
17+
- Support set validator for check input value.
18+
- Support auto generate help message.
19+
20+
**Arguments**:
21+
22+
- Support binding named arguemnt
23+
- Support define array argument
24+
25+
**Options**:
26+
27+
- Support long option `--long`
28+
- Support short option `-s -a value`, allow set multi short names.
29+
- Support define array option
30+
- eg: `--tag php --tag go` will get `tag: [php, go]`
31+
1132
## Install
1233

1334
**composer**
@@ -29,8 +50,12 @@ use Toolkit\PFlag\Flags;
2950

3051
require dirname(__DIR__) . '/test/bootstrap.php';
3152

53+
$flags = $_SERVER['argv'];
54+
// NOTICE: must shift first element.
55+
$scriptFile = array_shift($flags);
56+
3257
$fs = Flags::new();
33-
// with some config
58+
// can with some config
3459
$fs->setScriptFile($scriptFile);
3560
/** @see Flags::$settings */
3661
$fs->setSettings([
@@ -97,6 +122,8 @@ $fs->addArgument($arg);
97122
use Toolkit\PFlag\Flags;
98123
use Toolkit\PFlag\FlagType;
99124

125+
// ...
126+
100127
if (!$fs->parse($flags)) {
101128
// on render help
102129
return;

example/flags-demo.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
use Toolkit\Cli\Cli;
12+
use Toolkit\PFlag\Exception\FlagException;
1113
use Toolkit\PFlag\Flag\Argument;
1214
use Toolkit\PFlag\Flag\Option;
1315
use Toolkit\PFlag\Flags;
@@ -70,8 +72,24 @@
7072
$fs->addArgument($arg);
7173

7274
// edump($fs);
73-
if (!$fs->parse($flags)) {
74-
// on render help
75+
76+
// do parsing
77+
try {
78+
if (!$fs->parse($flags)) {
79+
// on render help
80+
return;
81+
}
82+
} catch (\Throwable $e) {
83+
if ($e instanceof FlagException) {
84+
Cli::colored('ERROR: ' . $e->getMessage(), 'error');
85+
} else {
86+
$code = $e->getCode() !== 0 ? $e->getCode() : -1;
87+
$eTpl = "Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n";
88+
89+
// print exception message
90+
printf($eTpl, $code, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
91+
}
92+
7593
return;
7694
}
7795

example/sflags-demo.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
use Toolkit\Cli\Cli;
12+
use Toolkit\PFlag\Exception\FlagException;
1113
use Toolkit\PFlag\SFlags;
1214

1315
require dirname(__DIR__) . '/test/bootstrap.php';
@@ -38,7 +40,23 @@
3840
$fs->setOptRules($optRules);
3941
$fs->setArgRules($argRules);
4042

41-
if (!$fs->parse($flags)) {
43+
// do parsing
44+
try {
45+
if (!$fs->parse($flags)) {
46+
// on render help
47+
return;
48+
}
49+
} catch (\Throwable $e) {
50+
if ($e instanceof FlagException) {
51+
Cli::colored('ERROR: ' . $e->getMessage(), 'error');
52+
} else {
53+
$code = $e->getCode() !== 0 ? $e->getCode() : -1;
54+
$eTpl = "Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n";
55+
56+
// print exception message
57+
printf($eTpl, $code, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
58+
}
59+
4260
return;
4361
}
4462

0 commit comments

Comments
 (0)