PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPUnit/Util/Getopt.php

https://github.com/item/sugarcrm_dev
PHP | 183 lines | 99 code | 26 blank | 58 comment | 49 complexity | 66d1a3ac3260bd1f3100e1a0578c3769 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  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. * @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-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: 3.3.17
  57. * @link http://www.phpunit.de/
  58. * @since Class available since Release 3.0.0
  59. * @abstract
  60. */
  61. class PHPUnit_Util_Getopt {
  62. public static function getopt(array $args, $short_options, $long_options = NULL)
  63. {
  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. {
  96. $argLen = strlen($arg);
  97. for ($i = 0; $i < $argLen; $i++) {
  98. $opt = $arg{$i};
  99. $opt_arg = NULL;
  100. if (($spec = strstr($short_options, $opt)) === FALSE || $arg{$i} == ':') {
  101. throw new RuntimeException("unrecognized option -- $opt");
  102. }
  103. if (strlen($spec) > 1 && $spec{1} == ':') {
  104. if (strlen($spec) > 2 && $spec{2} == ':') {
  105. if ($i + 1 < $argLen) {
  106. $opts[] = array($opt, substr($arg, $i + 1));
  107. break;
  108. }
  109. } else {
  110. if ($i + 1 < $argLen) {
  111. $opts[] = array($opt, substr($arg, $i + 1));
  112. break;
  113. }
  114. else if (list(, $opt_arg) = each($args)) {
  115. }
  116. else {
  117. throw new RuntimeException("option requires an argument -- $opt");
  118. }
  119. }
  120. }
  121. $opts[] = array($opt, $opt_arg);
  122. }
  123. }
  124. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  125. {
  126. @list($opt, $opt_arg) = explode('=', $arg);
  127. $opt_len = strlen($opt);
  128. for ($i = 0; $i < count($long_options); $i++) {
  129. $long_opt = $long_options[$i];
  130. $opt_start = substr($long_opt, 0, $opt_len);
  131. if ($opt_start != $opt) continue;
  132. $opt_rest = substr($long_opt, $opt_len);
  133. if ($opt_rest != '' && $opt{0} != '=' &&
  134. $i + 1 < count($long_options) &&
  135. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  136. throw new RuntimeException("option --$opt is ambiguous");
  137. }
  138. if (substr($long_opt, -1) == '=') {
  139. if (substr($long_opt, -2) != '==') {
  140. if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
  141. throw new RuntimeException("option --$opt requires an argument");
  142. }
  143. }
  144. }
  145. else if ($opt_arg) {
  146. throw new RuntimeException("option --$opt doesn't allow an argument");
  147. }
  148. $opts[] = array('--' . $opt, $opt_arg);
  149. return;
  150. }
  151. throw new RuntimeException("unrecognized option --$opt");
  152. }
  153. }
  154. ?>