1. Go to this page and download the library: Download toolkit/pflag library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
toolkit / pflag example snippets
use Toolkit\PFlag\Flags;
];
// NOTICE: must shift first element.
$scriptFile = array_shift($flags);
$fs = Flags::new();
// can with some config
$fs->setScriptFile($scriptFile);
/** @see Flags::$settings */
$fs->setSettings([
'descNlOnOptLen' => 26
]);
// ...
use Toolkit\PFlag\Flag\Option;
use Toolkit\PFlag\FlagType;
use Toolkit\PFlag\Validator\EnumValidator;
// add options
// - quick add
$fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
// - use string rule
$fs->addOptByRule('name,n', 'string;this is a string option;true');
// - use array rule
/** @see Flags::DEFINE_ITEM for array rule */
$fs->addOptByRule('name-is-very-lang', [
'type' => FlagType::STRING,
'desc' => 'option name is to lang, desc will print on newline',
'shorts' => ['d','e','f'],
// TIP: add validator limit input value.
'validator' => EnumValidator::new(['one', 'two', 'three']),
]);
// -- add multi option at once.
$fs->addOptsByRules([
'tag,t' => 'strings;array option, allow set multi times',
'f' => 'bool;this is an bool option',
]);
// - use Option
$opt = Option::new('str1', "this is string option, \ndesc has multi line, \nhaha...");
$opt->setDefault('defVal');
$fs->addOption($opt);
use Toolkit\PFlag\Flag\Argument;
use Toolkit\PFlag\FlagType;
// add arguments
// - quick add
$fs->addArg('strArg1', 'the is string arg and is ument::new('arrArg');
// OR $arg->setType(FlagType::ARRAY);
$arg->setType(FlagType::STRINGS);
$arg->setDesc("this is an array arg,\n allow multi value,\n must define at last");
$fs->addArgument($arg);
use Toolkit\PFlag\Flags;
use Toolkit\PFlag\FlagType;
// ...
if (!$fs->parse($flags)) {
// on render help
return;
}
vdump($fs->getOpts(), $fs->getArgs());
$arg0 = $fs->getArg(0); // string(arg0)
// get an array arg
$arrArg = $fs->getArg(1); // array{"arr0", "arr1"}
// get value by name
$arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
use Toolkit\Cli\Cli;
use Toolkit\PFlag\CliCmd;
use Toolkit\PFlag\FlagsParser;
CliCmd::new()
->config(function (CliCmd $cmd) {
$cmd->name = 'demo';
$cmd->desc = 'description for demo command';
// config flags
$cmd->options = [
'age, a' => 'int;the option age, is int',
'name, n' => 'the option name, is string and etArgs());
})
->run();
# show help
php example/clicmd.php -h
# run command
php example/clicmd.php --age 23 --name inhere value1
use Toolkit\Cli\Cli;
use Toolkit\PFlag\CliApp;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlagTest\Cases\DemoCmdHandler;
$app = new CliApp();
$app->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
'desc' => 'the test 1 command',
'options' => [
'opt1' => 'opt1 for command test1',
'opt2' => 'int;opt2 for command test1',
],
]);
$app->add('test2', function (FlagsParser $fs) {
Cli::info('options:');
vdump($fs->getOpts());
Cli::info('arguments:');
vdump($fs->getArgs());
}, [
// 'desc' => 'the test2 command',
'options' => [
'opt1' => 'a string opt1 for command test2',
'opt2' => 'int;a int opt2 for command test2',
],
'arguments' => [
'arg1' => '
# show help
php example/cliapp.php -h
# run command
php example/cliapp.php test2 --opt1 val1 --opt2 23 value1
use Toolkit\PFlag\FlagType;
$rules = [
// v: only value, as name and use default type FlagType::STRING
// k-v: key is name, value can be string|array
'long,s',
// name => rule
'long,a,b' => 'int', // long is option name, a and b is shorts.
'f' => FlagType::BOOL,
'str1' => ['type' => 'int', 'desc' => 'an string option'],
'tags' => 'array', // can also: ints, strings
'name' => 'type;the description message;
public const DEFINE_ITEM = [
'name' => '',
'desc' => '',
'type' => FlagType::STRING,
'helpType' => '', // use for render help
// 'index' => 0, // only for argument
'
// -------------------- settings for parse option --------------------
/**
* Stop parse option on found first argument.
*
* - Useful for support multi commands. eg: `top --opt ... sub --opt ...`
*
* @var bool
*/
protected $stopOnFistArg = true;
/**
* Skip on found undefined option.
*
* - FALSE will throw FlagException error.
* - TRUE will skip it and collect as raw arg, then continue parse next.
*
* @var bool
*/
protected $skipOnUndefined = false;
// -------------------- settings for parse argument --------------------
/**
* Whether auto bind remaining args after option parsed
*
* @var bool
*/
protected $autoBindArgs = true;
/**
* Strict match args number.
* if exist unbind args, will throw FlagException
*
* @var bool
*/
protected $strictMatchArgs = false;
// -------------------- settings for built-in render help --------------------
/**
* Auto render help on provide '-h', '--help'
*
* @var bool
*/
protected $autoRenderHelp = true;
/**
* Show flag data type on render help.
*
* if False:
*
* -o, --opt Option desc
*
* if True:
*
* -o, --opt STRING Option desc
*
* @var bool
*/
protected $showTypeOnHelp = true;
/**
* Will call it on before print help message
*
* @var callable
*/
private $beforePrintHelp;
$fs->setHelpRenderer(function (\Toolkit\PFlag\FlagsParser $fs) {
// render help messages
});