f2/cmd 问题修复 & 功能扩展

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

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

f2/cmd

Composer 安装命令:

composer require f2/cmd

包简介

Parses command line arguments and provides a nice help menu.

README 文档

README

Cmd is a simple command line parser for PHP, to make it easier to create command line tools with PHP.

Very simple example

#!/usr/bin/env php
<?php
require('vendor/autoload.php');

$cmd = new F2\Cmd\Cmd("Example tool", [
    'a|alpha'   => 'This is the description of the alpha parameter',
    '|beta'     => 'The beta parameter',
    'c'         => 'The charlie parameter',
]);

if ($cmd->flag('a')) {
    die("ALPHA!\n");
} else if ($cmd->flag('beta')) {
    die("BETA! VALUE OF c=".json_encode($cmd->value('c'))."\n");
} else if ($cmd->flag('c')) {
    die("VALUE OF c=".json_encode($cmd->value('c'))."\n");
}

echo "ALL OK!\n";

Usage:

# ./simple-example
ALL OK!
# ./simple-example --undeclared-option
simple-example: unrecognized option '--undeclared-option'
Try 'simple-example --help' for more information.
# ./simple-example -a
ALPHA!
# ./simple-example --alpha
ALPHA!
# ./simple-example --beta
BETA! VALUE OF c=null
# ./simple-example -c
VALUE OF c=true
# ./simple-example -c whatever
VALUE OF c='whatever'

Installation

The recommended way to install Fubber CMD is throught Composer

$ composer require f2/cmd

API

Constructor

Cmd::__construct(string $usage, array $args=[], array $mandatoryParams=[]);

$usage is a short description of your utility. You can use multiple lines if your utility warrants it.

$args is an array of arguments with a usage description. The key contains a pipe separated list of arguments, where the first part is a group of short options: 'abc|long1|long2'. For each short option and each long option you may specify a colon (:) to make the option require a value, or a double colon (::) to optionally have a value.

Format of the array keys:

[
    'a:b:c:|long1:|long2:|long3:' => 'Three short and three long options that require a value',
    'a:b::|long1:|long2::' => 'A combination of required and optional value',
    '|no-short-option' => 'No short option available',
    ]

More examples:

$args = [
    'a' => 'The -a flag is boolean and can be checked with $cmd->flag('a')',
    'b:' => 'Requires a value: -b 20. Check with $cmd->value('b').'
    'd:e:' => 'Aliases for a value: -d 20 is equivalent to -e 20.',
    'f:|force:' => 'Short and long version -f 20 and --force=20.',
    '|no-short' => 'No short version, only long flag --no-short.',
    ];

$mandatoryParams is a list of mandatory parameters at the end of your command:

$mandatoryParams = [
    'what-to-say',
    ];

Will make the following output:

$ your-tool --help
your-tool <what-to-say>

Flags

Flags are boolean options. They come in two forms: short and long.

$cmd = new Cmd('Tool with flags', [
    'abc' => 'Flags -a, -b and -c',                 // -a, -b and -c are aliases now
    'd|the-d-flag' => 'Flags -d and --the-d-flag',  // -d and --the-d-flag are aliases
]);
$a = $cmd->flag('a');
$b = $cmd->flag('a');
$theDFlag = $cmd->flag('d');

Options

Options are like flags, but they have a mandatory value. This is enabled by adding a colon (:) to the definition.

$cmd = new Cmd('Tool with options', [
    'a:b:c' => 'Option -a <value>, -b <value> and -c <value>',
    '|the-d-option' => 'Option --the-d-option=<value>',
    'e|the-e-option|the-extended-option' => 'Option -e <value>, --the-e-option=<value>, --the-extended-option=<value>',
]);
$a = $cmd->value('a');
$b = $cmd->value('b');
$c = $cmd->value('c');

Flags with optional value

Flags with optional options are marked with a double colon (::) in the definition.

$cmd = new Cmd('Tool with options', [
    'a::' => 'Option -a [value]',
    '|long-optional-option::' => 'Optional option',
]);

Arguments

Arguments are required arguments to the function.

$cmd = new Cmd('Tool with arguments', null, ['argument']);

Arguments are available through the built in PHP global $argv. Also you can access a version of $argv where the defined flags and options have been removed:

print_r( $cmd->argv );

More

You must do more advanced validation yourself. If there are validation errors, simply explain the error using the Cmd::error method.

$cmd->error("You can't combine -a with -b");

To display the command usage yourself:

$cmd->usage();
die();

f2/cmd 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-28