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

/bindings/php/generator/Getopt.php

https://code.google.com/p/oscats/
PHP | 188 lines | 88 code | 21 blank | 79 comment | 46 complexity | 50581de6c03a5c7484ea48b4acc52ce3 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Andrei Zmievski <andrei@ispi.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Getopt.php,v 1.3 2005/03/29 19:17:32 andrei Exp $
  20. /**
  21. * Command-line options parsing class.
  22. *
  23. * @author Andrei Zmievski <andrei@ispi.net>
  24. *
  25. */
  26. class Console_Getopt {
  27. /**
  28. * Parses the command-line options.
  29. *
  30. * The first parameter to this function should be the list of command-line
  31. * arguments without the leading reference to the running program.
  32. *
  33. * The second parameter is a string of allowed short options. Each of the
  34. * option letters can be followed by a colon ':' to specify that the option
  35. * requires an argument, or a double colon '::' to specify that the option
  36. * takes an optional argument.
  37. *
  38. * The third argument is an optional array of allowed long options. The
  39. * leading '--' should not be included in the option name. Options that
  40. * require an argument should be followed by '=', and options that take an
  41. * option argument should be followed by '=='.
  42. *
  43. * The return value is an array of two elements: the list of parsed
  44. * options and the list of non-option command-line arguments. Each entry in
  45. * the list of parsed options is a pair of elements - the first one
  46. * specifies the option, and the second one specifies the option argument,
  47. * if there was one.
  48. *
  49. * Long and short options can be mixed.
  50. *
  51. * Most of the semantics of this function are based on GNU getopt_long().
  52. *
  53. * @param $args array an array of command-line arguments
  54. * @param $short_options string specifies the list of allowed short options
  55. * @param $long_options array specifies the list of allowed long options
  56. *
  57. * @return array two-element array containing the list of parsed options and
  58. * the non-option arguments
  59. *
  60. * @access public
  61. *
  62. */
  63. function getopt($args, $short_options, $long_options = null)
  64. {
  65. $opts = array();
  66. $non_opts = array();
  67. settype($args, 'array');
  68. if ($long_options)
  69. sort($long_options);
  70. reset($args);
  71. while (list(, $arg) = each($args)) {
  72. /* The special element '--' means explicit end of options. Treat the
  73. rest of the arguments as non-options and end the loop. */
  74. if ($arg == '--') {
  75. $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
  76. break;
  77. }
  78. if ($arg{0} != '-' || ($arg{1} == '-' && !$long_options)) {
  79. $non_opts[] = $arg;
  80. } else if ($arg{1} == '-') {
  81. if (!Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args))
  82. return false;
  83. } else {
  84. if (!Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args))
  85. return false;
  86. }
  87. }
  88. return array($opts, $non_opts);
  89. }
  90. /**
  91. * @access private
  92. *
  93. */
  94. function _parseShortOption($arg, $short_options, &$opts, &$args)
  95. {
  96. for ($i = 0; $i < strlen($arg); $i++) {
  97. $opt = $arg{$i};
  98. $opt_arg = null;
  99. /* Try to find the short option in the specifier string. */
  100. if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
  101. {
  102. return false;
  103. }
  104. if (strlen($spec) > 1 && $spec{1} == ':') {
  105. if (strlen($spec) > 2 && $spec{2} == ':') {
  106. if ($i + 1 < strlen($arg)) {
  107. /* Option takes an optional argument. Use the remainder of
  108. the arg string if there is anything left. */
  109. $opts[] = array($opt, substr($arg, $i + 1));
  110. break;
  111. }
  112. } else {
  113. /* Option requires an argument. Use the remainder of the arg
  114. string if there is anything left. */
  115. if ($i + 1 < strlen($arg)) {
  116. $opts[] = array($opt, substr($arg, $i + 1));
  117. break;
  118. } else if (list(, $opt_arg) = each($args))
  119. /* Else use the next argument. */;
  120. else
  121. return false;
  122. }
  123. }
  124. $opts[] = array($opt, $opt_arg);
  125. return true;
  126. }
  127. }
  128. /**
  129. * @access private
  130. *
  131. */
  132. function _parseLongOption($arg, $long_options, &$opts, &$args)
  133. {
  134. list($opt, $opt_arg) = explode('=', $arg);
  135. $opt_len = strlen($opt);
  136. for ($i = 0; $i < count($long_options); $i++) {
  137. $long_opt = $long_options[$i];
  138. $opt_start = substr($long_opt, 0, $opt_len);
  139. /* Option doesn't match. Go on to the next one. */
  140. if ($opt_start != $opt)
  141. continue;
  142. $opt_rest = substr($long_opt, $opt_len);
  143. /* Check that the options uniquely matches one of the allowed
  144. options. */
  145. if ($opt_rest != '' && $opt{0} != '=' &&
  146. $i + 1 < count($long_options) &&
  147. $opt == substr($long_options[$i+1], 0, $opt_len)) {
  148. return false;
  149. }
  150. if (substr($long_opt, -1) == '=') {
  151. if (substr($long_opt, -2) != '==') {
  152. /* Long option requires an argument.
  153. Take the next argument if one wasn't specified. */;
  154. if (!$opt_arg && !(list(, $opt_arg) = each($args))) {
  155. return false;
  156. }
  157. }
  158. } else if ($opt_arg) {
  159. return false;
  160. }
  161. $opts[] = array('--' . substr($long_opt, 0, strpos($long_opt, '=')), $opt_arg);
  162. return true;
  163. }
  164. return false;
  165. }
  166. }
  167. ?>