PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/php/external/PHPUnit/Util/Getopt.php

https://github.com/anirvan/shindig-profiles
PHP | 177 lines | 93 code | 26 blank | 58 comment | 51 complexity | b96a6820699b3ce42bb7cad62793579c MD5 | raw file
Possible License(s): Apache-2.0
  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 1985 2007-12-26 18:11:55Z 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.2.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. 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. while (list($i, $arg) = each($args)) {
  77. if ($arg == '--') {
  78. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  79. break;
  80. }
  81. if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && ! $long_options)) {
  82. $non_opts = array_merge($non_opts, array_slice($args, $i));
  83. break;
  84. }
  85. elseif (strlen($arg) > 1 && $arg{1} == '-') {
  86. self::parseLongOption(substr($arg, 2), $long_options, $opts, $args);
  87. }
  88. else {
  89. self::parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  90. }
  91. }
  92. return array($opts, $non_opts);
  93. }
  94. protected static function parseShortOption($arg, $short_options, &$opts, &$args) {
  95. for ($i = 0; $i < strlen($arg); $i ++) {
  96. $opt = $arg{$i};
  97. $opt_arg = null;
  98. if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':') {
  99. throw new RuntimeException("unrecognized option -- $opt");
  100. }
  101. if (strlen($spec) > 1 && $spec{1} == ':') {
  102. if (strlen($spec) > 2 && $spec{2} == ':') {
  103. if ($i + 1 < strlen($arg)) {
  104. $opts[] = array($opt, substr($arg, $i + 1));
  105. break;
  106. }
  107. } else {
  108. if ($i + 1 < strlen($arg)) {
  109. $opts[] = array($opt, substr($arg, $i + 1));
  110. break;
  111. }
  112. else if (list(, $opt_arg) = each($args)) {
  113. }
  114. else {
  115. throw new RuntimeException("option requires an argument -- $opt");
  116. }
  117. }
  118. }
  119. $opts[] = array($opt, $opt_arg);
  120. }
  121. }
  122. protected static function parseLongOption($arg, $long_options, &$opts, &$args) {
  123. @list($opt, $opt_arg) = explode('=', $arg);
  124. $opt_len = strlen($opt);
  125. for ($i = 0; $i < count($long_options); $i ++) {
  126. $long_opt = $long_options[$i];
  127. $opt_start = substr($long_opt, 0, $opt_len);
  128. if ($opt_start != $opt) continue;
  129. $opt_rest = substr($long_opt, $opt_len);
  130. if ($opt_rest != '' && $opt{0} != '=' && $i + 1 < count($long_options) && $opt == substr($long_options[$i + 1], 0, $opt_len)) {
  131. throw new RuntimeException("option --$opt is ambiguous");
  132. }
  133. if (substr($long_opt, - 1) == '=') {
  134. if (substr($long_opt, - 2) != '==') {
  135. if (! strlen($opt_arg) && ! (list(, $opt_arg) = each($args))) {
  136. throw new RuntimeException("option --$opt requires an argument");
  137. }
  138. }
  139. }
  140. else if ($opt_arg) {
  141. throw new RuntimeException("option --$opt doesn't allow an argument");
  142. }
  143. $opts[] = array('--' . $opt, $opt_arg);
  144. return;
  145. }
  146. throw new RuntimeException("unrecognized option --$opt");
  147. }
  148. }
  149. ?>