adeptoas/sweet-cli
Composer 安装命令:
composer require adeptoas/sweet-cli
包简介
Framework for making easy CLI applications
README 文档
README
A framework for creating awesome CLI applications using PHP.
Installation
Add this to composer.json:
"require": { "adeptoas/sweet-cli": "^1.4.0" }
Make sure to merge your require-blocks!
Examples
For examples have a look in /example. There you'll find a step-by-step tutorial on how to create a CLI application.
Concept
Your CLI application will consist of the main entry point and your subcommands. Each subcommand can have options and arguments and is free to do whatever it needs to do. Commands look like this:
cli subcommand -o -n 2 --long-opt --long-opt-string Examplecli subcommand -on 2 --long-opt "Some Argument"cli -vv subcommand -on 2- …
How to Build an Application
-
Create a new file you want to use as your "binary" to be run. I recommend
cli. -
Put this in the top of your file (nothing before it!):
#!/usr/bin/env php <?php
-
Include the composer autoloader:
require 'vendor/autoload.php';
-
Subclass
Adepto\SweetCLI\Base\CLIApplicationand override these methods:public static function getTitle(): stringpublic static function getShortTitle(): string
-
Subclass
Adepto\SweetCLI\Subcommands\SubCommandfor every subcommand you need and override these methods:public static function getOptions(): array(refer to the options syntax for this)public static function getCommand(): stringpublic static function getDescription(): string
-
Instantiate your application (where
Applicationis the name of your class. If you used PHP anonymous classes you don't need this step.):$app = new Application(__FILE__);
-
Add your newly created subcommand classes to your application (do not create instances!):
$app->addSubCommand(SomeSubCommand::class); $app->addSubCommand(SomeOtherSubCommand::class);
-
(Optional) Add aliases:
$app->addAlias('npm'); $app->addAlias('php', '/usr/local/bin/php');
Aliases are special subcommands that collect all passed arguments and call another application with those options, i.e.
cli composer -v->composer -v. The command will be run from the application's working directory, if a path was passed to the application's constructor. -
Call
$app->run($argv). -
Make your script executable:
chmod +x cli -
You can now run it:
./cli --help
Options Syntax
When creating a subcommand you have to override the getOptions(): array method. This has to return an array of available options for that subcommand. The key of each item has to be the option specification with the value being another array with modifiers.
This is the option specification:
o→ -o [boolean]option→ --option [boolean]o|option→ -o and --option [boolean]o:→ -o [boolean, required]o+→ -o value [string values]o:+→ -o value -o value2 [string values, required]
So in short:
:→ required+→ values allowed|→ alternative
This is a list of available modifiers:
"desc" => "Some Description"→ description for the help page. Leave this empty to make the option invisible."default" => "Some Value"→ default to "Some Value" if option is not given"validValues" => [1, 2, 3]→ only allow 1, 2 or 3 for the option"type" => "number"→ only allow numbers
Usage
SubCommand
This is only a description of the public API that you will use to do stuff in your subcommand.
checkConflictingOptions()
See if hasConflictingOptions() returns true and if so throw an exception.
hasDuplicateBoolean(bool $bool, array $values): bool
Returns true if $bool occurs more than once in $values
hasOptions(): bool
See if this subcommand has been given options.
hasOption(string $option): bool
See if this subcommand has been given a specific $option.
getOption(string $option): mixed
Get the value of a specific option or null if the option wasn't set.
setOption(string $option, mixed $value)
Set the value of an option. Use this with caution!
requireOption(string $option, string $message)
Require an option to be set and terminate with $message if missing.
getApp(): CLIApplication
Get the application the subcommand has been called from. If you have multiple applications using the same subcommand this can be handy to determine what to do based on the application.
Check if $option is set and throw an exception containing $message if it isn't set.
adeptoas/sweet-cli 适用场景与选型建议
adeptoas/sweet-cli 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 128 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 adeptoas/sweet-cli 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adeptoas/sweet-cli 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 128
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2017-04-14