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

/classes/Xinc/Config/Getopt.php

http://xinc.googlecode.com/
PHP | 181 lines | 95 code | 19 blank | 67 comment | 52 complexity | 04bed1c2eaa93c9e465612d1f848e7db MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. declare(encoding = 'utf-8');
  3. /**
  4. * Xinc - Continuous Integration.
  5. * Parser for xinc command-line options.
  6. *
  7. * PHP version 5
  8. *
  9. * @category Development
  10. * @package Xinc.Config
  11. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  12. * @author Jamie Talbot <username@example.org>
  13. * @copyright 2002-2007 Sebastian Bergmann <sb@sebastian-bergmann.de>
  14. * @license http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php
  15. * This file is part of Xinc.
  16. * Xinc is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU Lesser General Public License as
  18. * published by the Free Software Foundation; either version 2.1 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * Xinc is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public
  27. * License along with Xinc, write to the Free Software Foundation,
  28. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. * @link http://xincplus.sourceforge.net
  30. */
  31. require_once 'Xinc/Config/Exception/Getopt.php';
  32. /**
  33. * Command-line options parsing class.
  34. *
  35. * @package Xinc.Config
  36. * @author Andrei Zmievski <andrei@php.net>
  37. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  38. * @author Jamie Talbot
  39. * @copyright 2002-2007 Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  41. */
  42. class Xinc_Config_Getopt
  43. {
  44. /**
  45. * Enter description here...
  46. *
  47. * @param array $args
  48. * @param unknown_type $shortOptions
  49. * @param unknown_type $longOptions
  50. * @return unknown
  51. * @throws Xinc_Config_Exception_Getopt
  52. */
  53. public static function getopt(array $args, $shortOptions, $longOptions = null)
  54. {
  55. if (empty($args)) {
  56. return array(array(), array());
  57. }
  58. $opts = array();
  59. $nonOpts = array();
  60. if ($longOptions) {
  61. sort($longOptions);
  62. }
  63. if (isset($args[0]{0}) && $args[0]{0} != '-') {
  64. array_shift($args);
  65. }
  66. reset($args);
  67. while (list($i, $arg) = each($args)) {
  68. if (empty($arg)) {
  69. continue;
  70. }
  71. if ($arg == '--') {
  72. $nonOpts = array_merge($nonOpts, array_slice($args, $i + 1));
  73. break;
  74. }
  75. if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$longOptions)) {
  76. // This argument is a project file, but there could be other
  77. // options following, so just take the next one and continue.
  78. $nonOpts = array_merge($nonOpts, array_slice($args, $i, 1));
  79. } else if (strlen($arg) > 1 && $arg{1} == '-') {
  80. self::parseLongOption(substr($arg, 2), $longOptions, $opts, $args);
  81. } else {
  82. self::parseShortOption(substr($arg, 1), $shortOptions, $opts, $args);
  83. }
  84. }
  85. return array($opts, $nonOpts);
  86. }
  87. /**
  88. * Parses short options from the command line.
  89. *
  90. * @param string $arg The argument to parse.
  91. * @param string $short_options Short options to match.
  92. * @param array $opts The options accumulator.
  93. * @param array $args The arguments passed to the option.
  94. * @throws Xinc_Config_Exception_Getopt If the option is unrecognised, or missing a required value.
  95. */
  96. protected static function parseShortOption($arg, $shortOptions, &$opts, &$args)
  97. {
  98. for ($i = 0; $i < strlen($arg); $i++) {
  99. $opt = $arg{$i};
  100. $optArg = null;
  101. if (($spec = strstr($shortOptions, $opt)) === false || $arg{$i} == ':') {
  102. throw new Xinc_Config_Exception_Getopt("unrecognized option -- $opt");
  103. }
  104. if (strlen($spec) > 1 && $spec{1} == ':') {
  105. if (strlen($spec) > 2 && $spec{2} == ':') {
  106. if ($i + 1 < strlen($arg)) {
  107. $opts[] = array($opt, substr($arg, $i + 1));
  108. break;
  109. }
  110. } else {
  111. if ($i + 1 < strlen($arg)) {
  112. $opts[] = array($opt, substr($arg, $i + 1));
  113. break;
  114. } else if (list(, $optArg) = each($args)) {
  115. } else {
  116. throw new Xinc_Config_Exception_Getopt("option requires an argument -- $opt");
  117. }
  118. }
  119. }
  120. $opts[] = array($opt, $optArg);
  121. }
  122. }
  123. /**
  124. * Parses long options from the command line.
  125. *
  126. * @param string $arg The argument to parse.
  127. * @param array $long_options Long options to match.
  128. * @param array $opts The options accumulator.
  129. * @param array $args The arguments passed to the option.
  130. * @throws Xinc_Config_Exception_Getopt If the option is unrecognised, or missing a required value, or ambiguous.
  131. */
  132. protected static function parseLongOption($arg, $longOptions, &$opts, &$args)
  133. {
  134. @list($opt, $optArg) = explode('=', $arg);
  135. $optLen = strlen($opt);
  136. for ($i = 0; $i < count($longOptions); $i++) {
  137. $longOpt = $longOptions[$i];
  138. $optStart = substr($longOpt, 0, $optLen);
  139. if ($optStart != $opt) continue;
  140. $optRest = substr($longOpt, $optLen);
  141. if ($optRest != '' && $opt{0} != '=' &&
  142. $i + 1 < count($longOptions) &&
  143. $opt == substr($longOptions[$i+1], 0, $optLen)) {
  144. throw new Xinc_Config_Exception_Getopt("option --$opt is ambiguous");
  145. }
  146. if (substr($longOpt, -1) == '=') {
  147. if (substr($longOpt, -2) != '==') {
  148. if (!strlen($optArg) && !(list(, $optArg) = each($args))) {
  149. throw new Xinc_Config_Exception_Getopt("option --$opt requires an argument");
  150. }
  151. }
  152. } else if ($optArg) {
  153. throw new Xinc_Config_Exception_Getopt("option --$opt doesn't allow an argument");
  154. }
  155. $opts[] = array('--' . $opt, $optArg);
  156. return;
  157. }
  158. throw new Xinc_Config_Exception_Getopt("unrecognized option --$opt");
  159. }
  160. }