PHP Funktion zum Parsen der Kommandozeilenargumente
PHP function to parse command line arguments
This little function parses command line parameter similar to getopt, except that you
don't need to specify what key-value-pairs you expect and the positions in the output
array are maintained.
Diese kleine Funktion parsed Kommandozeilen-Argumente, ähnlich wie getopt. Der Unterschied
liegt darin, dass das Ausgabearray die Reihenfolge der Argumente beibehält und Key-Value Paare
selbst erkennt. Lediglich bool'sche Flags (wie -h, ---help, --verbose etc) müssen im
Voraus bekannt sein.
Sample source code
Anwendungsbeispiel
if(php_sapi_name() == 'cli') {
$args = getargs(array('h', 'v', 'help', 'verbose'));
print print_r($args, true) . "\n";
}
Output
Ausgabe
$ php getargs.fn.php --verbose -v -f 'my-file' --file='my-other-file' -- dashes --not -interpreted here
Array
(
[0] => getargs.php
[verbose] => 1
[v] => 1
[f] => my-file
[file] => my-other-file
[1] => dashes
[2] => --not
[3] => -interpreted
[4] => here
)
Function source code
Funktion
<?php
/**
* Parses the command line arguments and detects parameters.
* You can specify boolean options (e.g. --help, -h, --verbose) that evaluate
* to TRUE if they appear in the arument list.
*
* Returns an array containing the key-value paramters and positional
* arguments.
*
* Parameters are detected allowing the following formats:
*
* --<KEY>=<VALUE>
* --<KEY>= <VALUE>
* --<KEY> = <VALUE>
* -<KEY> <VALUE>
* -<SINGLE CHARACTER BOOLEAN OPTION>
* --<BOOLEAN OPTION>
* -<OPT1><OPT2><OPT3><KEY> <VALUE>
* -<OPT> [other options] -- <ARGUMENTS HERE ARE INTERPRETED POSITIONAL>
*
* Examples:
*
* ./myscript -vczf 'file name' (e.g. like tar command)
* ./myscript -v -c -z -f 'file'
* ./myscript --verbose -c -z --file='file'
* ./myscript -abc --key= value -- only positional arguments follow
*
* @param array $bool_opts
* @return array
*/
function getargs($bool_opts = array()) {
if(!isset($GLOBALS['argv']) || empty ($GLOBALS['argv'])) {
return array();
}
$args = $GLOBALS['argv'];
$return = array();
reset($args);
while(list($k, $arg) = each($args)) {
if(strlen($arg) == 0) {
// argument is empty string '', as positional argument
$return[] = $arg;
} else if($arg == '--') {
// End of options, only operands follow (positional arguments)
while(list($k, $arg) = each($args)) {
$return[] = $arg;
}
return $return;
} else if(strpos($arg, '--') === 0) {
$arg = ltrim($arg, '-');
if(in_array($arg, $bool_opts)) {
$return[$arg] = true;
} else if(strpos($arg, '=') !== false) {
// Covers --arg=something, '--arg = something' and --arg=
$arg = explode('=', $arg, 2);
$return[trim($arg[0])] = trim($arg[1]);
} else {
// Problematic, could be --name '-=[MEEEEE]=-', or --verbose -k, we assume
// the first as --verbose should be otherwise defined in $bool_opts.
if (current($args) !== false) {
// There is another arg after this one --> value
list($k, $v) = each($args);
$return[$arg] = $v;
} else {
// Then a unspecified bool option? Preferred
$return[$arg] = '';
}
}
} else if($arg == '-') {
// This is an error as positional argument (if no value of another argument)
$return[] = $arg;
} else if(strpos($arg, '-') === 0) {
// "-" args can have only one character, can have a value following but
// no "=", like "-k=1". In short notation, the last character can denote
// a value arg.
$arg = str_split(trim($arg, '- '));
$last = array_pop($arg);
// The first are all bool
foreach($arg as $v) {
$return[$v] = true;
}
// The last has to be checked for bool, otherwise value arg
if(in_array($last, $bool_opts)) {
$return[$last] = true;
} else if (current($args) !== false) {
list($k, $v) = each($args);
$return[$last] = $v;
} else {
$return[$last] = true;
}
} else {
$return[] = $arg;
}
}
return $return;
}


