brzuchal / command-line

简单的PHP命令行参数和选项解析器

1.0.0 2017-03-09 11:36 UTC

This package is auto-updated.

Last update: 2024-09-07 03:33:57 UTC


README

本项目的目的是提供现代且完整的API,用于在命令行中解析命令行参数和选项。

PHP 7.1 Build Status

命令行泛型

当你输入 ENV=prod php -d "error_reporting=E_ALL" script.php test --debug 时,只有最后一条命令行组件在 script.php 范围内可见。

以下分析显示该命令行由以下组成

ENV=prod                   // this is shell way for setting environment variables per process
php                        // this part is pointing to php interpreter
-d "error_reporting=E_ALL" // this part holds php interpreter options
script.php                 // visible at runtime script filename
test                       // visible at runtime arguments
--debug                    // visible at runtime options

运行时命令行泛型组件是: command + arguments + options

示例

从CLI中检索具有定义的参数的最简单方法

$commandLine = (new \Brzuchal\CommandLine\CommandLineBuilder())
    ->withArgument('command')
    ->withOption('file', 'f')
    ->withOption('count', 'c', true)
    ->build($_SERVER['argv'], $_SERVER['PWD']);

从CLI中检索未定义参数的最简单方法

$parser = new \Brzuchal\CommandLine\ArrayCommandLineParser($_SERVER['argv']);
$commandLine = $parser->parse();

对于 php test.php command --env=prod --debug -f -c,结果是

PHP\CLI\CommandLine Object
(
    [command:protected] => test.php
    [parameters:protected] => Array
        (
            [0] => PHP\CLI\Argument Object
                (
                    [name:protected] => arg0
                    [value:protected] => command
                )

            [1] => PHP\CLI\Option Object
                (
                    [name:protected] => env
                    [value:protected] => prod
                )

            [2] => PHP\CLI\Option Object
                (
                    [name:protected] => debug
                    [value:protected] => 
                )

            [3] => PHP\CLI\Option Object
                (
                    [name:protected] => f
                    [value:protected] => 
                )

            [4] => PHP\CLI\Option Object
                (
                    [name:protected] => c
                    [value:protected] => 1
                )

        )

    [cwd:protected] => /home/brzuchal/Workspace/command-line
)

还可以检索具有其定义及其要求的参数

$definition = new \Brzuchal\CommandLine\Definition([
    new \Brzuchal\CommandLine\ArgumentDefinition('command'),
    new \Brzuchal\CommandLine\OptionDefinition('env', 'e'),
    new \Brzuchal\CommandLine\OptionDefinition('file', 'f'),
    new \Brzuchal\CommandLine\OptionDefinition('count', 'c'),
]);
$parser = new \Brzuchal\CommandLine\ArrayParameterParser($_SERVER['argv']);
$parameters = $parser->parse();

它还会验证选项是否有值并且是否是必需的。解析命令 php test.php command --env=prod --debug -f -c 得到

PHP\CLI\CommandLine Object
(
    [command:protected] => test.php
    [parameters:protected] => Array
        (
            [0] => PHP\CLI\Argument Object
                (
                    [name:protected] => command
                    [value:protected] => command
                )

            [1] => PHP\CLI\Option Object
                (
                    [name:protected] => env
                    [value:protected] => prod
                )

            [2] => PHP\CLI\Option Object
                (
                    [name:protected] => debug
                    [value:protected] => 
                )

            [3] => PHP\CLI\Option Object
                (
                    [name:protected] => file
                    [value:protected] => 
                )

            [4] => PHP\CLI\Option Object
                (
                    [name:protected] => count
                    [value:protected] => 1
                )

        )

    [cwd:protected] => /home/brzuchal/Workspace/command-line
)

待办事项

  • 从数组解析选项
  • 从字符串解析选项
  • 验证必需值
  • 提供默认值
  • 从数组解析参数
  • 从字符串解析参数
  • 验证参数的存在性

许可证

MIT许可证 (MIT)

版权所有 (c) 2017 Michał Brzuchalski michal.brzuchalski@gmail.com

特此授予任何人免费获得此软件及其相关文档文件(以下简称“软件”)的副本的权利,无限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本,并允许向软件提供者提供软件的人这样做,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和不侵犯版权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论这些责任是因合同、侵权或其他方式引起的,无论是在软件或其使用或其他交易中产生的。