deweller/cliopts
Composer 安装命令:
composer require deweller/cliopts
包简介
A no-nonsense command-line options parser and help generator for PHP CLI apps.
README 文档
README
A no-nonsense command-line options parser and help generator for PHP CLI apps.
Features
- Simple one-line usage with a human readable configuration format
- Parses $argv data into an associative array similar to getopt()
- Adds error validation for missing or malformed arguments or options
- Nicely formatted help generation
- Supports options (e.g. -i 100) and named arguments (./script.php /tmp/myfile.txt)
Usage
Code
In its simplest form, the parser can be used with one line of php code:
// specify the spec as human readable text $values = CLIOpts\CLIOpts::run(" {self} <in_filename> -i, --id <id> specify an id (required) -o, --out <out_filename> output filename -v be verbose -h, --help show this help "); // show the values echo "The values you supplied are:\n"; print_r((array)$values);
CLI Input
The interpretation of flags are somewhat flexible. The following lines are all handled in the same way by cliopts:
-
./script.php -v -i 101 -o /tmp/myfile.txt /tmp/infile.txt -
./script.php -vi 101 -o /tmp/myfile.txt /tmp/infile.txt -
./script.php -v --id 101 -o /tmp/myfile.txt /tmp/infile.txt -
./script.php -v --id="101" -o /tmp/myfile.txt /tmp/infile.txt
All of the above will show this output:
The values you supplied are:
Array
(
[in_filename] => /tmp/infile.txt
[id] => 101
[out] => /tmp/myfile.txt
[v] =>
)
The Human Readable Text Specification
Let's look at the specification in the following bit of code:
$values = CLIOpts\CLIOpts::run(" Usage: process_files.php [options] <in_file1> [<in_file2>] -i, --id <id> specify an id (required) -v be verbose ")
The Usage Line
The spec begins with a usage line. This line is optional. But if it is provided, here is a breakdown of how the usage line is interpreted:
Usage: process_files.php [options] <in_file1> [<in_file2>]
| | | | |
| | | | + Optional second argument named in_file2.
| | | |
| | | + Required first argument named in_file1
| | |
| | + An options placeholder. This may be ommitted. It must come before any arguments.
| |
| + An optional script name. Omit this or use {self} to show $_SERVER['argv'][0].
|
+ The usage keyword. This may be ommitted.
In this example, 1 argument is expected and the value provided will be assigned to the key "in_file1" in the values object. An optional second argument will be assed to the key "in_file2" if it is provided. And that's all. If a 3rd argument is provided it will not be assigned to a value and validation will fail.
The Option Lines
Here is how the first option line is interpreted:
-i, --id <id> specify an id (required)
| | | | |
| | | | + This makes the option required when validating.
| | | |
| | | + Help text. This can be any text.
| | |
| | + This specifies that the option requires a value. Unlike arguments, this is not used for the value name.
| |
| + This is a long option name. It is not required. If specified, this is used for the value name when arguments are parsed.
|
+ This is the short option name. It is not required. Values can be accessed using this shortcut.
deweller/cliopts 适用场景与选型建议
deweller/cliopts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.28k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2012 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cli」 「command」 「getopt」 「help」 「command-line」 「line」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 deweller/cliopts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 deweller/cliopts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 deweller/cliopts 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Command-line framework for PHP
A Wrapper for exec and it's results
Symfony bundle to monitor and execute commands
CommandLineLib provides a high-quality command line parser via a fluent interface
Finally a sane way to register available commands and arguments and match your command line in PHP.
PHP Option Parser
统计信息
- 总下载量: 18.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 23
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-12-01