jjulien/php-cli-helper
Composer 安装命令:
composer require jjulien/php-cli-helper
包简介
Helper for creating CLI based PHP programs
README 文档
README
PHP CLI Helper
PHP CLI Helper was created to make developing PHP command line tools easy. It currently supports command line option parsing and validation.
Usage Summary
The main class is the CLIHelper\Helper. You will tell this class all of the options you want to support and how you want that option to behave. After you tell the helper to parse the options, it will validate that the user has not violated any of the rules you defined for how your options should behave. If violations are found, the user receives a help message showing them how the option are suppose to work and which option rule they violated. If no violations are found, you then user the helper object to retrieve values for options and determine which options the user specified.
Usage Example
Code
#!/usr/bin/php
<?php
require(dirname(__FILE__) . '/../vendor/autoload.php');
use CLIHelper\Helper;
$helper = new Helper();
$helper->newOption()
->withName("file-in")
->withHelp("Input file to be processed")
->withShort("-i")
->withLong("--file-in")
->required()
->build();
$helper->newOption()
->withName("file-out")
->withHelp("Output file for processing result")
->withShort("-o")
->withLong("--file-out")
->required()
->build();
$helper->newOption()
->withName("verbose")
->boolean()
->withHelp("Display verbose output")
->withShort("v")
->withLong("verbose")
->build();
$helper->parse();
# Reading options
if ($helper->getValue('verbose')) {
print 'Reading in file "' . $helper->getValue('file-in') . '" and saving output to "' . $helper->getValue('file-out') . '"' . "\n";
}
Invocation
$./sample-script.php
Option -i or --file-in is required
Option -o or --file-out is required
Usage: sample-script.php <-i VALUE | --file-in VALUE> <-o VALUE | --file-out VALUE> [-h | --help] [-v | --verbose]
-h, --help : Displays this message
-i, --file-in : Input file to be processed
-o, --file-out : Output file for processing result
-v, --verbose : Display verbose output
Adding Options
There are two ways to add options. You can use the CLIHelper\OptionBuilder class, which can get using the convenience method ->newOption() on CLIHelper\Helper. You can also create your own CLIHelper\Optionobject and call->addOption($option)onCLIHelper\Helper`.
Options can either by of type Option::TYPE_BOOLEAN or Option::TYPE_VALUE, the default is Option::TYPE_VALUE. Option::TYPE_BOOLEAN options are options that are either on or off, such as the common "verbose" option. It takes no value, it's either provided on the command line or it is not. Option::TYPE_VALUE options read a value in that comes on the comman line right after the option. An example would be --file-in filename where --file-in must have a value provided along with it.
Example using ->addOption($option)
#!/usr/bin/php
<?php
require(dirname(__FILE__) . '/../vendor/autoload.php');
use CLIHelper\Helper;
use CLIHelper\Option;
$helper = new Helper();
$option = new Option();
$option->setName("verbose");
$option->setShortOpt("-v");
$option->setLongOpt("--verbose");
$option->setType(Option::TYPE_BOOLEAN);
$option->setHelp("Display verbose output");
$helper->addOption($option);
$helper->parse();
Example using ->newOption()
#!/usr/bin/php
<?php
require(dirname(__FILE__) . '/../vendor/autoload.php');
use CLIHelper\Helper;
$helper = new Helper();
$helper->newOption()
->withName("verbose")
->boolean()
->withHelp("Display verbose output")
->withShort("v")
->withLong("verbose")
->build();
$helper->parse();
jjulien/php-cli-helper 适用场景与选型建议
jjulien/php-cli-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 10 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jjulien/php-cli-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jjulien/php-cli-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2017-10-09