566 Part IV: Using PHP for Sysadmin Tasks (Web hosting company)
566 Part IV: Using PHP for Sysadmin Tasks Listing 16-4 (Continued) function getCommandLineOptions($options) { $type = gettype($options); if (gettype($options) != array ) { // Error in command line echo $options->message n ; return null; } $cmd = array(); foreach ($options[0] as $argArray) { $argName = preg_replace( /[^w]/ , , $argArray[0]); $argValue = $argArray[1]; $cmd[$argName] = ($argValue != ) ? $argValue : TRUE; } return (count($cmd) > 0) ? $cmd : null; } ?> Here, getCommandLineOptions() is passed the output of the Console_Getopt::getopt() function. The Console_Getopt::getopt() function takes $GLOBALS[ argv ], $CMD_SHORT_OPTIONS, and $CMD_LONG_OPTIONS as arguments. The $GLOBALS[ argv ] argument is same as $argv, which holds the user- supplied command-line arguments. The second argument, $CMD_SHORT_OPTIONS, is a list of short argument options such as hs: . These are the options that a user can enter as h and -s size. The -s option takes a value and therefore : is added in the $CMD_SHORT_OPTIONS string to indicate that. The $CMD_LONG_OPTIONS is set to an array such as array( help , size= ). These arguments can be entered by the user as help and –size=size.