PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPUnit/Util/Getopt.php

https://github.com/totallymeat/phpunit
PHP | 212 lines | 127 code | 28 blank | 57 comment | 50 complexity | cea35270779eef381d4288b88da1b62c MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.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. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.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. PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');
  46. /**
  47. * Command-line options parsing class.
  48. *
  49. * @category Testing
  50. * @package PHPUnit
  51. * @author Andrei Zmievski <andrei@php.net>
  52. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  53. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. * @version Release: @package_version@
  56. * @link http://www.phpunit.de/
  57. * @since Class available since Release 3.0.0
  58. * @abstract
  59. */
  60. class PHPUnit_Util_Getopt
  61. {
  62. public static function getopt(array $args, $short_options, $long_options = NULL)
  63. {
  64. if (empty($args)) {
  65. return array(array(), array());
  66. }
  67. $opts = array();
  68. $non_opts = array();
  69. if ($long_options) {
  70. sort($long_options);
  71. }
  72. if (isset($args[0]{0}) && $args[0]{0} != '-') {
  73. array_shift($args);
  74. }
  75. reset($args);
  76. array_map('trim', $args);
  77. while (list($i, $arg) = each($args)) {
  78. if ($arg == '') {
  79. continue;
  80. }
  81. if ($arg == '--') {
  82. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  83. break;
  84. }
  85. if ($arg{0} != '-' ||
  86. (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
  87. $non_opts = array_merge($non_opts, array_slice($args, $i));
  88. break;
  89. }
  90. elseif (strlen($arg) > 1 && $arg{1} == '-') {
  91. self::parseLongOption(
  92. substr($arg, 2), $long_options, $opts, $args
  93. );
  94. }
  95. else {
  96. self::parseShortOption(
  97. substr($arg, 1), $short_options, $opts, $args
  98. );
  99. }
  100. }
  101. return array($opts, $non_opts);
  102. }
  103. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  104. {
  105. $argLen = strlen($arg);
  106. for ($i = 0; $i < $argLen; $i++) {
  107. $opt = $arg{$i};
  108. $opt_arg = NULL;
  109. if (($spec = strstr($short_options, $opt)) === FALSE ||
  110. $arg{$i} == ':') {
  111. throw new PHPUnit_Framework_Exception(
  112. "unrecognized option -- $opt"
  113. );
  114. }
  115. if (strlen($spec) > 1 && $spec{1} == ':') {
  116. if (strlen($spec) > 2 && $spec{2} == ':') {
  117. if ($i + 1 < $argLen) {
  118. $opts[] = array($opt, substr($arg, $i + 1));
  119. break;
  120. }
  121. } else {
  122. if ($i + 1 < $argLen) {
  123. $opts[] = array($opt, substr($arg, $i + 1));
  124. break;
  125. }
  126. else if (list(, $opt_arg) = each($args)) {
  127. }
  128. else {
  129. throw new PHPUnit_Framework_Exception(
  130. "option requires an argument -- $opt"
  131. );
  132. }
  133. }
  134. }
  135. $opts[] = array($opt, $opt_arg);
  136. }
  137. }
  138. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  139. {
  140. $count = count($long_options);
  141. $list = explode('=', $arg);
  142. $opt = $list[0];
  143. $opt_arg = NULL;
  144. if (count($list) > 1) {
  145. $opt_arg = $list[1];
  146. }
  147. $opt_len = strlen($opt);
  148. for ($i = 0; $i < $count; $i++) {
  149. $long_opt = $long_options[$i];
  150. $opt_start = substr($long_opt, 0, $opt_len);
  151. if ($opt_start != $opt) {
  152. continue;
  153. }
  154. $opt_rest = substr($long_opt, $opt_len);
  155. if ($opt_rest != '' && $opt{0} != '=' && $i + 1 < $count &&
  156. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  157. throw new PHPUnit_Framework_Exception(
  158. "option --$opt is ambiguous"
  159. );
  160. }
  161. if (substr($long_opt, -1) == '=') {
  162. if (substr($long_opt, -2) != '==') {
  163. if (!strlen($opt_arg) &&
  164. !(list(, $opt_arg) = each($args))) {
  165. throw new PHPUnit_Framework_Exception(
  166. "option --$opt requires an argument"
  167. );
  168. }
  169. }
  170. }
  171. else if ($opt_arg) {
  172. throw new PHPUnit_Framework_Exception(
  173. "option --$opt doesn't allow an argument"
  174. );
  175. }
  176. $opts[] = array('--' . $opt, $opt_arg);
  177. return;
  178. }
  179. throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
  180. }
  181. }
  182. ?>