cheprasov / php-cli-args
从命令行参数列表中获取选项的简单方法
3.0.0
2019-02-02 19:40 UTC
Requires
- php: >=5.5
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is not auto-updated.
Last update: 2024-09-23 06:59:45 UTC
README
CliArgs v3.0.0 for PHP >= 5.5
关于
类 CliArgs 帮助您轻松地从命令行参数列表中获取选项。
特性
- CliArgs 使用 $GLOBAL['argv'] 作为基础,并且不使用函数 getopt()。
- 它不实现 'required' 的逻辑,这应该在您的代码中处理。
- 它帮助您轻松地从命令行参数列表中获取选项。
- 它根据配置生成有关选项的帮助信息。
- 灵活的配置和数据过滤。
注意
当 register_argc_argv 被禁用时,此类不可用。
用法
示例
运行
> example.php --name=Alexander --age=32 --sex=m
or
> example.php -n Alexander -a 32 -s m
example.php
$config = [ 'name' => 'n', 'age' => 'a', 'sex' => 's' ]; $CliArgs = new CliArgs($config); echo $CliArgs->getArg('name'); // Alexander echo $CliArgs->getArg('n'); // Alexander echo $CliArgs->getArg('age'); // 42 echo $CliArgs->getArg('a'); // 42 echo $CliArgs->getArg('sex'); // m echo $CliArgs->getArg('s'); // m
配置
注意:您想在 CLI 中使用的所有参数都应该在配置中指定,否则将忽略它们。
$config = [ // You should specify key as name of option from the command line argument list. // Example, name <param-name> for --param-name option 'param-name' => [ 'alias' => 'p', // [optional], [string] // Alias helps to have short or long name for this key. // Example, name <p> for -p option 'default' => false, // [optional], [mixed], [default = null] // Default value will returned if param is not setted // or params has not value. 'help' => 'Some description about param', // [optional], [string] // Text that will returned, if you request help 'filter' => 'int', // [optional], [string | array | callable] // Filter for the return value. // You can use next filters: flag, bool, int, float, help, json, <array>, <function> // 'int' - cast to integer before return. // 'float' - cast to float before return. // 'bool' - cast to bool before return. Yes, true, 1 = TRUE, other = FALSE // 'json' - decode JSON data before return. // 'flag' - will return TRUE, if key is exists in command line argument list, otherwise - FALSE // <array> - use array for enums. Example use ['a', 'b', 'c'] to get only one of these. // <callable> - use function($value, $default) { ... } to process value by yourself ] ]; $CliArgs = new CliArgs($config);
配置示例
示例 1
// Simple configs // The config1 and config2 are equal $config1 = ['foo', 'bar', 'a']; $config2 = [ 'foo' => [], 'bar' => [], 'a' => [], ]; // The config3 and config4 are equal $config3 = ['foo' => 'f', 'bar' => 'b', 'a']; $config4 = [ 'foo' => [ 'alias' => 'f', ], 'bar' => [ 'alias' => 'b', ], 'a' => [], ];
示例 2
$config = [ 'help' => [ 'alias' => 'h', 'help' => 'Show help about all options', ], 'data' => [ 'alias' => 'd', 'filter' => 'json', 'help' => 'Some description about this param', ], 'user-id' => [ 'alias' => 'u', 'filter' => 'int', 'help' => 'Some description about this param', ] ]; $CliArgs = new CliArgs($config);
Show help
> some-script.php --help
<?php if ($CliArgs->isFlagExist('help', 'h')) echo $CliArgs->getHelp('help'); ?>
Show help only for param data
> some-script.php --help data
<?php if ($CliArgs->isFlagExist('help')) echo $CliArgs->getHelp('help'); ?>
Show help for all params data
> some-script.php --help data
<?php if ($CliArgs->isFlagExist('help')) echo $CliArgs->getHelp(); ?>
All the same:
> some-script.php --data='{"foo":"bar"}' --user-id=42
or
> some-script.php --data '{"foo":"bar"}' --user-id 42
or
> some-script.php -d '{"foo":"bar"}' --user-id 42
or
> some-script.php -d '{"foo":"bar"}' -u 42
<?php
print_r($CliArgs->getArg('data'));
print_r($CliArgs->getArg('d'));
print_r($CliArgs->getArg('user-id'));
print_r($CliArgs->getArg('u'));
示例 3
$config = [ 'flag' => [ 'alias' => 'f', 'filter' => 'flag', ], 'id' => [ 'filter' => 'int', ], 'any' => [], ];
> some-script.php --flag
> some-script.php -f
> some-script.php -f --id=42 --any="any value"
> some-script.php --any="any value"
<?php
print_r($CliArgs->isFlagExist('flag')); // or $CliArgs->isFlagExist('f')
print_r($CliArgs->getArg('data'));
print_r($CliArgs->getArg('d'));
print_r($CliArgs->getArg('user-id'));
print_r($CliArgs->getArg('u'));
示例 4
$config = [ 'name' => [ 'alias' => 'n', 'filter' => function($name, $default) { return $name ? mb_convert_case($name, MB_CASE_TITLE, 'UTF-8') : $defult; }, 'default' => 'No name', ], 'sex' => [ 'alias' => 's', 'filter' => ['m', 'f'], 'default' => null, ], 'city' => [ 'alias' => 'c', 'filter' => function($city) { // ... some checks of city }, ], 'email' => [ 'alias' => 'e', 'filter' => function($city) { // ... some checks of email }, ] ];
> some-script.php --name alexander
> some-script.php -f
> some-script.php -f --id=42 --any="any value"
> some-script.php --any="any value"
<?php
print_r($CliArgs->getArg('name'));
print_r($CliArgs->('d'));
print_r($CliArgs->getArg('user-id'));
print_r($CliArgs->getArg('u'));
创建新实例
// simple config $config = ['foo' => 'f', 'bar' => 'b']; $CliArgs = new CliArgs($config);
方法
> example.php --foo Hello --bar World
new CliArgs(array|null $config = null)
构造函数。如果配置包含错误的别名,则抛出 ConfigErrorException。
$config = ['foo' => 'f', 'bar' => 'b']; $CliArgs = new CliArgs($config);
getArgs(): array
此方法返回所有配置中指定的传递的参数。
$argv = $CliArgs->getArgs(); print_r($argv); // array( // 'foo' => 'Hello', // 'bar' => 'World', // )
getArg(string $key): mixed | null
通过键返回参数的值。如果参数未设置,则返回默认值或 null
。
$arg = $CliArgs->getArg('foo'); // or $CliArgs->getArg('f'); echo $arg; // Hello
isFlagExist(string $key, boolean $checkAlias = true): bool
如果键存在,则返回 true
,否则方法返回 false
。如果 $checkAlias
为 true
,则方法将检查键和别名,如果键或别名存在,则返回 true
。如果 $checkAlias
为 false
,则方法将仅检查键,并且只有在键存在时才返回 true
。
// some_script.php --foo $CliArgs = new $CliArgs(['foo' => 'f']); echo $CliArgs->isFlagExist('foo'); // true echo $CliArgs->isFlagExist('f'); // true echo $CliArgs->isFlagExist('foo', false); // true echo $CliArgs->isFlagExist('f', false); // false
getArguments(): array
从 ARGV 获取准备好的参数
print_r($CliArgs->getArguments()); // array( // 0 => 'example.php' // 'foo' => 'Hello', // 'bar' => 'World', // )
geHelp([string $value = null]): string
获取帮助
echo $CliArgs->getHelp(); // Get help for all params echo $CliArgs->getHelp('help'); // Get help for secified params: --help data
安装
Composer
下载 composer
wget -nc https://getcomposer.org.cn/composer.phar
并将依赖项添加到您的项目中
php composer.phar require cheprasov/php-cli-args
运行测试
./vendor/bin/phpunit
某些事情不起作用
请随意分支项目,修复错误,并最终请求拉取(请记住编写测试)