pointybeard / shell-args
一个简单的接口,用于处理传递给 PHP 命令行的参数。
Requires
- php: >=5.6.6
Requires (Dev)
- phpunit/phpunit: ^5
README
一个方便的类,用于加载通过命令行($argv)传递的参数
- 从 $argv 自动加载,或传递自定义数组
- 使用 Iterator 基类,但添加了 find(),使查找和测试参数变得简单。
- 支持大多数常见的参数格式
安装
PHP CLI 的 Shell 参数通过 Composer 安装。要安装,请使用 composer require pointybeard/shell-args
或将 "pointybeard/shell-args": "~1.0"
添加到您的 composer.json
文件中。
用法
将 pointybeard\ShellArgs\Lib
包含到您的脚本中,然后创建一个 ArgumentIterator
实例。它将自动查找参数,或者您可以传递自己的参数字符串(见下文)。
支持的语法
此库支持大多数常见的参数格式。具体来说,支持 -x
、 --long
、/x
。它还支持使用 =
或 :
作为分隔符。以下是一些受支持的参数语法的示例
-x
--aa
--database=blah
-d:blah
--d blah
--database-name=blah
/d blah
-u http://www.theproject.com
-y something
-p:\Users\pointybeard\Sites\shellargs\
-p:"\Users\pointybeard\Sites"
-h:local:host
/host=local-host
示例
<?php use pointybeard\ShellArgs\Lib; // Load up the arguments from $argv. By default // it will ignore the first item, which is the // script name $args = new ArgumentIterator(); // Instead of using $argv, send in an array // of arguments. e.g. emulates "... -i --database blah" $args = new ArgumentIterator(false, [ '-i', '--database', 'blah' ]); // Arguments can an entire string too [Added 1.0.1] $args = new ArgumentIterator(false, [ '-i --database blah' ]); // Iterate over all the arguments foreach($args as $a){ printf("%s => %s" . PHP_EOL, $a->name(), $a->value()); } // Find a specific argument by name $args->find('i'); // Find also accepts an array of values, returning the first one that is valid $args->find(['h', 'help', 'usage']);
运行测试套件
您可以通过在 shell-args 文件夹中运行以下命令来检查所有代码是否通过
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ArgumentsTest
如果您想运行代码覆盖率(例如,--coverage-html tests/reports/ ...
),您需要一个较旧的 xdebug 版本(用于 PHP 5.6)。要安装它,请使用以下命令
pecl channel-update pecl.php.net
pecl install xdebug-2.5.5
您需要启用 xdebug.so
。尝试将以下内容添加到 /etc/php/5.6/mods-available
; configuration for php xdebug module
; priority=20
zend_extension=/usr/lib/php/20131226/xdebug.so
然后使用 phpenmod xdebug
启用它。上面的方法适用于 Ubuntu,但其他发行版的路径可能不同。
支持
如果您认为您发现了一个错误,请使用 GitHub 问题跟踪器 报告它,或者更好的是,分支库并提交一个拉取请求。
贡献
我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。
许可证
"Shell Arguments for PHP CLI" 在 MIT 许可证 下发布。