如何解析PHP中的命令行参数?[副本]

dauxcl2d  于 2023-05-16  发布在  PHP
关注(0)|答案(4)|浏览(95)

此问题已在此处有答案

PHP, pass parameters from command line to a PHP script(9个回答)
3天前关闭。
我从命令行$ php my_script.php --num1=124 --first_name=don调用PHP脚本
如何访问传入此脚本的 * 任何 * 键值对?键可以是任意的,所以使用getopt()和特定的值将不起作用。
以下是我希望在脚本中访问的内容:

$my_args = array(
  "num1" => 124,
  "first_name" => "don"
);

如果我使用var_dump($argv),我会得到这样的输出:

array(
  [0] => "my_script.php",
  [1] => "--num1=5",
  [2] => "--num2=123"
);

我是不是该看看

vmdwslir

vmdwslir1#

$my_args = array();
for ($i = 1; $i < count($argv); $i++) {
    if (preg_match('/^--([^=]+)=(.*)/', $argv[$i], $match)) {
        $my_args[$match[1]] = $match[2];
    }
}
8yoxcaq7

8yoxcaq72#

您可以使用https://github.com/pear/Console_CommandLine composer包来解析选项。(我是其中一个维护者)。
在文档中可以找到一个示例:https://pear.php.net/manual/en/package.console.console-commandline.intro.php

pbgvytdp

pbgvytdp3#

我已经使用了一些东西沿着这条线从PHP命令行
它会产生类似于

array(4) {
  ["commands"]=>
  array(0) {
  }
  ["options"]=>
  array(2) {
    ["num1"]=>
    string(1) "5"
    ["num2"]=>
    string(3) "123"
  }
  ["flags"]=>
  array(0) {
  }
  ["arguments"]=>
  array(0) {
  }
}
function arguments($args) {
    array_shift($args);
    $endofoptions = false;
    $ret = array(
        'commands' => array(),
        'options' => array(),
        'flags' => array(),
        'arguments' => array(),
    );
    while ($arg = array_shift($args)) {
        // if we have reached end of options,
        //we cast all remaining argvs as arguments
        if ($endofoptions) {
            $ret['arguments'][] = $arg;
            continue;
        }
        // Is it a command? (prefixed with --)
        if (substr($arg, 0, 2) === '--') {
            // is it the end of options flag?
            if (!isset($arg[3])) {
                $endofoptions = true;; // end of options;
                continue;
            }
            $value = "";
            $com = substr($arg, 2);
            // is it the syntax '--option=argument'?
            if (strpos($com, '='))
                list($com, $value) = split("=", $com, 2);
            // is the option not followed by another option but by arguments
            elseif(strpos($args[0], '-') !== 0) {
                while (strpos($args[0], '-') !== 0)
                    $value .= array_shift($args) . ' ';
                $value = rtrim($value, ' ');
            }
            $ret['options'][$com] = !empty($value) ? $value : true;
            continue;
        }
        // Is it a flag or a serial of flags? (prefixed with -)
        if (substr($arg, 0, 1) === '-') {
            for ($i = 1; isset($arg[$i]); $i++)
                $ret['flags'][] = $arg[$i];
            continue;
        }
        // finally, it is not option, nor flag, nor argument
        $ret['commands'][] = $arg;
        continue;
    }
    if (!count($ret['options']) && !count($ret['flags'])) {
        $ret['arguments'] = array_merge($ret['commands'], $ret['arguments']);
        $ret['commands'] = array();
    }
    return $ret;
}
vq8itlhq

vq8itlhq4#

你应该在PHP中使用getOpt函数,它在命令行中支持短和长选项:
https://www.php.net/manual/en/function.getopt.php
从上面给出的链接:
从shell:
php example.php -fvalue -h
$options = getopt(“f:hp:“); var_dump($options);
这将打印:数组(2){ [“f”]=> string(5)“value”[“h”]=> bool(false)}

相关问题