ArgvInput.php 9.1KB

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. $this->parsed = $this->tokens;
  70. while (null !== $token = array_shift($this->parsed)) {
  71. if (0 === strpos($token, '--')) {
  72. $this->parseLongOption($token);
  73. } elseif ('-' === $token[0]) {
  74. $this->parseShortOption($token);
  75. } else {
  76. $this->parseArgument($token);
  77. }
  78. }
  79. }
  80. /**
  81. * Parses a short option.
  82. *
  83. * @param string $token The current token.
  84. */
  85. private function parseShortOption($token)
  86. {
  87. $name = substr($token, 1);
  88. if (strlen($name) > 1) {
  89. if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
  90. // an option with a value (with no space)
  91. $this->addShortOption($name[0], substr($name, 1));
  92. } else {
  93. $this->parseShortOptionSet($name);
  94. }
  95. } else {
  96. $this->addShortOption($name, null);
  97. }
  98. }
  99. /**
  100. * Parses a short option set.
  101. *
  102. * @param string $name The current token
  103. *
  104. * @throws \RuntimeException When option given doesn't exist
  105. */
  106. private function parseShortOptionSet($name)
  107. {
  108. $len = strlen($name);
  109. for ($i = 0; $i < $len; $i++) {
  110. if (!$this->definition->hasShortcut($name[$i])) {
  111. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  112. }
  113. $option = $this->definition->getOptionForShortcut($name[$i]);
  114. if ($option->acceptValue()) {
  115. $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  116. break;
  117. } else {
  118. $this->addLongOption($option->getName(), true);
  119. }
  120. }
  121. }
  122. /**
  123. * Parses a long option.
  124. *
  125. * @param string $token The current token
  126. */
  127. private function parseLongOption($token)
  128. {
  129. $name = substr($token, 2);
  130. if (false !== $pos = strpos($name, '=')) {
  131. $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  132. } else {
  133. $this->addLongOption($name, null);
  134. }
  135. }
  136. /**
  137. * Parses an argument.
  138. *
  139. * @param string $token The current token
  140. *
  141. * @throws \RuntimeException When too many arguments are given
  142. */
  143. private function parseArgument($token)
  144. {
  145. $c = count($this->arguments);
  146. // if input is expecting another argument, add it
  147. if ($this->definition->hasArgument($c)) {
  148. $arg = $this->definition->getArgument($c);
  149. $this->arguments[$arg->getName()] = $arg->isArray()? array($token) : $token;
  150. // if last argument isArray(), append token to last argument
  151. } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
  152. $arg = $this->definition->getArgument($c - 1);
  153. $this->arguments[$arg->getName()][] = $token;
  154. // unexpected argument
  155. } else {
  156. throw new \RuntimeException('Too many arguments.');
  157. }
  158. }
  159. /**
  160. * Adds a short option value.
  161. *
  162. * @param string $shortcut The short option key
  163. * @param mixed $value The value for the option
  164. *
  165. * @throws \RuntimeException When option given doesn't exist
  166. */
  167. private function addShortOption($shortcut, $value)
  168. {
  169. if (!$this->definition->hasShortcut($shortcut)) {
  170. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  171. }
  172. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  173. }
  174. /**
  175. * Adds a long option value.
  176. *
  177. * @param string $name The long option key
  178. * @param mixed $value The value for the option
  179. *
  180. * @throws \RuntimeException When option given doesn't exist
  181. */
  182. private function addLongOption($name, $value)
  183. {
  184. if (!$this->definition->hasOption($name)) {
  185. throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
  186. }
  187. $option = $this->definition->getOption($name);
  188. if (null === $value && $option->acceptValue() && count($this->parsed)) {
  189. // if option accepts an optional or mandatory argument
  190. // let's see if there is one provided
  191. $next = array_shift($this->parsed);
  192. if (isset($next[0]) && '-' !== $next[0]) {
  193. $value = $next;
  194. } elseif (empty($next)) {
  195. $value = '';
  196. } else {
  197. array_unshift($this->parsed, $next);
  198. }
  199. }
  200. if (null === $value) {
  201. if ($option->isValueRequired()) {
  202. throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  203. }
  204. $value = $option->isValueOptional() ? $option->getDefault() : true;
  205. }
  206. if ($option->isArray()) {
  207. $this->options[$name][] = $value;
  208. } else {
  209. $this->options[$name] = $value;
  210. }
  211. }
  212. /**
  213. * Returns the first argument from the raw parameters (not parsed).
  214. *
  215. * @return string The value of the first argument or null otherwise
  216. */
  217. public function getFirstArgument()
  218. {
  219. foreach ($this->tokens as $token) {
  220. if ($token && '-' === $token[0]) {
  221. continue;
  222. }
  223. return $token;
  224. }
  225. }
  226. /**
  227. * Returns true if the raw parameters (not parsed) contain a value.
  228. *
  229. * This method is to be used to introspect the input parameters
  230. * before they have been validated. It must be used carefully.
  231. *
  232. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  233. *
  234. * @return Boolean true if the value is contained in the raw parameters
  235. */
  236. public function hasParameterOption($values)
  237. {
  238. $values = (array) $values;
  239. foreach ($this->tokens as $v) {
  240. if (in_array($v, $values)) {
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. /**
  247. * Returns the value of a raw option (not parsed).
  248. *
  249. * This method is to be used to introspect the input parameters
  250. * before they have been validated. It must be used carefully.
  251. *
  252. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  253. * @param mixed $default The default value to return if no result is found
  254. *
  255. * @return mixed The option value
  256. */
  257. public function getParameterOption($values, $default = false)
  258. {
  259. $values = (array) $values;
  260. $tokens = $this->tokens;
  261. while ($token = array_shift($tokens)) {
  262. foreach ($values as $value) {
  263. if (0 === strpos($token, $value)) {
  264. if (false !== $pos = strpos($token, '=')) {
  265. return substr($token, $pos + 1);
  266. }
  267. return array_shift($tokens);
  268. }
  269. }
  270. }
  271. return $default;
  272. }
  273. }