nerou/cli-parser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

nerou/cli-parser

Composer 安装命令:

composer require nerou/cli-parser

包简介

Parser for CLI arguments

README 文档

README

License PHP Version Require Version Psalm Type Coverage

This project is build around a slightly modified version of this fairly old comment on php.net.

This is for you, if...

...you are creating CLI applications with PHP, that take some command line arguments, options, commands and/or flags.

Install

Note: This library requires PHP 8.0+!

Use composer to install this library:

composer require nerou/cli-parser

There are no dependencies.

Usage

Wording

Command is just some value, e.g. myscript.php hello

Option starts with -- and can have a value, e.g. myscript.php --foo or myscript.php --foo=bar or myscript.php --foo bar

Flag starts with - and is a short form of an option, e.g. myscript.php -f or myscript.php -f bar

Argument is everything following a standalone --

Examples

Minimal example with options --foo and --bar as well as the flag -f which is a short form of --foo:

if(PHP_SAPI !== 'cli' || !isset($_SERVER['argv'])){
  exit(1);          // exit if not run via CLI
}

$cliArgs = new CLIParser($_SERVER['argv'], '[--foo] [--bar]');
$cliArgs->setAllowedOptions(['foo', 'bar']);    // list of supported options
$cliArgs->setAllowedFlags(['f' => 'foo']);      // maps flags to options
$cliArgs->setStrictMode(true);                  // parse() will return `false` if there are options/flags that are not allowed
if(!$cliArgs->parse()){
  $cliArgs->printUsage();     // show them how to use this script
  exit(1);
}

Use value validation (see PHP filters):

$cliArgs->setAllowedOptions([
    'foo' => [
        'filter' => FILTER_VALIDATE_FLOAT,
        'flags' => FILTER_FLAG_ALLOW_THOUSAND,
        'options' => [
            'min_range' => 0,
            'default' => 42                   // default value which will also be added to usage documentation
        ],
        'value_label' => 'my-val',            // for usage documentation
        'description' => 'pass some float'    // for usage documentation
    ], 
    'bar' => []     // defaults to `['filter' => FILTER_DEFAULT]`
]);

Limitations and concerns

  • using a single option multiple times is not supported, e.g. phpcpd src --exclude src/foo --exclude src/bar

License

This library is licensed under the MIT License. Please see LICENSE for more information.

nerou/cli-parser 适用场景与选型建议

nerou/cli-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 nerou/cli-parser 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-18