ArgvInput.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace Symfony\Components\Console\Input;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  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 program.
  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. * @package Symfony
  32. * @subpackage Components_Console
  33. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  34. *
  35. * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
  36. * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
  37. */
  38. class ArgvInput extends Input
  39. {
  40. protected $tokens;
  41. protected $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. public function __construct(array $argv = null, InputDefinition $definition = null)
  49. {
  50. if (null === $argv)
  51. {
  52. $argv = $_SERVER['argv'];
  53. }
  54. // strip the program name
  55. array_shift($argv);
  56. $this->tokens = $argv;
  57. parent::__construct($definition);
  58. }
  59. /**
  60. * Processes command line arguments.
  61. */
  62. protected function parse()
  63. {
  64. $this->parsed = $this->tokens;
  65. while (null !== $token = array_shift($this->parsed))
  66. {
  67. if ('--' === substr($token, 0, 2))
  68. {
  69. $this->parseLongOption($token);
  70. }
  71. elseif ('-' === $token[0])
  72. {
  73. $this->parseShortOption($token);
  74. }
  75. else
  76. {
  77. $this->parseArgument($token);
  78. }
  79. }
  80. }
  81. /**
  82. * Parses a short option.
  83. *
  84. * @param string $token The current token.
  85. */
  86. protected function parseShortOption($token)
  87. {
  88. $name = substr($token, 1);
  89. if (strlen($name) > 1)
  90. {
  91. if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptParameter())
  92. {
  93. // an option with a value (with no space)
  94. $this->addShortOption($name[0], substr($name, 1));
  95. }
  96. else
  97. {
  98. $this->parseShortOptionSet($name);
  99. }
  100. }
  101. else
  102. {
  103. $this->addShortOption($name, null);
  104. }
  105. }
  106. /**
  107. * Parses a short option set.
  108. *
  109. * @param string $token The current token
  110. *
  111. * @throws \RuntimeException When option given doesn't exist
  112. */
  113. protected function parseShortOptionSet($name)
  114. {
  115. $len = strlen($name);
  116. for ($i = 0; $i < $len; $i++)
  117. {
  118. if (!$this->definition->hasShortcut($name[$i]))
  119. {
  120. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  121. }
  122. $option = $this->definition->getOptionForShortcut($name[$i]);
  123. if ($option->acceptParameter())
  124. {
  125. $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  126. break;
  127. }
  128. else
  129. {
  130. $this->addLongOption($option->getName(), true);
  131. }
  132. }
  133. }
  134. /**
  135. * Parses a long option.
  136. *
  137. * @param string $token The current token
  138. */
  139. protected function parseLongOption($token)
  140. {
  141. $name = substr($token, 2);
  142. if (false !== $pos = strpos($name, '='))
  143. {
  144. $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  145. }
  146. else
  147. {
  148. $this->addLongOption($name, null);
  149. }
  150. }
  151. /**
  152. * Parses an argument.
  153. *
  154. * @param string $token The current token
  155. *
  156. * @throws \RuntimeException When too many arguments are given
  157. */
  158. protected function parseArgument($token)
  159. {
  160. if (!$this->definition->hasArgument(count($this->arguments)))
  161. {
  162. throw new \RuntimeException('Too many arguments.');
  163. }
  164. $this->arguments[$this->definition->getArgument(count($this->arguments))->getName()] = $token;
  165. }
  166. /**
  167. * Adds a short option value.
  168. *
  169. * @param string $shortcut The short option key
  170. * @param mixed $value The value for the option
  171. *
  172. * @throws \RuntimeException When option given doesn't exist
  173. */
  174. protected function addShortOption($shortcut, $value)
  175. {
  176. if (!$this->definition->hasShortcut($shortcut))
  177. {
  178. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  179. }
  180. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  181. }
  182. /**
  183. * Adds a long option value.
  184. *
  185. * @param string $name The long option key
  186. * @param mixed $value The value for the option
  187. *
  188. * @throws \RuntimeException When option given doesn't exist
  189. */
  190. protected function addLongOption($name, $value)
  191. {
  192. if (!$this->definition->hasOption($name))
  193. {
  194. throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
  195. }
  196. $option = $this->definition->getOption($name);
  197. if (null === $value && $option->acceptParameter())
  198. {
  199. // if option accepts an optional or mandatory argument
  200. // let's see if there is one provided
  201. $next = array_shift($this->parsed);
  202. if ('-' !== $next[0])
  203. {
  204. $value = $next;
  205. }
  206. else
  207. {
  208. array_unshift($this->parsed, $next);
  209. }
  210. }
  211. if (null === $value)
  212. {
  213. if ($option->isParameterRequired())
  214. {
  215. throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  216. }
  217. $value = $option->isParameterOptional() ? $option->getDefault() : true;
  218. }
  219. $this->options[$name] = $value;
  220. }
  221. /**
  222. * Returns the first argument from the raw parameters (not parsed).
  223. *
  224. * @return string The value of the first argument or null otherwise
  225. */
  226. public function getFirstArgument()
  227. {
  228. foreach ($this->tokens as $token)
  229. {
  230. if ($token && '-' === $token[0])
  231. {
  232. continue;
  233. }
  234. return $token;
  235. }
  236. }
  237. /**
  238. * Returns true if the raw parameters (not parsed) contains a value.
  239. *
  240. * This method is to be used to introspect the input parameters
  241. * before it has been validated. It must be used carefully.
  242. *
  243. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  244. *
  245. * @return Boolean true if the value is contained in the raw parameters
  246. */
  247. public function hasParameterOption($values)
  248. {
  249. if (!is_array($values))
  250. {
  251. $values = array($values);
  252. }
  253. foreach ($this->tokens as $v)
  254. {
  255. if (in_array($v, $values))
  256. {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. }