generated from inherelab/php-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsflags-demo.php
77 lines (63 loc) · 1.81 KB
/
sflags-demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php declare(strict_types=1);
/**
* This file is part of toolkit/pflag.
*
* @link https://github.com/php-toolkit
* @author https://github.com/inhere
* @license MIT
*/
use Toolkit\Cli\Cli;
use Toolkit\PFlag\Exception\FlagException;
use Toolkit\PFlag\SFlags;
require dirname(__DIR__) . '/test/bootstrap.php';
// run demo:
// php example/sflags-demo.php -h
// php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
$flags = $_SERVER['argv'];
// NOTICE: must shift first element.
$scriptFile = array_shift($flags);
$optRules = [
// some option rules
'name' => 'string;this is an string option', // string
'age' => 'int;this is an int option;required', // set required
'tag,t' => 'strings;array option, allow set multi times',
'f' => 'bool;this is an bool option',
];
$argRules = [
// some argument rules
'string',
// set name
'arrArg' => 'strings;this is an array arg, allow multi value;;[a,b]',
];
$fs = SFlags::new();
$fs->setScriptFile($scriptFile);
$fs->setOptRules($optRules);
$fs->setArgRules($argRules);
$fs->setMoreHelp('more help message ...');
$fs->setExample([
'example usage 1',
'example usage 2',
]);
// do parsing
try {
if (!$fs->parse($flags)) {
// on render help
return;
}
} catch (Throwable $e) {
if ($e instanceof FlagException) {
Cli::colored('ERROR: ' . $e->getMessage(), 'error');
} else {
$code = $e->getCode() !== 0 ? $e->getCode() : -1;
$eTpl = "Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n";
// print exception message
printf($eTpl, $code, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
}
return;
}
vdump(
// $fs->getRawArgs(),
$fs->getOpts(),
$fs->getArgs()
);
// vdump($fs->getArg('arrArg'));