note 75918 added to features.commandline
| From: | (http://unbiskant.org) | Date: | Thu, 21 Jun 2007 13:25:43 +0000 |
| Subject: | note 75918 added to features.commandline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127861@lists.php.net to get a copy of this message | ||
I wrote this function to parse all command-line arguments.
It parses both all kinds of options (-u or -uao or --user=admin or --verbose or -p asdf and all
kinds of combinations between them) and commands (like cvs update), and returns them in an array. It
works both on Windows and Unix/Linux/etc.
<?php
/**
* Parse console arguments.
*
* @param $args
* The console argument array (usually $argv)
* @param $arg_opts
* An array of options which are to be followed by an argument.
* This is needed to seperate these arguments of options from commands.
* e.g. shell.php -u admin -v
* --> $arg_opts = array('u')
* @param $default_options
* @return
* A associative array:
* $return['commands'] ia a numeric array of all commands,
* $return['options'] contains the options. The option keys
* are always noted without - or -- and are set to TRUE if they were
* invoked, to the argument if followed by an argument, and if not present
* to their default value or FALSE if no default value was specified.
*
* @author Franz Heinzmann (frando) < http://unbiskant.org
>
* @license GNU GPL Version 2 < http://www.gnu.org/licenses/gpl.txt >
*/
function drush_parse_args($args = array(), $arg_opts = array(), $default_options = array()) {
$options = $default_options;
$commands = array();
for ($i = 1; $i < count($args); $i++) {
$opt = $args[$i];
// Is the arg an option (starting with '-')?
if ($opt{0} == "-" && strlen($opt) != 1) {
// Do we have multiple options behind one '-'?
if (strlen($opt) > 2 && $opt{1} != "-") {
// Each char becomes a key of its own.
for ($j = 1; $j < strlen($opt); $j++) {
$options[substr($opt, $j, 1)] = true;
}
}
// Do we have a longopt (starting with '--')?
elseif ($opt{1} == "-") {
if ($pos = strpos($opt, '=')) {
$options[substr($opt, 2, $pos - 2)] = substr($opt, $pos + 1);
}
else {
$options[substr($opt, 2)] = true;
}
}
else {
$opt = substr($opt, 1);
// Check if the current opt is in $arg_opts (= has to be followed by an argument).
if ((in_array($opt, $arg_opts))) {
if (($args[$i+1] == NULL) || ($args[$i+1] == "") || ($args[$i + 1]{0} ==
"-")) {
exit("Invalid input: -$opt needs to be followed by an argument.");
}
$options[$opt] = $args[$i + 1];
$i++;
}
else {
$options[$opt] = true;
}
}
}
// If it's not an option, it's a command.
else {
$commands[] = $opt;
}
}
return array('options' => $options, 'commands' => $commands);
}
?>
----
Server IP: 217.160.72.57
Probable Submitter: 88.64.130.33
----
Manual Page -- http://www.php.net/manual/en/features.commandline.php
Edit -- https://master.php.net/note/edit/75918
Del: integrated -- https://master.php.net/note/delete/75918/integrated
Del: useless -- https://master.php.net/note/delete/75918/useless
Del: bad code -- https://master.php.net/note/delete/75918/bad+code
Del: spam -- https://master.php.net/note/delete/75918/spam
Del: non-english -- https://master.php.net/note/delete/75918/non-english
Del: in docs -- https://master.php.net/note/delete/75918/in+docs
Del: other reasons-- https://master.php.net/note/delete/75918
Reject -- https://master.php.net/note/reject/75918
Search -- https://master.php.net/manage/user-notes.php