PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tr/src/lmbTestGetopt.class.php

https://bitbucket.org/idler/mmp/
PHP | 305 lines | 164 code | 27 blank | 114 comment | 67 complexity | dc540c47d80d5d04884a0c7c9aa163e3 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * Class based on PEAR Console_Getopt, it's main goal is to
  11. * remove any external dependencies from TESTS_RUNNER package
  12. */
  13. // +----------------------------------------------------------------------+
  14. // | PHP Version 5 |
  15. // +----------------------------------------------------------------------+
  16. // | Copyright (c) 1997-2004 The PHP Group |
  17. // +----------------------------------------------------------------------+
  18. // | This source file is subject to version 3.0 of the PHP license, |
  19. // | that is bundled with this package in the file LICENSE, and is |
  20. // | available through the world-wide-web at the following url: |
  21. // | http://www.php.net/license/3_0.txt. |
  22. // | If you did not receive a copy of the PHP license and are unable to |
  23. // | obtain it through the world-wide-web, please send a note to |
  24. // | license@php.net so we can mail you a copy immediately. |
  25. // +----------------------------------------------------------------------+
  26. // | Author: Andrei Zmievski <andrei@php.net> |
  27. // +----------------------------------------------------------------------+
  28. //
  29. // $Id: Getopt.php,v 1.32 2007/02/18 04:13:07 cellog Exp $
  30. /**
  31. * Command-line options parsing class.
  32. *
  33. * @author Andrei Zmievski <andrei@php.net>
  34. *
  35. * @package tests_runner
  36. * @version $Id$
  37. */
  38. class lmbTestGetopt {
  39. /**
  40. * Parses the command-line options.
  41. *
  42. * The first parameter to this static function should be the list of command-line
  43. * arguments without the leading reference to the running program.
  44. *
  45. * The second parameter is a string of allowed short options. Each of the
  46. * option letters can be followed by a colon ':' to specify that the option
  47. * requires an argument, or a double colon '::' to specify that the option
  48. * takes an optional argument.
  49. *
  50. * The third argument is an optional array of allowed long options. The
  51. * leading '--' should not be included in the option name. Options that
  52. * require an argument should be followed by '=', and options that take an
  53. * option argument should be followed by '=='.
  54. *
  55. * The return value is an array of two elements: the list of parsed
  56. * options and the list of non-option command-line arguments. Each entry in
  57. * the list of parsed options is a pair of elements - the first one
  58. * specifies the option, and the second one specifies the option argument,
  59. * if there was one.
  60. *
  61. * Long and short options can be mixed.
  62. *
  63. * Most of the semantics of this static function are based on GNU getopt_long().
  64. *
  65. * @param array $args an array of command-line arguments
  66. * @param string $short_options specifies the list of allowed short options
  67. * @param array $long_options specifies the list of allowed long options
  68. *
  69. * @return array two-element array containing the list of parsed options and
  70. * the non-option arguments
  71. *
  72. * @access public
  73. *
  74. */
  75. static function getopt2($args, $short_options, $long_options = null)
  76. {
  77. return lmbTestGetopt::doGetopt(2, $args, $short_options, $long_options);
  78. }
  79. /**
  80. * This static function expects $args to start with the script name (POSIX-style).
  81. * Preserved for backwards compatibility.
  82. * @see getopt2()
  83. */
  84. static function getopt($args, $short_options, $long_options = null)
  85. {
  86. return lmbTestGetopt::doGetopt(1, $args, $short_options, $long_options);
  87. }
  88. static function defineAndExtractConstants(&$argv)
  89. {
  90. $filtered = array();
  91. for($i=0;$i<sizeof($argv);$i++) {
  92. //Windows cmd.exe uses '=' symbols as separators
  93. //that's why we need to fetch the next argument as well
  94. if(preg_match('~^[A-Z_][A-Z0-9_]+$~', $argv[$i]) && isset($argv[$i+1])) {
  95. @define($argv[$i], $argv[$i+1]);
  96. $i++;
  97. }
  98. elseif(preg_match('~^([A-Z_][A-Z0-9_]+)=(.*)$~', $argv[$i], $m)) {
  99. @define($m[1], $m[2]);
  100. } else {
  101. $filtered[] = $argv[$i];
  102. }
  103. }
  104. $argv = $filtered;
  105. }
  106. /**
  107. * The actual implementation of the argument parsing code.
  108. */
  109. static function doGetopt($version, $args, $short_options, $long_options = null)
  110. {
  111. if (empty($args)) {
  112. return array(array(), array());
  113. }
  114. $opts = array();
  115. $non_opts = array();
  116. settype($args, 'array');
  117. if ($long_options) {
  118. sort($long_options);
  119. }
  120. /*
  121. * Preserve backwards compatibility with callers that relied on
  122. * erroneous POSIX fix.
  123. */
  124. if ($version < 2) {
  125. if (isset($args[0]{0}) && $args[0]{0} != '-') {
  126. array_shift($args);
  127. }
  128. }
  129. reset($args);
  130. while (list($i, $arg) = each($args)) {
  131. /* The special element '--' means explicit end of
  132. options. Treat the rest of the arguments as non-options
  133. and end the loop. */
  134. if ($arg == '--') {
  135. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  136. break;
  137. }
  138. if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
  139. $non_opts = array_merge($non_opts, array_slice($args, $i));
  140. break;
  141. } elseif (strlen($arg) > 1 && $arg{1} == '-') {
  142. lmbTestGetopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
  143. } else {
  144. lmbTestGetopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  145. }
  146. }
  147. return array($opts, $non_opts);
  148. }
  149. /**
  150. * @access private
  151. *
  152. */
  153. static function _parseShortOption($arg, $short_options, &$opts, &$args)
  154. {
  155. for ($i = 0; $i < strlen($arg); $i++) {
  156. $opt = $arg{$i};
  157. $opt_arg = null;
  158. /* Try to find the short option in the specifier string. */
  159. if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
  160. {
  161. throw new Exception("unrecognized option -- $opt");
  162. }
  163. if (strlen($spec) > 1 && $spec{1} == ':') {
  164. if (strlen($spec) > 2 && $spec{2} == ':') {
  165. if ($i + 1 < strlen($arg)) {
  166. /* Option takes an optional argument. Use the remainder of
  167. the arg string if there is anything left. */
  168. $opts[] = array($opt, substr($arg, $i + 1));
  169. break;
  170. }
  171. } else {
  172. /* Option requires an argument. Use the remainder of the arg
  173. string if there is anything left. */
  174. if ($i + 1 < strlen($arg)) {
  175. $opts[] = array($opt, substr($arg, $i + 1));
  176. break;
  177. } else if (list(, $opt_arg) = each($args)) {
  178. /* Else use the next argument. */;
  179. if (lmbTestGetopt::_isShortOpt($opt_arg) || lmbTestGetopt::_isLongOpt($opt_arg)) {
  180. throw new Exception("option requires an argument -- $opt");
  181. }
  182. } else {
  183. throw new Exception("option requires an argument -- $opt");
  184. }
  185. }
  186. }
  187. $opts[] = array($opt, $opt_arg);
  188. }
  189. }
  190. /**
  191. * @access private
  192. *
  193. */
  194. static function _isShortOpt($arg)
  195. {
  196. return strlen($arg) == 2 && $arg[0] == '-' && preg_match('/[a-zA-Z]/', $arg[1]);
  197. }
  198. /**
  199. * @access private
  200. *
  201. */
  202. static function _isLongOpt($arg)
  203. {
  204. return strlen($arg) > 2 && $arg[0] == '-' && $arg[1] == '-' &&
  205. preg_match('/[a-zA-Z]+$/', substr($arg, 2));
  206. }
  207. /**
  208. * @access private
  209. *
  210. */
  211. static function _parseLongOption($arg, $long_options, &$opts, &$args)
  212. {
  213. @list($opt, $opt_arg) = explode('=', $arg, 2);
  214. $opt_len = strlen($opt);
  215. for ($i = 0; $i < count($long_options); $i++) {
  216. $long_opt = $long_options[$i];
  217. $opt_start = substr($long_opt, 0, $opt_len);
  218. $long_opt_name = str_replace('=', '', $long_opt);
  219. /* Option doesn't match. Go on to the next one. */
  220. if ($long_opt_name != $opt) {
  221. continue;
  222. }
  223. $opt_rest = substr($long_opt, $opt_len);
  224. /* Check that the options uniquely matches one of the allowed
  225. options. */
  226. $next_option_rest = '';
  227. if(isset($long_options[$i + 1]))
  228. $next_option_rest = substr($long_options[$i + 1], $opt_len);
  229. if ($opt_rest != '' && $opt{0} != '=' &&
  230. $i + 1 < count($long_options) &&
  231. $opt == substr($long_options[$i+1], 0, $opt_len) &&
  232. $next_option_rest != '' &&
  233. $next_option_rest{0} != '=') {
  234. throw new Exception("option --$opt is ambiguous");
  235. }
  236. if (substr($long_opt, -1) == '=') {
  237. if (substr($long_opt, -2) != '==') {
  238. /* Long option requires an argument.
  239. Take the next argument if one wasn't specified. */;
  240. if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
  241. throw new Exception("option --$opt requires an argument");
  242. }
  243. }
  244. } else if ($opt_arg) {
  245. throw new Exception("option --$opt doesn't allow an argument");
  246. }
  247. $opts[] = array('--' . $opt, $opt_arg);
  248. return;
  249. }
  250. throw new Exception("unrecognized option --$opt");
  251. }
  252. /**
  253. * Safely read the $argv PHP array across different PHP configurations.
  254. * Will take care on register_globals and register_argc_argv ini directives
  255. *
  256. * @access public
  257. * @return mixed the $argv PHP array or PEAR error if not registered
  258. */
  259. static function readPHPArgv()
  260. {
  261. global $argv;
  262. if (!is_array($argv)) {
  263. if (!@is_array($_SERVER['argv'])) {
  264. if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
  265. throw new Exception("Could not read cmd args (register_argc_argv=Off?)");
  266. }
  267. return $GLOBALS['HTTP_SERVER_VARS']['argv'];
  268. }
  269. return $_SERVER['argv'];
  270. }
  271. return $argv;
  272. }
  273. }