Reputation: 63597
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
Upvotes: 8
Views: 11030
Reputation: 306
You should use the getOpt function in PHP which supports short as well as long options in the command line:
https://www.php.net/manual/en/function.getopt.php
From the link given above:
From the shell:
php example.php -fvalue -h
Example: $options = getopt("f:hp:"); var_dump($options);
This will print: array(2) { ["f"]=> string(5) "value" ["h"]=> bool(false) }
Upvotes: 3
Reputation: 31078
You could use the https://github.com/pear/Console_CommandLine composer package to let it parse the options. (I'm one of the maintainers).
An example can be found in the documentation: https://pear.php.net/manual/en/package.console.console-commandline.intro.php
Upvotes: 3
Reputation: 3160
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;
}
Upvotes: 2
Reputation: 780879
$my_args = array();
for ($i = 1; $i < count($argv); $i++) {
if (preg_match('/^--([^=]+)=(.*)/', $argv[$i], $match)) {
$my_args[$match[1]] = $match[2];
}
}
Upvotes: 12