ArgvInput.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Input;
  11. /**
  12. * ArgvInput represents an input coming from the CLI arguments.
  13. *
  14. * Usage:
  15. *
  16. * $input = new ArgvInput();
  17. *
  18. * By default, the `$_SERVER['argv']` array is used for the input values.
  19. *
  20. * This can be overridden by explicitly passing the input values in the constructor:
  21. *
  22. * $input = new ArgvInput($_SERVER['argv']);
  23. *
  24. * If you pass it yourself, don't forget that the first element of the array
  25. * is the name of the running application.
  26. *
  27. * When passing an argument to the constructor, be sure that it respects
  28. * the same rules as the argv one. It's almost always better to use the
  29. * `StringInput` when you want to provide your own input.
  30. *
  31. * @author Fabien Potencier <fabien@symfony.com>
  32. *
  33. * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
  34. * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
  35. *
  36. * @api
  37. */
  38. class ArgvInput extends Input
  39. {
  40. private $tokens;
  41. private $parsed;
  42. /**
  43. * Constructor.
  44. *
  45. * @param array $argv An array of parameters from the CLI (in the argv format)
  46. * @param InputDefinition $definition A InputDefinition instance
  47. *
  48. * @api
  49. */
  50. public function __construct(array $argv = null, InputDefinition $definition = null)
  51. {
  52. if (null === $argv) {
  53. $argv = $_SERVER['argv'];
  54. }
  55. // strip the application name
  56. array_shift($argv);
  57. $this->tokens = $argv;
  58. parent::__construct($definition);
  59. }
  60. protected function setTokens(array $tokens)
  61. {
  62. $this->tokens = $tokens;
  63. }
  64. /**
  65. * Processes command line arguments.
  66. */
  67. protected function parse()
  68. {
  69. $parseOptions = true;
  70. $this->parsed = $this->tokens;
  71. while (null !== $token = array_shift($this->parsed)) {
  72. if ($parseOptions && '--' == $token) {
  73. $parseOptions = false;
  74. } elseif ($parseOptions && 0 === strpos($token, '--')) {
  75. $this->parseLongOption($token);
  76. } elseif ($parseOptions && '-' === $token[0]) {
  77. $this->parseShortOption($token);
  78. } else {
  79. $this->parseArgument($token);
  80. }
  81. }
  82. }
  83. /**
  84. * Parses a short option.
  85. *
  86. * @param string $token The current token.
  87. */
  88. private function parseShortOption($token)
  89. {
  90. $name = substr($token, 1);
  91. if (strlen($name) > 1) {
  92. if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
  93. // an option with a value (with no space)
  94. $this->addShortOption($name[0], substr($name, 1));
  95. } else {
  96. $this->parseShortOptionSet($name);
  97. }
  98. } else {
  99. $this->addShortOption($name, null);
  100. }
  101. }
  102. /**
  103. * Parses a short option set.
  104. *
  105. * @param string $name The current token
  106. *
  107. * @throws \RuntimeException When option given doesn't exist
  108. */
  109. private function parseShortOptionSet($name)
  110. {
  111. $len = strlen($name);
  112. for ($i = 0; $i < $len; $i++) {
  113. if (!$this->definition->hasShortcut($name[$i])) {
  114. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  115. }
  116. $option = $this->definition->getOptionForShortcut($name[$i]);
  117. if ($option->acceptValue()) {
  118. $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  119. break;
  120. } else {
  121. $this->addLongOption($option->getName(), true);
  122. }
  123. }
  124. }
  125. /**
  126. * Parses a long option.
  127. *
  128. * @param string $token The current token
  129. */
  130. private function parseLongOption($token)
  131. {
  132. $name = substr($token, 2);
  133. if (false !== $pos = strpos($name, '=')) {
  134. $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  135. } else {
  136. $this->addLongOption($name, null);
  137. }
  138. }
  139. /**
  140. * Parses an argument.
  141. *
  142. * @param string $token The current token
  143. *
  144. * @throws \RuntimeException When too many arguments are given
  145. */
  146. private function parseArgument($token)
  147. {
  148. $c = count($this->arguments);
  149. // if input is expecting another argument, add it
  150. if ($this->definition->hasArgument($c)) {
  151. $arg = $this->definition->getArgument($c);
  152. $this->arguments[$arg->getName()] = $arg->isArray()? array($token) : $token;
  153. // if last argument isArray(), append token to last argument
  154. } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
  155. $arg = $this->definition->getArgument($c - 1);
  156. $this->arguments[$arg->getName()][] = $token;
  157. // unexpected argument
  158. } else {
  159. throw new \RuntimeException('Too many arguments.');
  160. }
  161. }
  162. /**
  163. * Adds a short option value.
  164. *
  165. * @param string $shortcut The short option key
  166. * @param mixed $value The value for the option
  167. *
  168. * @throws \RuntimeException When option given doesn't exist
  169. */
  170. private function addShortOption($shortcut, $value)
  171. {
  172. if (!$this->definition->hasShortcut($shortcut)) {
  173. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  174. }
  175. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  176. }
  177. /**
  178. * Adds a long option value.
  179. *
  180. * @param string $name The long option key
  181. * @param mixed $value The value for the option
  182. *
  183. * @throws \RuntimeException When option given doesn't exist
  184. */
  185. private function addLongOption($name, $value)
  186. {
  187. if (!$this->definition->hasOption($name)) {
  188. throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
  189. }
  190. $option = $this->definition->getOption($name);
  191. if (null === $value && $option->acceptValue()) {
  192. // if option accepts an optional or mandatory argument
  193. // let's see if there is one provided
  194. $next = array_shift($this->parsed);
  195. if ('-' !== $next[0]) {
  196. $value = $next;
  197. } else {
  198. array_unshift($this->parsed, $next);
  199. }
  200. }
  201. if (null === $value) {
  202. if ($option->isValueRequired()) {
  203. throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  204. }
  205. $value = $option->isValueOptional() ? $option->getDefault() : true;
  206. }
  207. if ($option->isArray()) {
  208. $this->options[$name][] = $value;
  209. } else {
  210. $this->options[$name] = $value;
  211. }
  212. }
  213. /**
  214. * Returns the first argument from the raw parameters (not parsed).
  215. *
  216. * @return string The value of the first argument or null otherwise
  217. */
  218. public function getFirstArgument()
  219. {
  220. foreach ($this->tokens as $token) {
  221. if ($token && '-' === $token[0]) {
  222. continue;
  223. }
  224. return $token;
  225. }
  226. }
  227. /**
  228. * Returns true if the raw parameters (not parsed) contain a value.
  229. *
  230. * This method is to be used to introspect the input parameters
  231. * before they have been validated. It must be used carefully.
  232. *
  233. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  234. *
  235. * @return Boolean true if the value is contained in the raw parameters
  236. */
  237. public function hasParameterOption($values)
  238. {
  239. $values = (array) $values;
  240. foreach ($this->tokens as $v) {
  241. if (in_array($v, $values)) {
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. /**
  248. * Returns the value of a raw option (not parsed).
  249. *
  250. * This method is to be used to introspect the input parameters
  251. * before they have been validated. It must be used carefully.
  252. *
  253. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  254. * @param mixed $default The default value to return if no result is found
  255. *
  256. * @return mixed The option value
  257. */
  258. public function getParameterOption($values, $default = false)
  259. {
  260. $values = (array) $values;
  261. $tokens = $this->tokens;
  262. while ($token = array_shift($tokens)) {
  263. foreach ($values as $value) {
  264. if (0 === strpos($token, $value)) {
  265. if (false !== $pos = strpos($token, '=')) {
  266. return substr($token, $pos + 1);
  267. }
  268. return array_shift($tokens);
  269. }
  270. }
  271. }
  272. return $default;
  273. }
  274. }