PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/libs/PHPUnit-3.4.2/PHPUnit/Util/Getopt.php

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