ArgvInput.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 overriden 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 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 ($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. protected function parseShortOptionSet($name)
  112. {
  113. $len = strlen($name);
  114. for ($i = 0; $i < $len; $i++)
  115. {
  116. if (!$this->definition->hasShortcut($name[$i]))
  117. {
  118. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  119. }
  120. $option = $this->definition->getOptionForShortcut($name[$i]);
  121. if ($option->acceptParameter())
  122. {
  123. $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  124. break;
  125. }
  126. else
  127. {
  128. $this->addLongOption($option->getName(), true);
  129. }
  130. }
  131. }
  132. /**
  133. * Parses a long option.
  134. *
  135. * @param string $token The current token
  136. */
  137. protected function parseLongOption($token)
  138. {
  139. $name = substr($token, 2);
  140. if (false !== $pos = strpos($name, '='))
  141. {
  142. $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  143. }
  144. else
  145. {
  146. $this->addLongOption($name, null);
  147. }
  148. }
  149. /**
  150. * Parses an argument.
  151. *
  152. * @param string $token The current token
  153. */
  154. protected function parseArgument($token)
  155. {
  156. if (!$this->definition->hasArgument(count($this->arguments)))
  157. {
  158. throw new \RuntimeException('Too many arguments.');
  159. }
  160. $this->arguments[$this->definition->getArgument(count($this->arguments))->getName()] = $token;
  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. protected function addShortOption($shortcut, $value)
  169. {
  170. if (!$this->definition->hasShortcut($shortcut))
  171. {
  172. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  173. }
  174. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  175. }
  176. /**
  177. * Adds a long option value.
  178. *
  179. * @param string $name The long option key
  180. * @param mixed $value The value for the option
  181. */
  182. protected function addLongOption($name, $value)
  183. {
  184. if (!$this->definition->hasOption($name))
  185. {
  186. throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
  187. }
  188. $option = $this->definition->getOption($name);
  189. if (null === $value && $option->acceptParameter())
  190. {
  191. // if option accepts an optional or mandatory argument
  192. // let's see if there is one provided
  193. $next = array_shift($this->parsed);
  194. if ('-' !== $next[0])
  195. {
  196. $value = $next;
  197. }
  198. else
  199. {
  200. array_unshift($this->parsed, $next);
  201. }
  202. }
  203. if (null === $value)
  204. {
  205. if ($option->isParameterRequired())
  206. {
  207. throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  208. }
  209. $value = $option->isParameterOptional() ? $option->getDefault() : true;
  210. }
  211. $this->options[$name] = $value;
  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. {
  222. if ($token && '-' === $token[0])
  223. {
  224. continue;
  225. }
  226. return $token;
  227. }
  228. }
  229. /**
  230. * Returns true if the raw parameters (not parsed) contains a value.
  231. *
  232. * This method is to be used to introspect the input parameters
  233. * before it has been validated. It must be used carefully.
  234. *
  235. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  236. *
  237. * @return Boolean true if the value is contained in the raw parameters
  238. */
  239. public function hasParameterOption($values)
  240. {
  241. if (!is_array($values))
  242. {
  243. $values = array($values);
  244. }
  245. foreach ($this->tokens as $v)
  246. {
  247. if (in_array($v, $values))
  248. {
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. }