SFlags - is an simple flags(options&argument) parser and manager.
use Toolkit\PFlag\SFlags;
$flags = ['--name', 'inhere', '--age', '99', '--tag', 'php', '-t', 'go', '--tag', 'java', '-f', 'arg0'];
$optRules = [
'name', // string
'age' => 'int;an int option;required', // set required
'tag,t' => FlagType::ARRAY,
'f' => FlagType::BOOL,
];
$argRules = [
// some argument rules
];
$fs->setOptRules($optRules);
$fs->setArgRules($argRules);
$fs->parse($rawFlags);
// or use
// $fs->parseDefined($flags, $optRules, $argRules);
vdump($fs->getOpts(), $fs->getRawArgs());
Output:
array(3) {
["name"]=> string(6) "inhere"
["tag"]=> array(3) {
[0]=> string(3) "php"
[1]=> string(2) "go"
[2]=> string(4) "java"
}
["f"]=> bool(true)
}
array(1) {
[0]=> string(4) "arg0"
}
write the codes to an php file(see example/sflags-demo.php)
use Toolkit\PFlag\SFlags;
$rawFlags = $_SERVER['argv'];
// NOTICE: must shift first element.
$scriptFile = array_shift($rawFlags);
$optRules = [
// some option rules
'name', // string
'age' => 'int;an int option;required', // set required
'tag,t' => FlagType::ARRAY,
'f' => FlagType::BOOL,
];
$argRules = [
// some argument rules
'string',
// set name
'arrArg' => 'array',
];
$fs = SFlags::new();
$fs->parseDefined($rawFlags, $optRules, $argRules);
Run demo:
php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
Output:
array(4) {
["name"]=> string(6) "inhere"
["age"]=> int(99)
["tag"]=> array(3) {
[0]=> string(2) "go"
[1]=> string(3) "php"
[2]=> string(4) "java"
}
["f"]=> bool(true)
}
array(2) {
[0]=> string(4) "arg0"
[1]=> array(2) {
[0]=> string(4) "arr0"
[1]=> string(4) "arr1"
}
}
Show help
$ php example/sflags-demo.php --help