PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/unit-test/phpunit-portable/Util/Getopt.php

http://buddypress-media.googlecode.com/
PHP | 208 lines | 125 code | 27 blank | 56 comment | 50 complexity | 8e71edf5fe9fafdc403682a0b8929b1a MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2011, 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 2002-2011 Sebastian Bergmann <sebastian@phpunit.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. /**
  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 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  53. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  54. * @version Release: 3.5.13
  55. * @link http://www.phpunit.de/
  56. * @since Class available since Release 3.0.0
  57. */
  58. class PHPUnit_Util_Getopt
  59. {
  60. public static function getopt(array $args, $short_options, $long_options = NULL)
  61. {
  62. if (empty($args)) {
  63. return array(array(), array());
  64. }
  65. $opts = array();
  66. $non_opts = array();
  67. if ($long_options) {
  68. sort($long_options);
  69. }
  70. if (isset($args[0][0]) && $args[0][0] != '-') {
  71. array_shift($args);
  72. }
  73. reset($args);
  74. array_map('trim', $args);
  75. while (list($i, $arg) = each($args)) {
  76. if ($arg == '') {
  77. continue;
  78. }
  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. $count = count($long_options);
  139. $list = explode('=', $arg);
  140. $opt = $list[0];
  141. $opt_arg = NULL;
  142. if (count($list) > 1) {
  143. $opt_arg = $list[1];
  144. }
  145. $opt_len = strlen($opt);
  146. for ($i = 0; $i < $count; $i++) {
  147. $long_opt = $long_options[$i];
  148. $opt_start = substr($long_opt, 0, $opt_len);
  149. if ($opt_start != $opt) {
  150. continue;
  151. }
  152. $opt_rest = substr($long_opt, $opt_len);
  153. if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
  154. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  155. throw new PHPUnit_Framework_Exception(
  156. "option --$opt is ambiguous"
  157. );
  158. }
  159. if (substr($long_opt, -1) == '=') {
  160. if (substr($long_opt, -2) != '==') {
  161. if (!strlen($opt_arg) &&
  162. !(list(, $opt_arg) = each($args))) {
  163. throw new PHPUnit_Framework_Exception(
  164. "option --$opt requires an argument"
  165. );
  166. }
  167. }
  168. }
  169. else if ($opt_arg) {
  170. throw new PHPUnit_Framework_Exception(
  171. "option --$opt doesn't allow an argument"
  172. );
  173. }
  174. $opts[] = array('--' . $opt, $opt_arg);
  175. return;
  176. }
  177. throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
  178. }
  179. }