InputDefinition.php 12 KB

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