Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse command line arguments in PHP?

I call a PHP script from command line $ php my_script.php --num1=124 --first_name=don

How can I get access to any key value pairs passed into this script? The keys can be arbitrary, so using getopt() with particular values will not work.

Here is what I want access to in my script:

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

If I use var_dump($argv), I get this output:

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

Should I just look

like image 633
Don P Avatar asked Oct 23 '14 01:10

Don P


2 Answers

$my_args = array();
for ($i = 1; $i < count($argv); $i++) {
    if (preg_match('/^--([^=]+)=(.*)/', $argv[$i], $match)) {
        $my_args[$match[1]] = $match[2];
    }
}
like image 140
Barmar Avatar answered Sep 28 '22 16:09

Barmar


I have used something along the lines of this from PHP commandline

and it will produce something like

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;
}
like image 34
Class Avatar answered Sep 28 '22 15:09

Class