定制 deweller/cliopts 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

deweller/cliopts

Composer 安装命令:

composer require deweller/cliopts

包简介

A no-nonsense command-line options parser and help generator for PHP CLI apps.

README 文档

README

A no-nonsense command-line options parser and help generator for PHP CLI apps.

Features

  • Simple one-line usage with a human readable configuration format
  • Parses $argv data into an associative array similar to getopt()
  • Adds error validation for missing or malformed arguments or options
  • Nicely formatted help generation
  • Supports options (e.g. -i 100) and named arguments (./script.php /tmp/myfile.txt)

Usage

Code

In its simplest form, the parser can be used with one line of php code:

// specify the spec as human readable text
$values = CLIOpts\CLIOpts::run("
{self} <in_filename>
-i, --id <id> specify an id (required)
-o, --out <out_filename> output filename
-v be verbose
-h, --help show this help
");

// show the values
echo "The values you supplied are:\n";
print_r((array)$values);

CLI Input

The interpretation of flags are somewhat flexible. The following lines are all handled in the same way by cliopts:

  1. ./script.php -v -i 101 -o /tmp/myfile.txt /tmp/infile.txt

  2. ./script.php -vi 101 -o /tmp/myfile.txt /tmp/infile.txt

  3. ./script.php -v --id 101 -o /tmp/myfile.txt /tmp/infile.txt

  4. ./script.php -v --id="101" -o /tmp/myfile.txt /tmp/infile.txt

All of the above will show this output:

The values you supplied are:        
Array                               
(                                   
    [in_filename] => /tmp/infile.txt
    [id] => 101
    [out] => /tmp/myfile.txt
    [v] =>
)                                   

The Human Readable Text Specification

Let's look at the specification in the following bit of code:

$values = CLIOpts\CLIOpts::run("
  Usage: process_files.php [options] <in_file1> [<in_file2>]
  -i, --id <id> specify an id (required)
  -v be verbose
")

The Usage Line

The spec begins with a usage line. This line is optional. But if it is provided, here is a breakdown of how the usage line is interpreted:

Usage: process_files.php [options] <in_file1> [<in_file2>]
|      |                 |         |          |
|      |                 |         |          + Optional second argument named in_file2.
|      |                 |         |
|      |                 |         + Required first argument named in_file1
|      |                 |          
|      |                 + An options placeholder.  This may be ommitted.  It must come before any arguments.
|      |
|      + An optional script name.  Omit this or use {self} to show $_SERVER['argv'][0].
|
+ The usage keyword.  This may be ommitted.

In this example, 1 argument is expected and the value provided will be assigned to the key "in_file1" in the values object. An optional second argument will be assed to the key "in_file2" if it is provided. And that's all. If a 3rd argument is provided it will not be assigned to a value and validation will fail.

The Option Lines

Here is how the first option line is interpreted:

-i, --id <id> specify an id (required)
|   |    |    |             |
|   |    |    |             + This makes the option required when validating.
|   |    |    |
|   |    |    + Help text.  This can be any text.
|   |    |
|   |    + This specifies that the option requires a value.  Unlike arguments, this is not used for the value name.
|   |
|   + This is a long option name.  It is not required.  If specified, this is used for the value name when arguments are parsed.
|
+ This is the short option name.  It is not required.  Values can be accessed using this shortcut.

deweller/cliopts 适用场景与选型建议

deweller/cliopts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.28k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2012 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cli」 「command」 「getopt」 「help」 「command-line」 「line」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 deweller/cliopts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 deweller/cliopts 我们能提供哪些服务?
定制开发 / 二次开发

基于 deweller/cliopts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 23
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-12-01