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

/Vendor/phpunit/phpunit/PHPUnit/Util/Getopt.php

https://bitbucket.org/daveschwan/ronin-group
PHP | 207 lines | 125 code | 27 blank | 55 comment | 50 complexity | 7e804e33078e1bb09fa0d8cd11e0981d MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2014, 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 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause 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 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
  53. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  54. * @link http://www.phpunit.de/
  55. * @since Class available since Release 3.0.0
  56. */
  57. class PHPUnit_Util_Getopt
  58. {
  59. public static function getopt(array $args, $short_options, $long_options = NULL)
  60. {
  61. if (empty($args)) {
  62. return array(array(), array());
  63. }
  64. $opts = array();
  65. $non_opts = array();
  66. if ($long_options) {
  67. sort($long_options);
  68. }
  69. if (isset($args[0][0]) && $args[0][0] != '-') {
  70. array_shift($args);
  71. }
  72. reset($args);
  73. array_map('trim', $args);
  74. while (list($i, $arg) = each($args)) {
  75. if ($arg == '') {
  76. continue;
  77. }
  78. if ($arg == '--') {
  79. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  80. break;
  81. }
  82. if ($arg[0] != '-' ||
  83. (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
  84. $non_opts = array_merge($non_opts, array_slice($args, $i));
  85. break;
  86. }
  87. elseif (strlen($arg) > 1 && $arg[1] == '-') {
  88. self::parseLongOption(
  89. substr($arg, 2), $long_options, $opts, $args
  90. );
  91. }
  92. else {
  93. self::parseShortOption(
  94. substr($arg, 1), $short_options, $opts, $args
  95. );
  96. }
  97. }
  98. return array($opts, $non_opts);
  99. }
  100. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  101. {
  102. $argLen = strlen($arg);
  103. for ($i = 0; $i < $argLen; $i++) {
  104. $opt = $arg[$i];
  105. $opt_arg = NULL;
  106. if (($spec = strstr($short_options, $opt)) === FALSE ||
  107. $arg[$i] == ':') {
  108. throw new PHPUnit_Framework_Exception(
  109. "unrecognized option -- $opt"
  110. );
  111. }
  112. if (strlen($spec) > 1 && $spec[1] == ':') {
  113. if (strlen($spec) > 2 && $spec[2] == ':') {
  114. if ($i + 1 < $argLen) {
  115. $opts[] = array($opt, substr($arg, $i + 1));
  116. break;
  117. }
  118. } else {
  119. if ($i + 1 < $argLen) {
  120. $opts[] = array($opt, substr($arg, $i + 1));
  121. break;
  122. }
  123. else if (list(, $opt_arg) = each($args)) {
  124. }
  125. else {
  126. throw new PHPUnit_Framework_Exception(
  127. "option requires an argument -- $opt"
  128. );
  129. }
  130. }
  131. }
  132. $opts[] = array($opt, $opt_arg);
  133. }
  134. }
  135. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  136. {
  137. $count = count($long_options);
  138. $list = explode('=', $arg);
  139. $opt = $list[0];
  140. $opt_arg = NULL;
  141. if (count($list) > 1) {
  142. $opt_arg = $list[1];
  143. }
  144. $opt_len = strlen($opt);
  145. for ($i = 0; $i < $count; $i++) {
  146. $long_opt = $long_options[$i];
  147. $opt_start = substr($long_opt, 0, $opt_len);
  148. if ($opt_start != $opt) {
  149. continue;
  150. }
  151. $opt_rest = substr($long_opt, $opt_len);
  152. if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
  153. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  154. throw new PHPUnit_Framework_Exception(
  155. "option --$opt is ambiguous"
  156. );
  157. }
  158. if (substr($long_opt, -1) == '=') {
  159. if (substr($long_opt, -2) != '==') {
  160. if (!strlen($opt_arg) &&
  161. !(list(, $opt_arg) = each($args))) {
  162. throw new PHPUnit_Framework_Exception(
  163. "option --$opt requires an argument"
  164. );
  165. }
  166. }
  167. }
  168. else if ($opt_arg) {
  169. throw new PHPUnit_Framework_Exception(
  170. "option --$opt doesn't allow an argument"
  171. );
  172. }
  173. $opts[] = array('--' . $opt, $opt_arg);
  174. return;
  175. }
  176. throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
  177. }
  178. }