iwnnay/scriptopts
Composer 安装命令:
composer require iwnnay/scriptopts
包简介
This is a library to help quickly add options and help menus to PHP written command line scripts.
README 文档
README
PHP library to help pass options and make a quick help screen for command line scripts
Installation
ScriptOpts is designed to be a portable library with one main class and a series of exceptions. It can of course be cloned directly from GitHub, but I'd highly recommend downloading it using Composer. With composer the installation is fairly regular.
If you do not have a composer.json already in project root then you can run
composer init
You can either add it during the interactive part of the composer init or enter it after in the composer.json like so:
"require": {
"iwnnay/scriptopts": "~1.0.0"
}
and then run
composer install
OR
If you're not keen on manually installing it you can always just do this instead:
composer require iwnnay/scriptopts:1.*
Usage
As always, when using Composer to install your applications you'll have access to the handy vendor/autoload.php. You should either be requiring that in your app or right away in the script in which you intend to use ScriptOpts.
<?php
require __DIR__ . '/vendor/autoload.php';
Initial options
The power of using ScriptOpts should be apparent in this section. Instead of having to manually write a man page or defining the options and then attributing them manually you can simply call the public status method parseNow()
Definition
ScriptOpts\Options::parseNow(array $options, [string ..$designation]);
$options: (array) This is an array of arrays that will be arguments used to quickly build out the options needed for your scripts. You can indicate that an option should take a value by adding a : after the long and short name.
$designation: You can enter as many designations as you want. When a user of the script passes a non-option argument to the script it will be assigned in three places.
Example
$options = new Options::parseNow(
[
['longName', 'l', 'Description of this option'],
['takesValue:', 't', 'Description of an option that takes a value'],
['hasCallBack', 'c', 'Uses a scoped callback($value, Options)',
'callback' => [$this, 'functionOrFunctionName']
]
//default option ['help', 'h', 'Displays help screen']
],
'*requiredParam',
'optionalParam'
);
function functionOrFunctionName($value, $options)
{
return $valueThatHasBeenChangedOrChecked;
}
$options->get('longName'); // (bool) true if --longName or -l were used options
$options->get('longName'); // null if --longName or -l were NOT used options
$options->get('takesValue'); // (string) $value if --takesValue=value || --takesValue value || -t value
$options->get('takesValue'); // null if --takesValue || -t were NOT used
$options->get('hasCallBack'); // (mixed) value returned from function
$options->get('hasCallBack'); // null if --hasCallBack or -c were NOT used
$options->get('notUsed'); // null
Designations
If a designation is entered it can be accessed one of three ways.
// This is the most secure way and inline with other usages
$options->get('requiredParam');
// It will be added back into the script $argv
global $argv;
$argv['requiredParam'];
// And if there are no collisions with existing attributes you can also use
$options->requiredParam;
Custom Error Handler
If you don't like the way that errors are being handled by default you can add your own custom error handler like so:
$options = new Options($usageStatement, $optionsArray);
$options->setCustomErrorHandler(function(){
die('with custom error every time');
});
$options->parse();
Other Public Methods
getOptions: Returns an array of all the discovered parsed options and designations
$options = Options::ParseNow(...);
$options->getOptions(); // [ 'passedOption' => 'parsedValue' ]
displayManPage: Prints out the usage statement and available options. Does not cause script to die
$options = Options::ParseNow(...);
$options->displayManPage();
setUsageStatement: Takes passed var and uses that when displaying man page instead of generated usage statement
$options = new Options(...);
$options->setUsageStatement('usage: Something Unusual -42');
getUsageStatement: Retrieves either the set or generated usage statement depending on whichever is relevant
$options = new Options(...); // or parseNow()
$options->getUsageStatement();
Author
My name is Joe Imhoff, you can reach me at iwnnay@gmail.com if you have any questions, concerns or suggestions about ScriptOpts or any other packages I've built.
I was in Ruby land for quite some time as my main language and now that I'm back in PHP I'm happy to be part of a thriving community of developers that have made it easy to share projects like this one.
Enjoy!
iwnnay/scriptopts 适用场景与选型建议
iwnnay/scriptopts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 iwnnay/scriptopts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iwnnay/scriptopts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache
- 更新时间: 2016-04-23