PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPUnit/Util/Getopt.php

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