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

/iic_db/INC/Vendor/PHPUnit/Util/Getopt.php

http://iiccms.googlecode.com/
PHP | 183 lines | 99 code | 26 blank | 58 comment | 49 complexity | b991abff8c80c9bbc257e63c29a1ecdb MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2008, 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-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Getopt.php 3146 2008-06-08 07:56:46Z 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-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  56. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  57. * @version Release: 3.3.9
  58. * @link http://www.phpunit.de/
  59. * @since Class available since Release 3.0.0
  60. * @abstract
  61. */
  62. class PHPUnit_Util_Getopt {
  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. while (list($i, $arg) = each($args)) {
  78. if ($arg == '--') {
  79. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  80. break;
  81. }
  82. if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
  83. $non_opts = array_merge($non_opts, array_slice($args, $i));
  84. break;
  85. }
  86. elseif (strlen($arg) > 1 && $arg{1} == '-') {
  87. self::parseLongOption(substr($arg, 2), $long_options, $opts, $args);
  88. }
  89. else {
  90. self::parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  91. }
  92. }
  93. return array($opts, $non_opts);
  94. }
  95. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  96. {
  97. $argLen = strlen($arg);
  98. for ($i = 0; $i < $argLen; $i++) {
  99. $opt = $arg{$i};
  100. $opt_arg = NULL;
  101. if (($spec = strstr($short_options, $opt)) === FALSE || $arg{$i} == ':') {
  102. throw new RuntimeException("unrecognized option -- $opt");
  103. }
  104. if (strlen($spec) > 1 && $spec{1} == ':') {
  105. if (strlen($spec) > 2 && $spec{2} == ':') {
  106. if ($i + 1 < $argLen) {
  107. $opts[] = array($opt, substr($arg, $i + 1));
  108. break;
  109. }
  110. } else {
  111. if ($i + 1 < $argLen) {
  112. $opts[] = array($opt, substr($arg, $i + 1));
  113. break;
  114. }
  115. else if (list(, $opt_arg) = each($args)) {
  116. }
  117. else {
  118. throw new RuntimeException("option requires an argument -- $opt");
  119. }
  120. }
  121. }
  122. $opts[] = array($opt, $opt_arg);
  123. }
  124. }
  125. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  126. {
  127. @list($opt, $opt_arg) = explode('=', $arg);
  128. $opt_len = strlen($opt);
  129. for ($i = 0; $i < count($long_options); $i++) {
  130. $long_opt = $long_options[$i];
  131. $opt_start = substr($long_opt, 0, $opt_len);
  132. if ($opt_start != $opt) continue;
  133. $opt_rest = substr($long_opt, $opt_len);
  134. if ($opt_rest != '' && $opt{0} != '=' &&
  135. $i + 1 < count($long_options) &&
  136. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  137. throw new RuntimeException("option --$opt is ambiguous");
  138. }
  139. if (substr($long_opt, -1) == '=') {
  140. if (substr($long_opt, -2) != '==') {
  141. if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
  142. throw new RuntimeException("option --$opt requires an argument");
  143. }
  144. }
  145. }
  146. else if ($opt_arg) {
  147. throw new RuntimeException("option --$opt doesn't allow an argument");
  148. }
  149. $opts[] = array('--' . $opt, $opt_arg);
  150. return;
  151. }
  152. throw new RuntimeException("unrecognized option --$opt");
  153. }
  154. }
  155. ?>