blumfontein / command-line-php
PHP 命令行界面类。提供友好且灵活的 CLI 参数解析。
v1.0.0
2022-03-31 08:46 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-29 06:20:28 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 function arguments($argv) by technorati at gmail dot com, 2008年2月12日
- 评论 #78651 function getArgs($args) by B Crawford, 2007年10月22日
注意:parseArgs.php 包含了相同代码的“压缩”版本,供您复制粘贴使用。适用于您想要具有良好界面的小脚本。