Last active
January 10, 2022 17:09
-
-
Save kodie/31aa6b29544f9c09e134e07242c4966e to your computer and use it in GitHub Desktop.
Parse $argv in PHP get the command, arguments, options, and flags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Parses $argv to get the command, arguments, options, and flags. | |
* | |
* @param array $args The $argv variable. | |
* @param array $short_opts Optional. An array with keys set to short options and their values set to the long option they're assigned to. | |
* @param array $flag_opts Optional. An array of options to be treated as flags. If a flag is not defined here, it will be treated as an option. | |
* | |
* @return array An array with the command, arguments, options, and flags keys. | |
*/ | |
function parse_argv($args, $short_opts = array(), $flag_opts = array()) { | |
$command = array_shift($args); | |
$arguments = array(); | |
$options = array(); | |
$flags = array(); | |
$last_opt = null; | |
foreach($args as $arg) { | |
$value = null; | |
if (substr($arg, 0, 1) !== '-') { | |
if ($last_opt) { | |
$value = $arg; | |
$arg = $last_opt; | |
} else { | |
$arguments[] = $arg; | |
$last_opt = null; | |
continue; | |
} | |
} else { | |
$arg_split = array(); | |
preg_match('/^--?([A-Z\d\-_]+)=?(.+)?$/i', $arg, $arg_split); | |
$arg = $arg_split[1]; | |
if (count($arg_split) > 2) $value = $arg_split[2]; | |
} | |
if (array_key_exists($arg, $short_opts)) $arg = $short_opts[$arg]; | |
if (in_array($arg, $flag_opts)) { | |
if (!in_array($arg, $flags)) $flags[] = $arg; | |
$last_opt = null; | |
} else { | |
if (array_key_exists($arg, $options)) { | |
if (is_array($options[$arg])) { | |
$options[$arg][] = $value; | |
} else { | |
if (is_null($options[$arg])) { | |
$options[$arg] = $value; | |
} elseif (!is_null($value)) { | |
$options[$arg] = array($options[$arg], $value); | |
} | |
} | |
} else { | |
$options[$arg] = $value; | |
} | |
$last_opt = $value ? null : $arg; | |
} | |
} | |
return compact('command', 'arguments', 'options', 'flags'); | |
} | |
// Example | |
$argv = array( | |
'bin/my-command', | |
'argument', | |
'--option', | |
'option-value', | |
'--flag', | |
'-x', | |
'option2-value', | |
'-o=option-value2', | |
'argument2', | |
'-t' | |
); | |
$cli = parse_argv( | |
$argv, | |
array( | |
'o' => 'option', | |
't' => 'test', | |
'x' => 'option-2' | |
), | |
array('flag', 'test') | |
); | |
print_r($cli); | |
/* Output: | |
Array | |
( | |
[command] => bin/my-command | |
[arguments] => Array | |
( | |
[0] => argument | |
[1] => argument2 | |
) | |
[options] => Array | |
( | |
[option] => Array | |
( | |
[0] => option-value | |
[1] => option-value2 | |
) | |
[option-2] => option2-value | |
) | |
[flags] => Array | |
( | |
[0] => flag | |
[1] => test | |
) | |
) | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment