/** * Gets the options and parameters from the command arguments * * Extracts the command arguments. Tidies the options. * Builds the help/usage text. * * @param array $config the command configuration * @param boolean $toLongNames Converts options names to long names if true, * e.g. "align", or to short names if false, * e.g. "A", default is true * @param boolean $asKeys return options with the names used as array * keys if true, or leave them as returned by * Console_Getopt::doGetopt(), default is true * @param integer $version Console_Getopt::doGetopt version * @return array the options and parameters * @access public * @static */ public static function getopt($config = array(), $toLongNames = true, $asKeys = true, $version = 2) { $getopt = new self(); // silently ignore invalid configuration arrays, defaults the absent ones to empty arrays $default = array_combine($getopt->configKeys, array_fill(0, count($getopt->configKeys), array())); $config = array_intersect_key($config, $default); $config = array_merge($default, $config); // extracts the command arguments, including the command name $args = Console_Getopt::readPHPArgv(); $command = array_shift($args); // tidies the options, sets the short and long options, and calls getopt $config['options'] = $getopt->tidyOptions($config['options']); $options = Console_Getopt::doGetopt($version, $args, $getopt->shortOptions, $getopt->longOptions); // a syntax error, prints out the error message PEAR::isError($options) and exit($options->getMessage()); // tidies the arguments $options[0] = $getopt->tidyArgs($options[0], $toLongNames, $asKeys); // a request for help/usage, prints the command usage $options[0] == 'help' and exit(implode("\n", $getopt->setHelp($config, $command))); return $options; }