PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/phpunit/src/Util/Getopt.php

https://gitlab.com/ealexis.t/trends
PHP | 163 lines | 128 code | 22 blank | 13 comment | 46 complexity | 42f53e3f03af6b361732bae8d71132fd MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Command-line options parsing class.
  12. *
  13. * @since Class available since Release 3.0.0
  14. */
  15. class PHPUnit_Util_Getopt
  16. {
  17. public static function getopt(array $args, $short_options, $long_options = null)
  18. {
  19. if (empty($args)) {
  20. return array(array(), array());
  21. }
  22. $opts = array();
  23. $non_opts = array();
  24. if ($long_options) {
  25. sort($long_options);
  26. }
  27. if (isset($args[0][0]) && $args[0][0] != '-') {
  28. array_shift($args);
  29. }
  30. reset($args);
  31. array_map('trim', $args);
  32. while (list($i, $arg) = each($args)) {
  33. if ($arg == '') {
  34. continue;
  35. }
  36. if ($arg == '--') {
  37. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  38. break;
  39. }
  40. if ($arg[0] != '-' ||
  41. (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
  42. $non_opts[] = $args[$i];
  43. continue;
  44. } elseif (strlen($arg) > 1 && $arg[1] == '-') {
  45. self::parseLongOption(
  46. substr($arg, 2),
  47. $long_options,
  48. $opts,
  49. $args
  50. );
  51. } else {
  52. self::parseShortOption(
  53. substr($arg, 1),
  54. $short_options,
  55. $opts,
  56. $args
  57. );
  58. }
  59. }
  60. return array($opts, $non_opts);
  61. }
  62. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  63. {
  64. $argLen = strlen($arg);
  65. for ($i = 0; $i < $argLen; $i++) {
  66. $opt = $arg[$i];
  67. $opt_arg = null;
  68. if (($spec = strstr($short_options, $opt)) === false ||
  69. $arg[$i] == ':') {
  70. throw new PHPUnit_Framework_Exception(
  71. "unrecognized option -- $opt"
  72. );
  73. }
  74. if (strlen($spec) > 1 && $spec[1] == ':') {
  75. if (strlen($spec) > 2 && $spec[2] == ':') {
  76. if ($i + 1 < $argLen) {
  77. $opts[] = array($opt, substr($arg, $i + 1));
  78. break;
  79. }
  80. } else {
  81. if ($i + 1 < $argLen) {
  82. $opts[] = array($opt, substr($arg, $i + 1));
  83. break;
  84. } elseif (list(, $opt_arg) = each($args)) {
  85. } else {
  86. throw new PHPUnit_Framework_Exception(
  87. "option requires an argument -- $opt"
  88. );
  89. }
  90. }
  91. }
  92. $opts[] = array($opt, $opt_arg);
  93. }
  94. }
  95. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  96. {
  97. $count = count($long_options);
  98. $list = explode('=', $arg);
  99. $opt = $list[0];
  100. $opt_arg = null;
  101. if (count($list) > 1) {
  102. $opt_arg = $list[1];
  103. }
  104. $opt_len = strlen($opt);
  105. for ($i = 0; $i < $count; $i++) {
  106. $long_opt = $long_options[$i];
  107. $opt_start = substr($long_opt, 0, $opt_len);
  108. if ($opt_start != $opt) {
  109. continue;
  110. }
  111. $opt_rest = substr($long_opt, $opt_len);
  112. if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
  113. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  114. throw new PHPUnit_Framework_Exception(
  115. "option --$opt is ambiguous"
  116. );
  117. }
  118. if (substr($long_opt, -1) == '=') {
  119. if (substr($long_opt, -2) != '==') {
  120. if (!strlen($opt_arg) &&
  121. !(list(, $opt_arg) = each($args))) {
  122. throw new PHPUnit_Framework_Exception(
  123. "option --$opt requires an argument"
  124. );
  125. }
  126. }
  127. } elseif ($opt_arg) {
  128. throw new PHPUnit_Framework_Exception(
  129. "option --$opt doesn't allow an argument"
  130. );
  131. }
  132. $full_option = '--' . preg_replace('/={1,2}$/', '', $long_opt);
  133. $opts[] = array($full_option, $opt_arg);
  134. return;
  135. }
  136. throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
  137. }
  138. }