Getopt.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.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: Getopt.php,v 1.21.4.7 2003/12/05 21:57:01 andrei Exp $
  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 getopt2($args, $short_options, $long_options = null)
  65. {
  66. return Console_Getopt::doGetopt(2, $args, $short_options, $long_options);
  67. }
  68. /**
  69. * This function expects $args to start with the script name (POSIX-style).
  70. * Preserved for backwards compatibility.
  71. * @see getopt2()
  72. */
  73. function getopt($args, $short_options, $long_options = null)
  74. {
  75. return Console_Getopt::doGetopt(1, $args, $short_options, $long_options);
  76. }
  77. /**
  78. * The actual implementation of the argument parsing code.
  79. */
  80. function doGetopt($version, $args, $short_options, $long_options = null)
  81. {
  82. // in case you pass directly readPHPArgv() as the first arg
  83. if (PEAR::isError($args)) {
  84. return $args;
  85. }
  86. if (empty($args)) {
  87. return array(array(), array());
  88. }
  89. $opts = array();
  90. $non_opts = array();
  91. settype($args, 'array');
  92. if ($long_options) {
  93. sort($long_options);
  94. }
  95. /*
  96. * Preserve backwards compatibility with callers that relied on
  97. * erroneous POSIX fix.
  98. */
  99. if ($version < 2) {
  100. if (isset($args[0]{0}) && $args[0]{0} != '-') {
  101. array_shift($args);
  102. }
  103. }
  104. reset($args);
  105. while (list($i, $arg) = each($args)) {
  106. /* The special element '--' means explicit end of
  107. options. Treat the rest of the arguments as non-options
  108. and end the loop. */
  109. if ($arg == '--') {
  110. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  111. break;
  112. }
  113. if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
  114. $non_opts = array_merge($non_opts, array_slice($args, $i));
  115. break;
  116. } elseif (strlen($arg) > 1 && $arg{1} == '-') {
  117. $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
  118. if (PEAR::isError($error))
  119. return $error;
  120. } else {
  121. $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  122. if (PEAR::isError($error))
  123. return $error;
  124. }
  125. }
  126. return array($opts, $non_opts);
  127. }
  128. /**
  129. * @access private
  130. *
  131. */
  132. function _parseShortOption($arg, $short_options, &$opts, &$args)
  133. {
  134. for ($i = 0; $i < strlen($arg); $i++) {
  135. $opt = $arg{$i};
  136. $opt_arg = null;
  137. /* Try to find the short option in the specifier string. */
  138. if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
  139. {
  140. return PEAR::raiseError("Console_Getopt: unrecognized option -- $opt");
  141. }
  142. if (strlen($spec) > 1 && $spec{1} == ':') {
  143. if (strlen($spec) > 2 && $spec{2} == ':') {
  144. if ($i + 1 < strlen($arg)) {
  145. /* Option takes an optional argument. Use the remainder of
  146. the arg string if there is anything left. */
  147. $opts[] = array($opt, substr($arg, $i + 1));
  148. break;
  149. }
  150. } else {
  151. /* Option requires an argument. Use the remainder of the arg
  152. string if there is anything left. */
  153. if ($i + 1 < strlen($arg)) {
  154. $opts[] = array($opt, substr($arg, $i + 1));
  155. break;
  156. } else if (list(, $opt_arg) = each($args))
  157. /* Else use the next argument. */;
  158. else
  159. return PEAR::raiseError("Console_Getopt: option requires an argument -- $opt");
  160. }
  161. }
  162. $opts[] = array($opt, $opt_arg);
  163. }
  164. }
  165. /**
  166. * @access private
  167. *
  168. */
  169. function _parseLongOption($arg, $long_options, &$opts, &$args)
  170. {
  171. @list($opt, $opt_arg) = explode('=', $arg);
  172. $opt_len = strlen($opt);
  173. for ($i = 0; $i < count($long_options); $i++) {
  174. $long_opt = $long_options[$i];
  175. $opt_start = substr($long_opt, 0, $opt_len);
  176. /* Option doesn't match. Go on to the next one. */
  177. if ($opt_start != $opt)
  178. continue;
  179. $opt_rest = substr($long_opt, $opt_len);
  180. /* Check that the options uniquely matches one of the allowed
  181. options. */
  182. if ($opt_rest != '' && $opt{0} != '=' &&
  183. $i + 1 < count($long_options) &&
  184. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  185. return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous");
  186. }
  187. if (substr($long_opt, -1) == '=') {
  188. if (substr($long_opt, -2) != '==') {
  189. /* Long option requires an argument.
  190. Take the next argument if one wasn't specified. */;
  191. if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
  192. return PEAR::raiseError("Console_Getopt: option --$opt requires an argument");
  193. }
  194. }
  195. } else if ($opt_arg) {
  196. return PEAR::raiseError("Console_Getopt: option --$opt doesn't allow an argument");
  197. }
  198. $opts[] = array('--' . $opt, $opt_arg);
  199. return;
  200. }
  201. return PEAR::raiseError("Console_Getopt: unrecognized option --$opt");
  202. }
  203. /**
  204. * Safely read the $argv PHP array across different PHP configurations.
  205. * Will take care on register_globals and register_argc_argv ini directives
  206. *
  207. * @access public
  208. * @return mixed the $argv PHP array or PEAR error if not registered
  209. */
  210. function readPHPArgv()
  211. {
  212. global $argv;
  213. if (!is_array($argv)) {
  214. if (!@is_array($_SERVER['argv'])) {
  215. if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
  216. return PEAR::raiseError("Console_Getopt: Could not read cmd args (register_argc_argv=Off?)");
  217. }
  218. return $GLOBALS['HTTP_SERVER_VARS']['argv'];
  219. }
  220. return $_SERVER['argv'];
  221. }
  222. return $argv;
  223. }
  224. }
  225. ?>