amercier/cli-helpers
Composer 安装命令:
composer require amercier/cli-helpers
包简介
Utility classes to write PHP command-line scripts
README 文档
README
Utility classes to write PHP command-line scripts
Installation
php-cli-helpers is available through Composer.
"require": { "amercier/cli-helpers": "1.*" }
php composer.phar install
If you are not familiar with Composer, please read the full installation intructions.
Usage
\Cli\Helpers\Parameter
Utility class to handle command-line parameters.
$options = \Cli\Helpers\Parameter::getFromCommandLine(array( 'host' => new Parameter('h', 'host' , '127.0.0.1'), 'username' => new Parameter('u', 'username', Parameter::VALUE_REQUIRED), 'password' => new Parameter('p', 'password', Parameter::VALUE_REQUIRED), 'verbose' => new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE), )); $options['host']; // given -h/--host, or 127.0.0.1 otherwise $options['username']; // given -u/--username $options['password']; // given -p/--password $options['verbose']; // true if -v/--verbose is given, false otherwise
See API Documentation for Parameter
\Cli\Helpers\Script and \Cli\Helpers\DocumentedScript
Utility class to write scripts as objects.
#!/usr/bin/env php <?php require_once dirname(__FILE__) . '/path/to/vendor/autoload.php'; use Cli\Helpers\DocumentedScript; use Cli\Helpers\Parameter; $script = new DocumentedScript(); $script ->setName('test-documented-script.php') ->setVersion('1.0') ->setDescription('Test script for Cli\Helpers\DocumentedScript') ->setCopyright('Copyright (c) Alexandre Mercier 2014') ->addParameter(new Parameter('H', 'host' , '127.0.0.1') , 'Host.') ->addParameter(new Parameter('u', 'username', Parameter::VALUE_REQUIRED), 'User name.') ->addParameter(new Parameter('p', 'password', Parameter::VALUE_REQUIRED), 'Password.') ->addParameter(new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE), 'Enable verbosity.') ->setProgram(function ($options, $arguments) { var_dump($arguments); var_dump($options); }) ->start();
While Script doesn't have any pre-configured switch, DocumentedScript has
--h, --help and -V, --version. This provides an automatic handling of this
two switches.
Version example:
test-documented-script.php -V
test-documented-script.php v1.0
Copyright (c) 2014 Alexandre Mercier
Help example:
test-documented-script.php -h
Usage: test-documented-script.php -p PASSWORD -u USERNAME [OPTIONS]
Test script for Cli\Helpers\DocumentedScript
-H, --host HOST Host (defaults to '127.0.0.1').
-u, --username USERNAME User name.
-p, --password PASSWORD Password.
-v, --verbose Enable verbosity.
-h, --help Display this help and exit.
-V, --version Output version information and exit.
test-documented-script.php v1.0
Copyright (c) 2014 Alexandre Mercier
\Cli\Helpers\Job
Utility class to run a job and catch exceptions.
On successful jobs:
\Cli\Helpers\Job::run('Doing awesome stuff', function() { ... // awesome stuff });
Doing awesome stuff... OK
On unsuccessful jobs:
\Cli\Helpers\Job::run('Fighting Chuck Norris', function() { ... // throws a RoundHouseKickException('You've received a round-house kick', 'face') });
Fighting Chuck Norris... NOK - You've received a round-house kick in the face
You can also add parameters to the function:
\Cli\Helpers\Job::run( 'Doing awesome stuff', function($a, $b) { $a; // => 1337; $b; // => 'good luck, im behind 7 firewalls'; }, array(1337, 'im behind 7 firewalls') });
\Cli\Helpers\IO
Utility class to handle standard input/output.
IO::form
Usage
\Cli\Helpers\IO::form('an apple', array( 'Golden Delicious', 'Granny Smith', 'Pink Lady', 'Royal Gala', ));
will display:
1. Golden Delicious
2. Granny Smith
3. Pink Lady
4. Royal Gala
Choose an apple: |
Then, user is asked to make a choice between 1 and 3 on standard input.
IO::strPadAll
echo IO::strPadAll( array( // items array('#', 'EN', 'FR', 'ES'), '', array('1', 'One', 'Un', 'Uno'), array('2', 'Two', 'Deux', 'Dos'), array('3', 'Three', 'Trois', 'Tres'), array('4', 'Four', 'Quatre', 'Cuatro'), array('5', 'Five', 'Cinq', 'Cinco'), ), array( // alignment 2 => STR_PAD_LEFT, 3 => STR_PAD_RIGHT, ), "\n", // line separator ' ' // field separator ));
will display:
# EN FR ES
1 One Un Uno
2 Two Deux Dos
3 Three Trois Tres
4 Four Quatre Cuatro
5 Five Cinq Cinco
Contributing
Contributions (issues ♥, pull requests ♥♥♥) are more than welcome! Feel free to clone, fork, modify, extend, etc, as long as you respect the license terms.
See contributing intructions for details.
Licensing
This project is released under ISC License license. If this license does not fit your requirement for whatever reason, but you would be interested in using the work (as defined below) under another license, please contact authors.
amercier/cli-helpers 适用场景与选型建议
amercier/cli-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.92k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2013 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「helpers」 「cli」 「command」 「utility」 「command-line」 「line」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 amercier/cli-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amercier/cli-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 amercier/cli-helpers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Wrapper for exec and it's results
Symfony bundle to monitor and execute commands
Shell command module for PHP.
Library with classes to help you do batch.
Command bus implementation: Commands and domain events
HTML and form generation
统计信息
- 总下载量: 8.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 39
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: ISC
- 更新时间: 2013-07-19