amercier/cli-helpers

用于编写PHP命令行脚本的实用类

v1.4.5 2017-11-15 21:10 UTC

This package is auto-updated.

Last update: 2024-09-12 23:11:43 UTC


README

用于编写PHP命令行脚本的实用类

Build Status Code Climate Test Coverage Dependency Status

Latest Stable Version PHP version License

安装

php-cli-helpers 通过 Composer 可用。

  "require": {
    "amercier/cli-helpers": "1.*"
  }
php composer.phar install

如果您不熟悉 Composer,请阅读 完整的安装说明

使用方法

\Cli\Helpers\Parameter

用于处理命令行参数的实用类。

$options = \Cli\Helpers\Parameter::getFromCommandLine(array(
    'host'     => new Parameter('h', 'host'    , '127.0.0.1'),
    'username' => new Parameter('u', 'username', Parameter::VALUE_REQUIRED),
    'password' => new Parameter('p', 'password', Parameter::VALUE_REQUIRED),
    'verbose'  => new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE),
));

$options['host'];     // given -h/--host, or 127.0.0.1 otherwise
$options['username']; // given -u/--username
$options['password']; // given -p/--password
$options['verbose'];  // true if -v/--verbose is given, false otherwise

请参阅 参数API文档

\Cli\Helpers\Script 和 \Cli\Helpers\DocumentedScript

用于将脚本作为对象编写的实用类。

#!/usr/bin/env php
<?php
require_once dirname(__FILE__) . '/path/to/vendor/autoload.php';

use Cli\Helpers\DocumentedScript;
use Cli\Helpers\Parameter;

$script = new DocumentedScript();
$script
    ->setName('test-documented-script.php')
    ->setVersion('1.0')
    ->setDescription('Test script for Cli\Helpers\DocumentedScript')
    ->setCopyright('Copyright (c) Alexandre Mercier 2014')
    ->addParameter(new Parameter('H', 'host'    , '127.0.0.1')              , 'Host.')
    ->addParameter(new Parameter('u', 'username', Parameter::VALUE_REQUIRED), 'User name.')
    ->addParameter(new Parameter('p', 'password', Parameter::VALUE_REQUIRED), 'Password.')
    ->addParameter(new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE), 'Enable verbosity.')
    ->setProgram(function ($options, $arguments) {
        var_dump($arguments);
        var_dump($options);
    })
    ->start();

虽然 Script 没有任何预配置的开关,但 DocumentedScript--h, --help-V, --version。这提供了这两个开关的自动处理。

版本示例

test-documented-script.php -V

test-documented-script.php v1.0
Copyright (c) 2014 Alexandre Mercier

帮助示例

test-documented-script.php -h

Usage: test-documented-script.php -p PASSWORD -u USERNAME [OPTIONS]

Test script for Cli\Helpers\DocumentedScript

  -H, --host     HOST        Host (defaults to '127.0.0.1').
  -u, --username USERNAME    User name.
  -p, --password PASSWORD    Password.
  -v, --verbose              Enable verbosity.
  -h, --help                 Display this help and exit.
  -V, --version              Output version information and exit.

test-documented-script.php v1.0
Copyright (c) 2014 Alexandre Mercier

\Cli\Helpers\Job

用于运行作业并捕获异常的实用类。

成功作业时

\Cli\Helpers\Job::run('Doing awesome stuff', function() {
    ... // awesome stuff
});
Doing awesome stuff... OK

失败作业时

\Cli\Helpers\Job::run('Fighting Chuck Norris', function() {
    ... // throws a RoundHouseKickException('You've received a round-house kick', 'face')
});
Fighting Chuck Norris... NOK - You've received a round-house kick in the face

您还可以向函数添加参数

\Cli\Helpers\Job::run(
    'Doing awesome stuff',
    function($a, $b) {
        $a; // => 1337;
        $b; // => 'good luck, im behind 7 firewalls';
    },
    array(1337, 'im behind 7 firewalls')
});

请参阅 作业API文档

\Cli\Helpers\IO

用于处理标准输入/输出的实用类。

IO::form

使用方法

\Cli\Helpers\IO::form('an apple', array(
    'Golden Delicious',
    'Granny Smith',
    'Pink Lady',
    'Royal Gala',
));

将显示

1. Golden Delicious
2. Granny Smith
3. Pink Lady
4. Royal Gala

Choose an apple: |

然后,用户将在标准输入中选择1和3之间的一个选项。

IO::strPadAll

echo IO::strPadAll(
    array( // items
        array('#', 'EN', 'FR', 'ES'),
        '',
        array('1', 'One', 'Un', 'Uno'),
        array('2', 'Two', 'Deux', 'Dos'),
        array('3', 'Three', 'Trois', 'Tres'),
        array('4', 'Four', 'Quatre', 'Cuatro'),
        array('5', 'Five', 'Cinq', 'Cinco'),
    ),
    array( // alignment
        2 => STR_PAD_LEFT,
        3 => STR_PAD_RIGHT,
    ),
    "\n", // line separator
    '   ' // field separator
));

将显示

#   EN          FR   ES

1   One         Un   Uno
2   Two       Deux   Dos
3   Three    Trois   Tres
4   Four    Quatre   Cuatro
5   Five      Cinq   Cinco

请参阅 IO API文档

贡献

贡献(问题 ♥,拉取请求 ♥♥♥)非常欢迎!请随意克隆、分支、修改、扩展等,只要您尊重 许可协议

请参阅 贡献说明 获取详细信息。

许可

本项目在 ISC许可 下发布。如果您由于任何原因需要另一个许可,但您有兴趣使用以下定义的工作,请联系 作者