I can't seem to find any examples of code using getopt() around. I'm thinking not many 
people use php for command line shell scripts.

Here is what i came up with for getopt(); 

<?
$options = getopt("a:bc");

reset($options);
while(list($key, $value) = each($options)) {
    switch ($key) {
        case 'b':
            $B=1;
            break;
        case 'a':
            $A=$value;
            break;
        case 'c':
            $C=1;
            break;
        default:
            echo "unknown option";
            // print usage or something
            break;
    }
}
?>

The problem with this is it never hits default in the switch. If you give
an argument that it doesn't know about it accepts it without doing anything
and without an error, same thing if you don't give a value for an argument
thats supposed to have one (like -a above). Oh and --long arguments would be
nice too.

So why not use the Console/Getopt.php library you ask. Because i can't
figure out how to parse the output array. I can figure out the first part

<?
require 'Console/Getopt.php';

$optclass =  new Console_Getopt;
$args = $optclass->readPHPArgv();
array_shift($args);

$shortoptions = "a:bc";
$longoptions = array("long", "alsolong=", "doublelong==");

$options = $optclass->getopt( $args, $shortoptions, $longoptions);
print_r($options);
?>

But that returns an array with more then one dimension and i have no idea
how to loop though to set variables like i did with getopt().

My questions are what is the preferred way to get command line options, 
is there a way to give errors and take long arguments with getopt(), 
and how would one parse though the array returned from $optclass->getopt? 

Code examples of anyone using getopt(); or Getopt.php would be greatly
appreciated.

thanks

eli

PS anyone ever thought of setting up a website and maybe mailing list
devoted to php as a shell scripting language (not web scripting)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to