One of the cool things about PHP is the abundance of ready made built-in functions.
I write a lot of PHP shell scripts.
PHP, like almost any other proper language, accepts arguments in a $argv array, where the 1st argument ($argv[0]) is the filename, and then all of the rest of the arguments.
But what happens if you don't always want to accept the parameters as a command-line argument?
Then you either have to pass a predefined 'placeholder' that nominates 'use default value', or you have to define flags, and build a parsing mechanism to cut off the first character or two, and then define what to do with the rest.
This is where getopt() comes in handy.
All you have to do is define a. the different options the script can accept. b. if the option is required and/or accompanied with an additional argument.
example:
$options = getopt("ab:c:C::");
a is a standalone - no additional argument required (obviously - not required).
b: is required.
so is c: .
C:: (Capital C) is optional. (PHP 5.3 and up).
$options is an (associative) array of all passed parameters (with arguments, if applies).
Now that's effective.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment