Example #1
0
 /**
  * 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;
 }
 /**
  * This function expects $args to start with the script name (POSIX-style).
  * Preserved for backwards compatibility.
  * @see getopt2()
  */
 function getopt($args, $short_options, $long_options = null)
 {
     return Console_Getopt::doGetopt(1, $args, $short_options, $long_options);
 }
Example #3
0
 /**
  * This function expects $args to start with the script name (POSIX-style).
  * Preserved for backwards compatibility.
  *
  * @param array  $args          an array of command-line arguments
  * @param string $short_options specifies the list of allowed short options
  * @param array  $long_options  specifies the list of allowed long options
  *
  * @see getopt2()
  * @return array two-element array containing the list of parsed options and
  * the non-option arguments
  */
 function getopt($args, $short_options, $long_options = null, $skip_unknown = false)
 {
     return Console_Getopt::doGetopt(1, $args, $short_options, $long_options, $skip_unknown);
 }