PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pear/Console/Getopt.php

https://bitbucket.org/ceu/moodle_demo
PHP | 290 lines | 165 code | 23 blank | 102 comment | 70 complexity | c52501f978ee31500ed800426aed56ba MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 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.1.2.2 2008/02/27 02:58:24 martinlanghoff 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. } elseif ($arg == '-') {
  121. // - is stdin
  122. $non_opts = array_merge($non_opts, array_slice($args, $i));
  123. break;
  124. } else {
  125. $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  126. if (PEAR::isError($error))
  127. return $error;
  128. }
  129. }
  130. return array($opts, $non_opts);
  131. }
  132. /**
  133. * @access private
  134. *
  135. */
  136. function _parseShortOption($arg, $short_options, &$opts, &$args)
  137. {
  138. for ($i = 0; $i < strlen($arg); $i++) {
  139. $opt = $arg{$i};
  140. $opt_arg = null;
  141. /* Try to find the short option in the specifier string. */
  142. if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
  143. {
  144. return PEAR::raiseError("Console_Getopt: unrecognized option -- $opt");
  145. }
  146. if (strlen($spec) > 1 && $spec{1} == ':') {
  147. if (strlen($spec) > 2 && $spec{2} == ':') {
  148. if ($i + 1 < strlen($arg)) {
  149. /* Option takes an optional argument. Use the remainder of
  150. the arg string if there is anything left. */
  151. $opts[] = array($opt, substr($arg, $i + 1));
  152. break;
  153. }
  154. } else {
  155. /* Option requires an argument. Use the remainder of the arg
  156. string if there is anything left. */
  157. if ($i + 1 < strlen($arg)) {
  158. $opts[] = array($opt, substr($arg, $i + 1));
  159. break;
  160. } else if (list(, $opt_arg) = each($args)) {
  161. /* Else use the next argument. */;
  162. if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) {
  163. return PEAR::raiseError("Console_Getopt: option requires an argument -- $opt");
  164. }
  165. } else {
  166. return PEAR::raiseError("Console_Getopt: option requires an argument -- $opt");
  167. }
  168. }
  169. }
  170. $opts[] = array($opt, $opt_arg);
  171. }
  172. }
  173. /**
  174. * @access private
  175. *
  176. */
  177. function _isShortOpt($arg)
  178. {
  179. return strlen($arg) == 2 && $arg[0] == '-' && preg_match('/[a-zA-Z]/', $arg[1]);
  180. }
  181. /**
  182. * @access private
  183. *
  184. */
  185. function _isLongOpt($arg)
  186. {
  187. return strlen($arg) > 2 && $arg[0] == '-' && $arg[1] == '-' &&
  188. preg_match('/[a-zA-Z]+$/', substr($arg, 2));
  189. }
  190. /**
  191. * @access private
  192. *
  193. */
  194. function _parseLongOption($arg, $long_options, &$opts, &$args)
  195. {
  196. @list($opt, $opt_arg) = explode('=', $arg, 2);
  197. $opt_len = strlen($opt);
  198. for ($i = 0; $i < count($long_options); $i++) {
  199. $long_opt = $long_options[$i];
  200. $opt_start = substr($long_opt, 0, $opt_len);
  201. $long_opt_name = str_replace('=', '', $long_opt);
  202. /* Option doesn't match. Go on to the next one. */
  203. if ($long_opt_name != $opt) {
  204. continue;
  205. }
  206. $opt_rest = substr($long_opt, $opt_len);
  207. /* Check that the options uniquely matches one of the allowed
  208. options. */
  209. if ($i + 1 < count($long_options)) {
  210. $next_option_rest = substr($long_options[$i + 1], $opt_len);
  211. } else {
  212. $next_option_rest = '';
  213. }
  214. if ($opt_rest != '' && $opt{0} != '=' &&
  215. $i + 1 < count($long_options) &&
  216. $opt == substr($long_options[$i+1], 0, $opt_len) &&
  217. $next_option_rest != '' &&
  218. $next_option_rest{0} != '=') {
  219. return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous");
  220. }
  221. if (substr($long_opt, -1) == '=') {
  222. if (substr($long_opt, -2) != '==') {
  223. /* Long option requires an argument.
  224. Take the next argument if one wasn't specified. */;
  225. if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
  226. return PEAR::raiseError("Console_Getopt: option --$opt requires an argument");
  227. }
  228. if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) {
  229. return PEAR::raiseError("Console_Getopt: option requires an argument --$opt");
  230. }
  231. }
  232. } else if ($opt_arg) {
  233. return PEAR::raiseError("Console_Getopt: option --$opt doesn't allow an argument");
  234. }
  235. $opts[] = array('--' . $opt, $opt_arg);
  236. return;
  237. }
  238. return PEAR::raiseError("Console_Getopt: unrecognized option --$opt");
  239. }
  240. /**
  241. * Safely read the $argv PHP array across different PHP configurations.
  242. * Will take care on register_globals and register_argc_argv ini directives
  243. *
  244. * @access public
  245. * @return mixed the $argv PHP array or PEAR error if not registered
  246. */
  247. function readPHPArgv()
  248. {
  249. global $argv;
  250. if (!is_array($argv)) {
  251. if (!@is_array($_SERVER['argv'])) {
  252. if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
  253. return PEAR::raiseError("Console_Getopt: Could not read cmd args (register_argc_argv=Off?)");
  254. }
  255. return $GLOBALS['HTTP_SERVER_VARS']['argv'];
  256. }
  257. return $_SERVER['argv'];
  258. }
  259. return $argv;
  260. }
  261. }
  262. ?>