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

/src/Util/Getopt.php

https://github.com/benmatselby/phpunit
PHP | 164 lines | 122 code | 22 blank | 20 comment | 46 complexity | 4f08d356f8b36317a3802287498ec30a MD5 | raw file
Possible License(s): BSD-3-Clause
  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. * @package PHPUnit
  14. * @subpackage Util
  15. * @author Andrei Zmievski <andrei@php.net>
  16. * @author Sebastian Bergmann <sebastian@phpunit.de>
  17. * @copyright Sebastian Bergmann <sebastian@phpunit.de>
  18. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  19. * @link http://www.phpunit.de/
  20. * @since Class available since Release 3.0.0
  21. */
  22. class PHPUnit_Util_Getopt
  23. {
  24. public static function getopt(array $args, $short_options, $long_options = null)
  25. {
  26. if (empty($args)) {
  27. return array(array(), array());
  28. }
  29. $opts = array();
  30. $non_opts = array();
  31. if ($long_options) {
  32. sort($long_options);
  33. }
  34. if (isset($args[0][0]) && $args[0][0] != '-') {
  35. array_shift($args);
  36. }
  37. reset($args);
  38. array_map('trim', $args);
  39. while (list($i, $arg) = each($args)) {
  40. if ($arg == '') {
  41. continue;
  42. }
  43. if ($arg == '--') {
  44. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  45. break;
  46. }
  47. if ($arg[0] != '-' ||
  48. (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) {
  49. $non_opts = array_merge($non_opts, array_slice($args, $i));
  50. break;
  51. } elseif (strlen($arg) > 1 && $arg[1] == '-') {
  52. self::parseLongOption(
  53. substr($arg, 2), $long_options, $opts, $args
  54. );
  55. } else {
  56. self::parseShortOption(
  57. substr($arg, 1), $short_options, $opts, $args
  58. );
  59. }
  60. }
  61. return array($opts, $non_opts);
  62. }
  63. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  64. {
  65. $argLen = strlen($arg);
  66. for ($i = 0; $i < $argLen; $i++) {
  67. $opt = $arg[$i];
  68. $opt_arg = null;
  69. if (($spec = strstr($short_options, $opt)) === false ||
  70. $arg[$i] == ':') {
  71. throw new PHPUnit_Framework_Exception(
  72. "unrecognized option -- $opt"
  73. );
  74. }
  75. if (strlen($spec) > 1 && $spec[1] == ':') {
  76. if (strlen($spec) > 2 && $spec[2] == ':') {
  77. if ($i + 1 < $argLen) {
  78. $opts[] = array($opt, substr($arg, $i + 1));
  79. break;
  80. }
  81. } else {
  82. if ($i + 1 < $argLen) {
  83. $opts[] = array($opt, substr($arg, $i + 1));
  84. break;
  85. } elseif (list(, $opt_arg) = each($args)) {
  86. } else {
  87. throw new PHPUnit_Framework_Exception(
  88. "option requires an argument -- $opt"
  89. );
  90. }
  91. }
  92. }
  93. $opts[] = array($opt, $opt_arg);
  94. }
  95. }
  96. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  97. {
  98. $count = count($long_options);
  99. $list = explode('=', $arg);
  100. $opt = $list[0];
  101. $opt_arg = null;
  102. if (count($list) > 1) {
  103. $opt_arg = $list[1];
  104. }
  105. $opt_len = strlen($opt);
  106. for ($i = 0; $i < $count; $i++) {
  107. $long_opt = $long_options[$i];
  108. $opt_start = substr($long_opt, 0, $opt_len);
  109. if ($opt_start != $opt) {
  110. continue;
  111. }
  112. $opt_rest = substr($long_opt, $opt_len);
  113. if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
  114. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  115. throw new PHPUnit_Framework_Exception(
  116. "option --$opt is ambiguous"
  117. );
  118. }
  119. if (substr($long_opt, -1) == '=') {
  120. if (substr($long_opt, -2) != '==') {
  121. if (!strlen($opt_arg) &&
  122. !(list(, $opt_arg) = each($args))) {
  123. throw new PHPUnit_Framework_Exception(
  124. "option --$opt requires an argument"
  125. );
  126. }
  127. }
  128. } elseif ($opt_arg) {
  129. throw new PHPUnit_Framework_Exception(
  130. "option --$opt doesn't allow an argument"
  131. );
  132. }
  133. $full_option = '--' . preg_replace('/={1,2}$/', '', $long_opt);
  134. $opts[] = array($full_option, $opt_arg);
  135. return;
  136. }
  137. throw new PHPUnit_Framework_Exception("unrecognized option --$opt");
  138. }
  139. }