InputDefinition.php 12 KB

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