pwfisher / command-line-php
PHP 命令行接口类。提供友好且灵活的 CLI 参数解析。
dev-master
2014-09-23 01:07 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 03:02:54 UTC
README
PHP 命令行接口类。提供友好且灵活的 CLI 参数解析。
用法
$args = CommandLine::parseArgs($_SERVER['argv']);
此命令行选项解析器支持三种类型选项的任意组合 [单个字符选项 (-a -b
或 -ab
或 -c -d=dog
或 -cd dog
),长选项 (--foo
或 --bar=baz
或 --bar baz
) 和参数 (arg1 arg2
)],并返回一个简单的数组。
[pfisher ~]$ php test.php --foo --bar=baz --spam eggs
["foo"] => true
["bar"] => "baz"
["spam"] => "eggs"
[pfisher ~]$ php test.php -abc foo
["a"] => true
["b"] => true
["c"] => "foo"
[pfisher ~]$ php test.php arg1 arg2 arg3
[0] => "arg1"
[1] => "arg2"
[2] => "arg3"
[pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \
> 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s
[0] => "plain-arg"
["foo"] => true
["bar"] => "baz"
["funny"] => "spam=eggs"
["also-funny"]=> "spam=eggs"
[1] => "plain arg 2"
["a"] => true
["b"] => true
["c"] => true
["k"] => "value"
[2] => "plain arg 3"
["s"] => "overwrite"
不支持: -cd=dog
。
@作者 Patrick Fisher patrick@pwfisher.com
@自 2009年8月21日
@参考
- https://php.ac.cn/manual/en/features.commandline.php
- 评论 #81042 函数 arguments($argv) 由 technorati at gmail dot com, 2008年2月12日
- 评论 #78651 函数 getArgs($args) 由 B Crawford, 2007年10月22日
注意:parseArgs.php 包含相同代码的“压缩”版本,供您复制粘贴使用。对于需要良好界面的小脚本。