PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/pear/php/PHPCPD/TextUI/Getopt.php

https://github.com/wrobel/horde-glue
PHP | 173 lines | 97 code | 24 blank | 52 comment | 49 complexity | de994bdcf503c022a1aff3915508c9a2 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * phpcpd
  4. *
  5. * Copyright (c) 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. * @package phpcpd
  38. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  39. * @copyright 2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  41. * @since File available since Release 1.0.0
  42. */
  43. /**
  44. * Command-line options parsing class.
  45. *
  46. * @author Andrei Zmievski <andrei@php.net>
  47. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  48. * @copyright 2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  49. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  50. * @version Release: 1.1.1
  51. * @link http://github.com/sebastianbergmann/phpcpd/tree
  52. * @since Class available since Release 1.0.0
  53. */
  54. class PHPCPD_TextUI_Getopt {
  55. public static function getopt(array $args, $short_options, $long_options = NULL)
  56. {
  57. if (empty($args)) {
  58. return array(array(), array());
  59. }
  60. $opts = array();
  61. $non_opts = array();
  62. if ($long_options) {
  63. sort($long_options);
  64. }
  65. if (isset($args[0]{0}) && $args[0]{0} != '-') {
  66. array_shift($args);
  67. }
  68. reset($args);
  69. while (list($i, $arg) = each($args)) {
  70. if ($arg == '--') {
  71. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  72. break;
  73. }
  74. if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
  75. $non_opts = array_merge($non_opts, array_slice($args, $i));
  76. break;
  77. }
  78. elseif (strlen($arg) > 1 && $arg{1} == '-') {
  79. self::parseLongOption(substr($arg, 2), $long_options, $opts, $args);
  80. }
  81. else {
  82. self::parseShortOption(substr($arg, 1), $short_options, $opts, $args);
  83. }
  84. }
  85. return array($opts, $non_opts);
  86. }
  87. protected static function parseShortOption($arg, $short_options, &$opts, &$args)
  88. {
  89. $argLen = strlen($arg);
  90. for ($i = 0; $i < $argLen; $i++) {
  91. $opt = $arg{$i};
  92. $opt_arg = NULL;
  93. if (($spec = strstr($short_options, $opt)) === FALSE || $arg{$i} == ':') {
  94. throw new RuntimeException("unrecognized option -- $opt");
  95. }
  96. if (strlen($spec) > 1 && $spec{1} == ':') {
  97. if (strlen($spec) > 2 && $spec{2} == ':') {
  98. if ($i + 1 < $argLen) {
  99. $opts[] = array($opt, substr($arg, $i + 1));
  100. break;
  101. }
  102. } else {
  103. if ($i + 1 < $argLen) {
  104. $opts[] = array($opt, substr($arg, $i + 1));
  105. break;
  106. }
  107. else if (list(, $opt_arg) = each($args)) {
  108. }
  109. else {
  110. throw new RuntimeException("option requires an argument -- $opt");
  111. }
  112. }
  113. }
  114. $opts[] = array($opt, $opt_arg);
  115. }
  116. }
  117. protected static function parseLongOption($arg, $long_options, &$opts, &$args)
  118. {
  119. @list($opt, $opt_arg) = explode('=', $arg);
  120. $opt_len = strlen($opt);
  121. for ($i = 0; $i < count($long_options); $i++) {
  122. $long_opt = $long_options[$i];
  123. $opt_start = substr($long_opt, 0, $opt_len);
  124. if ($opt_start != $opt) continue;
  125. $opt_rest = substr($long_opt, $opt_len);
  126. if ($opt_rest != '' && $opt{0} != '=' &&
  127. $i + 1 < count($long_options) &&
  128. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  129. throw new RuntimeException("option --$opt is ambiguous");
  130. }
  131. if (substr($long_opt, -1) == '=') {
  132. if (substr($long_opt, -2) != '==') {
  133. if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
  134. throw new RuntimeException("option --$opt requires an argument");
  135. }
  136. }
  137. }
  138. else if ($opt_arg) {
  139. throw new RuntimeException("option --$opt doesn't allow an argument");
  140. }
  141. $opts[] = array('--' . $opt, $opt_arg);
  142. return;
  143. }
  144. throw new RuntimeException("unrecognized option --$opt");
  145. }
  146. }
  147. ?>