PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/script/lib/PHPUnit/Util/Getopt.php

https://bitbucket.org/ywarnier/chamilo-dev
PHP | 220 lines | 136 code | 28 blank | 56 comment | 51 complexity | dbdc315b9a65c5f6790ccd63821f3fd8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Util
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.0.0
  44. */
  45. /**
  46. * Command-line options parsing class.
  47. *
  48. * @package PHPUnit
  49. * @subpackage Util
  50. * @author Andrei Zmievski <andrei@php.net>
  51. * @author Sebastian Bergmann <sebastian@phpunit.de>
  52. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  53. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  54. * @version Release: 3.5.9
  55. * @link http://www.phpunit.de/
  56. * @since Class available since Release 3.0.0
  57. */
  58. class PHPUnit_Util_Getopt
  59. {
  60. public static function getopt(array $args, $short_options, $long_options = NULL)
  61. {
  62. if (empty($args))
  63. {
  64. return array(array(), array());
  65. }
  66. $opts = array();
  67. $non_opts = array();
  68. if ($long_options)
  69. {
  70. sort($long_options);
  71. }
  72. if (isset($args[0][0]) && $args[0][0] != '-')
  73. {
  74. array_shift($args);
  75. }
  76. reset($args);
  77. array_map('trim', $args);
  78. while (list($i, $arg) = each($args))
  79. {
  80. if ($arg == '')
  81. {
  82. continue;
  83. }
  84. if ($arg == '--')
  85. {
  86. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  87. break;
  88. }
  89. if ($arg[0] != '-' || (strlen($arg) > 1 && $arg[1] == '-' && ! $long_options))
  90. {
  91. $non_opts = array_merge($non_opts, array_slice($args, $i));
  92. break;
  93. }
  94. elseif (strlen($arg) > 1 && $arg[1] == '-')
  95. {
  96. self :: parseLongOption(substr($arg, 2), $long_options, $opts, $args);
  97. }
  98. else
  99. {
  100. self :: parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  101. }
  102. }
  103. return array($opts, $non_opts);
  104. }
  105. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  106. {
  107. $argLen = strlen($arg);
  108. for($i = 0; $i < $argLen; $i ++)
  109. {
  110. $opt = $arg[$i];
  111. $opt_arg = NULL;
  112. if (($spec = strstr($short_options, $opt)) === FALSE || $arg[$i] == ':')
  113. {
  114. throw new PHPUnit_Framework_Exception("unrecognized option -- $opt");
  115. }
  116. if (strlen($spec) > 1 && $spec[1] == ':')
  117. {
  118. if (strlen($spec) > 2 && $spec[2] == ':')
  119. {
  120. if ($i + 1 < $argLen)
  121. {
  122. $opts[] = array($opt, substr($arg, $i + 1));
  123. break;
  124. }
  125. }
  126. else
  127. {
  128. if ($i + 1 < $argLen)
  129. {
  130. $opts[] = array($opt, substr($arg, $i + 1));
  131. break;
  132. }
  133. else
  134. if (list(, $opt_arg) = each($args))
  135. {
  136. }
  137. else
  138. {
  139. throw new PHPUnit_Framework_Exception("option requires an argument -- $opt");
  140. }
  141. }
  142. }
  143. $opts[] = array($opt, $opt_arg);
  144. }
  145. }
  146. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  147. {
  148. $count = count($long_options);
  149. $list = explode('=', $arg);
  150. $opt = $list[0];
  151. $opt_arg = NULL;
  152. if (count($list) > 1)
  153. {
  154. $opt_arg = $list[1];
  155. }
  156. $opt_len = strlen($opt);
  157. for($i = 0; $i < $count; $i ++)
  158. {
  159. $long_opt = $long_options[$i];
  160. $opt_start = substr($long_opt, 0, $opt_len);
  161. if ($opt_start != $opt)
  162. {
  163. continue;
  164. }
  165. $opt_rest = substr($long_opt, $opt_len);
  166. if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count && $opt == substr($long_options[$i + 1], 0, $opt_len))
  167. {
  168. throw new PHPUnit_Framework_Exception("option --$opt is ambiguous");
  169. }
  170. if (substr($long_opt, - 1) == '=')
  171. {
  172. if (substr($long_opt, - 2) != '==')
  173. {
  174. if (! strlen($opt_arg) && ! (list(, $opt_arg) = each($args)))
  175. {
  176. throw new PHPUnit_Framework_Exception("option --$opt requires an argument");
  177. }
  178. }
  179. }
  180. else
  181. if ($opt_arg)
  182. {
  183. throw new PHPUnit_Framework_Exception("option --$opt doesn't allow an argument");
  184. }
  185. $opts[] = array('--' . $opt, $opt_arg);
  186. return;
  187. }
  188. throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
  189. }
  190. }