You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
8.5 KiB

24 years ago
23 years ago
24 years ago
25 years ago
25 years ago
25 years ago
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Andrei Zmievski <andrei@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20. require_once 'PEAR.php';
  21. /**
  22. * Command-line options parsing class.
  23. *
  24. * @author Andrei Zmievski <andrei@php.net>
  25. *
  26. */
  27. class Console_Getopt {
  28. /**
  29. * Parses the command-line options.
  30. *
  31. * The first parameter to this function should be the list of command-line
  32. * arguments without the leading reference to the running program.
  33. *
  34. * The second parameter is a string of allowed short options. Each of the
  35. * option letters can be followed by a colon ':' to specify that the option
  36. * requires an argument, or a double colon '::' to specify that the option
  37. * takes an optional argument.
  38. *
  39. * The third argument is an optional array of allowed long options. The
  40. * leading '--' should not be included in the option name. Options that
  41. * require an argument should be followed by '=', and options that take an
  42. * option argument should be followed by '=='.
  43. *
  44. * The return value is an array of two elements: the list of parsed
  45. * options and the list of non-option command-line arguments. Each entry in
  46. * the list of parsed options is a pair of elements - the first one
  47. * specifies the option, and the second one specifies the option argument,
  48. * if there was one.
  49. *
  50. * Long and short options can be mixed.
  51. *
  52. * Most of the semantics of this function are based on GNU getopt_long().
  53. *
  54. * @param array $args an array of command-line arguments
  55. * @param string $short_options specifies the list of allowed short options
  56. * @param array $long_options specifies the list of allowed long options
  57. *
  58. * @return array two-element array containing the list of parsed options and
  59. * the non-option arguments
  60. *
  61. * @access public
  62. *
  63. */
  64. function getopt($args, $short_options, $long_options = null)
  65. {
  66. // in case you pass directly readPHPArgv() as the first arg
  67. if (PEAR::isError($args)) {
  68. return $args;
  69. }
  70. if (empty($args)) {
  71. return array(array(), array());
  72. }
  73. $opts = array();
  74. $non_opts = array();
  75. settype($args, 'array');
  76. if ($long_options) {
  77. sort($long_options);
  78. }
  79. if (isset($args[0]{0}) && $args[0]{0} != '-') {
  80. array_shift($args);
  81. }
  82. reset($args);
  83. while (list($i, $arg) = each($args)) {
  84. /* The special element '--' means explicit end of
  85. options. Treat the rest of the arguments as non-options
  86. and end the loop. */
  87. if ($arg == '--') {
  88. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  89. break;
  90. }
  91. if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
  92. $non_opts = array_merge($non_opts, array_slice($args, $i));
  93. break;
  94. } elseif (strlen($arg) > 1 && $arg{1} == '-') {
  95. $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
  96. if (PEAR::isError($error))
  97. return $error;
  98. } else {
  99. $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  100. if (PEAR::isError($error))
  101. return $error;
  102. }
  103. }
  104. return array($opts, $non_opts);
  105. }
  106. /**
  107. * @access private
  108. *
  109. */
  110. function _parseShortOption($arg, $short_options, &$opts, &$args)
  111. {
  112. for ($i = 0; $i < strlen($arg); $i++) {
  113. $opt = $arg{$i};
  114. $opt_arg = null;
  115. /* Try to find the short option in the specifier string. */
  116. if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
  117. {
  118. return PEAR::raiseError("Console_Getopt: unrecognized option -- $opt");
  119. }
  120. if (strlen($spec) > 1 && $spec{1} == ':') {
  121. if (strlen($spec) > 2 && $spec{2} == ':') {
  122. if ($i + 1 < strlen($arg)) {
  123. /* Option takes an optional argument. Use the remainder of
  124. the arg string if there is anything left. */
  125. $opts[] = array($opt, substr($arg, $i + 1));
  126. break;
  127. }
  128. } else {
  129. /* Option requires an argument. Use the remainder of the arg
  130. string if there is anything left. */
  131. if ($i + 1 < strlen($arg)) {
  132. $opts[] = array($opt, substr($arg, $i + 1));
  133. break;
  134. } else if (list(, $opt_arg) = each($args))
  135. /* Else use the next argument. */;
  136. else
  137. return PEAR::raiseError("Console_Getopt: option requires an argument -- $opt");
  138. }
  139. }
  140. $opts[] = array($opt, $opt_arg);
  141. }
  142. }
  143. /**
  144. * @access private
  145. *
  146. */
  147. function _parseLongOption($arg, $long_options, &$opts, &$args)
  148. {
  149. @list($opt, $opt_arg) = explode('=', $arg);
  150. $opt_len = strlen($opt);
  151. for ($i = 0; $i < count($long_options); $i++) {
  152. $long_opt = $long_options[$i];
  153. $opt_start = substr($long_opt, 0, $opt_len);
  154. /* Option doesn't match. Go on to the next one. */
  155. if ($opt_start != $opt)
  156. continue;
  157. $opt_rest = substr($long_opt, $opt_len);
  158. /* Check that the options uniquely matches one of the allowed
  159. options. */
  160. if ($opt_rest != '' && $opt{0} != '=' &&
  161. $i + 1 < count($long_options) &&
  162. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  163. return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous");
  164. }
  165. if (substr($long_opt, -1) == '=') {
  166. if (substr($long_opt, -2) != '==') {
  167. /* Long option requires an argument.
  168. Take the next argument if one wasn't specified. */;
  169. if (!$opt_arg && !(list(, $opt_arg) = each($args))) {
  170. return PEAR::raiseError("Console_Getopt: option --$opt requires an argument");
  171. }
  172. }
  173. } else if ($opt_arg) {
  174. return PEAR::raiseError("Console_Getopt: option --$opt doesn't allow an argument");
  175. }
  176. $opts[] = array('--' . $opt, $opt_arg);
  177. return;
  178. }
  179. return PEAR::raiseError("Console_Getopt: unrecognized option --$opt");
  180. }
  181. /**
  182. * Safely read the $argv PHP array across different PHP configurations.
  183. * Will take care on register_globals and register_argc_argv ini directives
  184. *
  185. * @access public
  186. * @return mixed the $argv PHP array or PEAR error if not registered
  187. */
  188. function readPHPArgv()
  189. {
  190. global $argv;
  191. if (!is_array($argv)) {
  192. if (!@is_array($_SERVER['argv'])) {
  193. if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
  194. return PEAR::raiseError("Console_Getopt: Could not read cmd args (register_argc_argv=Off?)");
  195. }
  196. return $GLOBALS['HTTP_SERVER_VARS']['argv'];
  197. }
  198. return $_SERVER['argv'];
  199. }
  200. return $argv;
  201. }
  202. }
  203. ?>