PageRenderTime 80ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phpunit/phpunit/src/Util/Getopt.php

https://gitlab.com/jhonn/rest
PHP | 199 lines | 122 code | 22 blank | 55 comment | 46 complexity | 8fd9c4afea12db61822036b855c08990 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. } elseif (strlen($arg) > 1 && $arg[1] == '-') {
  87. self::parseLongOption(
  88. substr($arg, 2), $long_options, $opts, $args
  89. );
  90. } else {
  91. self::parseShortOption(
  92. substr($arg, 1), $short_options, $opts, $args
  93. );
  94. }
  95. }
  96. return array($opts, $non_opts);
  97. }
  98. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  99. {
  100. $argLen = strlen($arg);
  101. for ($i = 0; $i < $argLen; $i++) {
  102. $opt = $arg[$i];
  103. $opt_arg = null;
  104. if (($spec = strstr($short_options, $opt)) === false ||
  105. $arg[$i] == ':') {
  106. throw new PHPUnit_Framework_Exception(
  107. "unrecognized option -- $opt"
  108. );
  109. }
  110. if (strlen($spec) > 1 && $spec[1] == ':') {
  111. if (strlen($spec) > 2 && $spec[2] == ':') {
  112. if ($i + 1 < $argLen) {
  113. $opts[] = array($opt, substr($arg, $i + 1));
  114. break;
  115. }
  116. } else {
  117. if ($i + 1 < $argLen) {
  118. $opts[] = array($opt, substr($arg, $i + 1));
  119. break;
  120. } elseif (list(, $opt_arg) = each($args)) {
  121. } else {
  122. throw new PHPUnit_Framework_Exception(
  123. "option requires an argument -- $opt"
  124. );
  125. }
  126. }
  127. }
  128. $opts[] = array($opt, $opt_arg);
  129. }
  130. }
  131. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  132. {
  133. $count = count($long_options);
  134. $list = explode('=', $arg);
  135. $opt = $list[0];
  136. $opt_arg = null;
  137. if (count($list) > 1) {
  138. $opt_arg = $list[1];
  139. }
  140. $opt_len = strlen($opt);
  141. for ($i = 0; $i < $count; $i++) {
  142. $long_opt = $long_options[$i];
  143. $opt_start = substr($long_opt, 0, $opt_len);
  144. if ($opt_start != $opt) {
  145. continue;
  146. }
  147. $opt_rest = substr($long_opt, $opt_len);
  148. if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
  149. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  150. throw new PHPUnit_Framework_Exception(
  151. "option --$opt is ambiguous"
  152. );
  153. }
  154. if (substr($long_opt, -1) == '=') {
  155. if (substr($long_opt, -2) != '==') {
  156. if (!strlen($opt_arg) &&
  157. !(list(, $opt_arg) = each($args))) {
  158. throw new PHPUnit_Framework_Exception(
  159. "option --$opt requires an argument"
  160. );
  161. }
  162. }
  163. } elseif ($opt_arg) {
  164. throw new PHPUnit_Framework_Exception(
  165. "option --$opt doesn't allow an argument"
  166. );
  167. }
  168. $full_option = '--' . preg_replace('/={1,2}$/', '', $long_opt);
  169. $opts[] = array($full_option, $opt_arg);
  170. return;
  171. }
  172. throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
  173. }
  174. }