nerou/cli-parser
Composer 安装命令:
composer require nerou/cli-parser
包简介
Parser for CLI arguments
README 文档
README
This project is build around a slightly modified version of this fairly old comment on php.net.
This is for you, if...
...you are creating CLI applications with PHP, that take some command line arguments, options, commands and/or flags.
Install
Note: This library requires PHP 8.0+!
Use composer to install this library:
composer require nerou/cli-parser
There are no dependencies.
Usage
Wording
Command is just some value, e.g. myscript.php hello
Option starts with -- and can have a value, e.g. myscript.php --foo or myscript.php --foo=bar or myscript.php --foo bar
Flag starts with - and is a short form of an option, e.g. myscript.php -f or myscript.php -f bar
Argument is everything following a standalone --
Examples
Minimal example with options --foo and --bar as well as the flag -f which is a short form of --foo:
if(PHP_SAPI !== 'cli' || !isset($_SERVER['argv'])){ exit(1); // exit if not run via CLI } $cliArgs = new CLIParser($_SERVER['argv'], '[--foo] [--bar]'); $cliArgs->setAllowedOptions(['foo', 'bar']); // list of supported options $cliArgs->setAllowedFlags(['f' => 'foo']); // maps flags to options $cliArgs->setStrictMode(true); // parse() will return `false` if there are options/flags that are not allowed if(!$cliArgs->parse()){ $cliArgs->printUsage(); // show them how to use this script exit(1); }
Use value validation (see PHP filters):
$cliArgs->setAllowedOptions([ 'foo' => [ 'filter' => FILTER_VALIDATE_FLOAT, 'flags' => FILTER_FLAG_ALLOW_THOUSAND, 'options' => [ 'min_range' => 0, 'default' => 42 // default value which will also be added to usage documentation ], 'value_label' => 'my-val', // for usage documentation 'description' => 'pass some float' // for usage documentation ], 'bar' => [] // defaults to `['filter' => FILTER_DEFAULT]` ]);
Limitations and concerns
- using a single option multiple times is not supported, e.g.
phpcpd src --exclude src/foo --exclude src/bar
License
This library is licensed under the MIT License. Please see LICENSE for more information.
nerou/cli-parser 适用场景与选型建议
nerou/cli-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nerou/cli-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nerou/cli-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-18