InputDefinition.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. /**
  14. * A InputDefinition represents a set of valid command line arguments and options.
  15. *
  16. * Usage:
  17. *
  18. * $definition = new InputDefinition([
  19. * new InputArgument('name', InputArgument::REQUIRED),
  20. * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
  21. * ]);
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class InputDefinition
  26. {
  27. private $arguments;
  28. private $requiredCount;
  29. private $hasAnArrayArgument = false;
  30. private $hasOptional;
  31. private $options;
  32. private $shortcuts;
  33. /**
  34. * @param array $definition An array of InputArgument and InputOption instance
  35. */
  36. public function __construct(array $definition = [])
  37. {
  38. $this->setDefinition($definition);
  39. }
  40. /**
  41. * Sets the definition of the input.
  42. */
  43. public function setDefinition(array $definition)
  44. {
  45. $arguments = [];
  46. $options = [];
  47. foreach ($definition as $item) {
  48. if ($item instanceof InputOption) {
  49. $options[] = $item;
  50. } else {
  51. $arguments[] = $item;
  52. }
  53. }
  54. $this->setArguments($arguments);
  55. $this->setOptions($options);
  56. }
  57. /**
  58. * Sets the InputArgument objects.
  59. *
  60. * @param InputArgument[] $arguments An array of InputArgument objects
  61. */
  62. public function setArguments(array $arguments = [])
  63. {
  64. $this->arguments = [];
  65. $this->requiredCount = 0;
  66. $this->hasOptional = false;
  67. $this->hasAnArrayArgument = false;
  68. $this->addArguments($arguments);
  69. }
  70. /**
  71. * Adds an array of InputArgument objects.
  72. *
  73. * @param InputArgument[] $arguments An array of InputArgument objects
  74. */
  75. public function addArguments(?array $arguments = [])
  76. {
  77. if (null !== $arguments) {
  78. foreach ($arguments as $argument) {
  79. $this->addArgument($argument);
  80. }
  81. }
  82. }
  83. /**
  84. * @throws LogicException When incorrect argument is given
  85. */
  86. public function addArgument(InputArgument $argument)
  87. {
  88. if (isset($this->arguments[$argument->getName()])) {
  89. throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
  90. }
  91. if ($this->hasAnArrayArgument) {
  92. throw new LogicException('Cannot add an argument after an array argument.');
  93. }
  94. if ($argument->isRequired() && $this->hasOptional) {
  95. throw new LogicException('Cannot add a required argument after an optional one.');
  96. }
  97. if ($argument->isArray()) {
  98. $this->hasAnArrayArgument = true;
  99. }
  100. if ($argument->isRequired()) {
  101. ++$this->requiredCount;
  102. } else {
  103. $this->hasOptional = true;
  104. }
  105. $this->arguments[$argument->getName()] = $argument;
  106. }
  107. /**
  108. * Returns an InputArgument by name or by position.
  109. *
  110. * @param string|int $name The InputArgument name or position
  111. *
  112. * @return InputArgument An InputArgument object
  113. *
  114. * @throws InvalidArgumentException When argument given doesn't exist
  115. */
  116. public function getArgument($name)
  117. {
  118. if (!$this->hasArgument($name)) {
  119. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  120. }
  121. $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
  122. return $arguments[$name];
  123. }
  124. /**
  125. * Returns true if an InputArgument object exists by name or position.
  126. *
  127. * @param string|int $name The InputArgument name or position
  128. *
  129. * @return bool true if the InputArgument object exists, false otherwise
  130. */
  131. public function hasArgument($name)
  132. {
  133. $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
  134. return isset($arguments[$name]);
  135. }
  136. /**
  137. * Gets the array of InputArgument objects.
  138. *
  139. * @return InputArgument[] An array of InputArgument objects
  140. */
  141. public function getArguments()
  142. {
  143. return $this->arguments;
  144. }
  145. /**
  146. * Returns the number of InputArguments.
  147. *
  148. * @return int The number of InputArguments
  149. */
  150. public function getArgumentCount()
  151. {
  152. return $this->hasAnArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
  153. }
  154. /**
  155. * Returns the number of required InputArguments.
  156. *
  157. * @return int The number of required InputArguments
  158. */
  159. public function getArgumentRequiredCount()
  160. {
  161. return $this->requiredCount;
  162. }
  163. /**
  164. * Gets the default values.
  165. *
  166. * @return array An array of default values
  167. */
  168. public function getArgumentDefaults()
  169. {
  170. $values = [];
  171. foreach ($this->arguments as $argument) {
  172. $values[$argument->getName()] = $argument->getDefault();
  173. }
  174. return $values;
  175. }
  176. /**
  177. * Sets the InputOption objects.
  178. *
  179. * @param InputOption[] $options An array of InputOption objects
  180. */
  181. public function setOptions(array $options = [])
  182. {
  183. $this->options = [];
  184. $this->shortcuts = [];
  185. $this->addOptions($options);
  186. }
  187. /**
  188. * Adds an array of InputOption objects.
  189. *
  190. * @param InputOption[] $options An array of InputOption objects
  191. */
  192. public function addOptions(array $options = [])
  193. {
  194. foreach ($options as $option) {
  195. $this->addOption($option);
  196. }
  197. }
  198. /**
  199. * @throws LogicException When option given already exist
  200. */
  201. public function addOption(InputOption $option)
  202. {
  203. if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
  204. throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
  205. }
  206. if ($option->getShortcut()) {
  207. foreach (explode('|', $option->getShortcut()) as $shortcut) {
  208. if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
  209. throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
  210. }
  211. }
  212. }
  213. $this->options[$option->getName()] = $option;
  214. if ($option->getShortcut()) {
  215. foreach (explode('|', $option->getShortcut()) as $shortcut) {
  216. $this->shortcuts[$shortcut] = $option->getName();
  217. }
  218. }
  219. }
  220. /**
  221. * Returns an InputOption by name.
  222. *
  223. * @return InputOption A InputOption object
  224. *
  225. * @throws InvalidArgumentException When option given doesn't exist
  226. */
  227. public function getOption(string $name)
  228. {
  229. if (!$this->hasOption($name)) {
  230. throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  231. }
  232. return $this->options[$name];
  233. }
  234. /**
  235. * Returns true if an InputOption object exists by name.
  236. *
  237. * This method can't be used to check if the user included the option when
  238. * executing the command (use getOption() instead).
  239. *
  240. * @return bool true if the InputOption object exists, false otherwise
  241. */
  242. public function hasOption(string $name)
  243. {
  244. return isset($this->options[$name]);
  245. }
  246. /**
  247. * Gets the array of InputOption objects.
  248. *
  249. * @return InputOption[] An array of InputOption objects
  250. */
  251. public function getOptions()
  252. {
  253. return $this->options;
  254. }
  255. /**
  256. * Returns true if an InputOption object exists by shortcut.
  257. *
  258. * @return bool true if the InputOption object exists, false otherwise
  259. */
  260. public function hasShortcut(string $name)
  261. {
  262. return isset($this->shortcuts[$name]);
  263. }
  264. /**
  265. * Gets an InputOption by shortcut.
  266. *
  267. * @return InputOption An InputOption object
  268. */
  269. public function getOptionForShortcut(string $shortcut)
  270. {
  271. return $this->getOption($this->shortcutToName($shortcut));
  272. }
  273. /**
  274. * Gets an array of default values.
  275. *
  276. * @return array An array of all default values
  277. */
  278. public function getOptionDefaults()
  279. {
  280. $values = [];
  281. foreach ($this->options as $option) {
  282. $values[$option->getName()] = $option->getDefault();
  283. }
  284. return $values;
  285. }
  286. /**
  287. * Returns the InputOption name given a shortcut.
  288. *
  289. * @throws InvalidArgumentException When option given does not exist
  290. *
  291. * @internal
  292. */
  293. public function shortcutToName(string $shortcut): string
  294. {
  295. if (!isset($this->shortcuts[$shortcut])) {
  296. throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  297. }
  298. return $this->shortcuts[$shortcut];
  299. }
  300. /**
  301. * Gets the synopsis.
  302. *
  303. * @return string The synopsis
  304. */
  305. public function getSynopsis(bool $short = false)
  306. {
  307. $elements = [];
  308. if ($short && $this->getOptions()) {
  309. $elements[] = '[options]';
  310. } elseif (!$short) {
  311. foreach ($this->getOptions() as $option) {
  312. $value = '';
  313. if ($option->acceptValue()) {
  314. $value = sprintf(
  315. ' %s%s%s',
  316. $option->isValueOptional() ? '[' : '',
  317. strtoupper($option->getName()),
  318. $option->isValueOptional() ? ']' : ''
  319. );
  320. }
  321. $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
  322. $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
  323. }
  324. }
  325. if (\count($elements) && $this->getArguments()) {
  326. $elements[] = '[--]';
  327. }
  328. $tail = '';
  329. foreach ($this->getArguments() as $argument) {
  330. $element = '<'.$argument->getName().'>';
  331. if ($argument->isArray()) {
  332. $element .= '...';
  333. }
  334. if (!$argument->isRequired()) {
  335. $element = '['.$element;
  336. $tail .= ']';
  337. }
  338. $elements[] = $element;
  339. }
  340. return implode(' ', $elements).$tail;
  341. }
  342. }