承接 alcamo/cli 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

alcamo/cli

最新稳定版本:0.6.0

Composer 安装命令:

composer require alcamo/cli

包简介

Simplify creation of command-line interfaces

关键字:

README 文档

README

#!/usr/bin/env php
<?php

use alcamo\cli\AbstractCli;

include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';

class ExampleCli extends AbstractCli
{
    public const OPTIONS =
        [
            'foo' => [ 'f', self::REQUIRED_ARGUMENT, 'Set foo.']
        ]
        + parent::OPTIONS;

    public function innerRun(): int
    {
        if ($this->getOption('foo')) {
            $this->getLogger()
                ->notice('Foo was set to ' . $this->getOption('foo'));
        }

        $this->getLogger()->info('Info message.');

        return 0;
    }
}

exit((new ExampleCli([]))->run());

This example is contained in this package as a file in the bin directory. If called as bin/example --help, it outputs

Usage: bin/example [options]

Options:
  -f, --foo <arg>  Set foo.
  -h, --help       Show help.
  -q, --quiet      Be less verbose.
  -v, --verbose    Be more verbose.

The invocation example --foo 42 outputs something like

[19:01:38] N Foo was set to 42

Adding verbosity with example --foo 42 -v leads to something like

[20:02:02.913675] N Foo was set to 42
[20:02:02.913771] I Info message.

Reducing verbosity with example --foo 42 -q creates no output.

This illustrates the basic ideas:

  • Options, operands, commands etc. are defined by class constants in a class derived from AbstractCli. See GetOpt.php for details. The options --help, --quiet and --verbose are predefined.
  • AbstractCli::run() processes the options, displays the help text if requested, provides a logger with a log level depending on the verbosity, and executes innerRun().
  • You implement innerRun() to do the actual work. The return value of innerRun() becopme the program's exit code.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-07-19