Hudzilla.org - the homepage of Paul Hudson
Contents > Alternative PHP uses > Command-line scripting Wish List | Report Bug | About Me ]

21.2.4     Advanced command-line parsing: getopt()

This is NOT the latest copy of this book; click here for the latest version.

array getopt ( string options [, array long_options])

Being able to accept parameters into your scripts with argc and argv is good enough for some people, but if you run on Unix there's an even better way: the getopt() function. With getopt() you just tell it what parameters your script is able to accept, and it does the rest.

Author's Note: getopt() doesn't work on Windows. You might try using the Console_GetOpt package from PEAR, available from http://pear.php.net/package/Console_Getopt

Here's a basic example:

<?php
    $options
= getopt("abc");
    
var_dump($options);
?>

That code tells getopt() to read in three parameters from the command line: -a, -b, and -c. If any of these are specified, they are placed into the array return value. Those that are not specified are simply left out of the array. Any extra parameters that were passed that aren't listed get ignored.

So, try running that script like this:

php getopt.php -a -b -d

That should output the following:

array(2) {
    ["a"]=>
    bool(false)
    ["b"]=>
    bool(false)
}

As you can see, as "c" wasn't specified and "d" wasn't asked for, neither of them make it into the returned array.

Of course, just knowing the "a" and "b" are set isn't very interesting - getopt() can also track values that parameters are set to. Change the script to this:

<?php
    $options
= getopt("ab:cd:");
    
var_dump($options);
?>

Note that I've changed the parameter passed to getopt() - there are now two colons in there, and we also accept a "d" parameter. What the two colons do is say that the parameter before expects to get some value attached to it. So, we can now call the script like this:

php getopt -a -b hello -d world

This time we only get "false" an array value when there was no value passed:

array(3) {
    ["a"]=>
    bool(false)
    ["b"]=>
    string(5) "hello"
    ["d"]=>
    string(5) "world"
}

There is one potential gotcha here, and that's the fact that the function isn't smart enough to allow optional values. Try calling the script like this:

php getopt -a -b -d world

There is a second parameter you can pass to getopt(), which is an array specifying the names of long parameters (that is, --foo rather than just -f) to accept. Sadly the functions behind it only work on a small number of systems; my test Linux boxes didn't work at all!





<< 21.2.3 Your first CLI script   21.2.5 Getting down and dirty >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow