InputDefinition.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 array $arguments = [];
  28. private int $requiredCount = 0;
  29. private ?InputArgument $lastArrayArgument = null;
  30. private ?InputArgument $lastOptionalArgument = null;
  31. private array $options = [];
  32. private array $negations = [];
  33. private array $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): void
  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 = []): void
  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 = []): void
  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): void
  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. * @throws InvalidArgumentException When argument given doesn't exist
  112. */
  113. public function getArgument(string|int $name): InputArgument
  114. {
  115. if (!$this->hasArgument($name)) {
  116. throw new InvalidArgumentException(\sprintf('The "%s" argument does not exist.', $name));
  117. }
  118. $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
  119. return $arguments[$name];
  120. }
  121. /**
  122. * Returns true if an InputArgument object exists by name or position.
  123. */
  124. public function hasArgument(string|int $name): bool
  125. {
  126. $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
  127. return isset($arguments[$name]);
  128. }
  129. /**
  130. * Gets the array of InputArgument objects.
  131. *
  132. * @return InputArgument[]
  133. */
  134. public function getArguments(): array
  135. {
  136. return $this->arguments;
  137. }
  138. /**
  139. * Returns the number of InputArguments.
  140. */
  141. public function getArgumentCount(): int
  142. {
  143. return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
  144. }
  145. /**
  146. * Returns the number of required InputArguments.
  147. */
  148. public function getArgumentRequiredCount(): int
  149. {
  150. return $this->requiredCount;
  151. }
  152. /**
  153. * @return array<string|bool|int|float|array|null>
  154. */
  155. public function getArgumentDefaults(): array
  156. {
  157. $values = [];
  158. foreach ($this->arguments as $argument) {
  159. $values[$argument->getName()] = $argument->getDefault();
  160. }
  161. return $values;
  162. }
  163. /**
  164. * Sets the InputOption objects.
  165. *
  166. * @param InputOption[] $options An array of InputOption objects
  167. */
  168. public function setOptions(array $options = []): void
  169. {
  170. $this->options = [];
  171. $this->shortcuts = [];
  172. $this->negations = [];
  173. $this->addOptions($options);
  174. }
  175. /**
  176. * Adds an array of InputOption objects.
  177. *
  178. * @param InputOption[] $options An array of InputOption objects
  179. */
  180. public function addOptions(array $options = []): void
  181. {
  182. foreach ($options as $option) {
  183. $this->addOption($option);
  184. }
  185. }
  186. /**
  187. * @throws LogicException When option given already exist
  188. */
  189. public function addOption(InputOption $option): void
  190. {
  191. if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
  192. throw new LogicException(\sprintf('An option named "%s" already exists.', $option->getName()));
  193. }
  194. if (isset($this->negations[$option->getName()])) {
  195. throw new LogicException(\sprintf('An option named "%s" already exists.', $option->getName()));
  196. }
  197. if ($option->getShortcut()) {
  198. foreach (explode('|', $option->getShortcut()) as $shortcut) {
  199. if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
  200. throw new LogicException(\sprintf('An option with shortcut "%s" already exists.', $shortcut));
  201. }
  202. }
  203. }
  204. $this->options[$option->getName()] = $option;
  205. if ($option->getShortcut()) {
  206. foreach (explode('|', $option->getShortcut()) as $shortcut) {
  207. $this->shortcuts[$shortcut] = $option->getName();
  208. }
  209. }
  210. if ($option->isNegatable()) {
  211. $negatedName = 'no-'.$option->getName();
  212. if (isset($this->options[$negatedName])) {
  213. throw new LogicException(\sprintf('An option named "%s" already exists.', $negatedName));
  214. }
  215. $this->negations[$negatedName] = $option->getName();
  216. }
  217. }
  218. /**
  219. * Returns an InputOption by name.
  220. *
  221. * @throws InvalidArgumentException When option given doesn't exist
  222. */
  223. public function getOption(string $name): InputOption
  224. {
  225. if (!$this->hasOption($name)) {
  226. throw new InvalidArgumentException(\sprintf('The "--%s" option does not exist.', $name));
  227. }
  228. return $this->options[$name];
  229. }
  230. /**
  231. * Returns true if an InputOption object exists by name.
  232. *
  233. * This method can't be used to check if the user included the option when
  234. * executing the command (use getOption() instead).
  235. */
  236. public function hasOption(string $name): bool
  237. {
  238. return isset($this->options[$name]);
  239. }
  240. /**
  241. * Gets the array of InputOption objects.
  242. *
  243. * @return InputOption[]
  244. */
  245. public function getOptions(): array
  246. {
  247. return $this->options;
  248. }
  249. /**
  250. * Returns true if an InputOption object exists by shortcut.
  251. */
  252. public function hasShortcut(string $name): bool
  253. {
  254. return isset($this->shortcuts[$name]);
  255. }
  256. /**
  257. * Returns true if an InputOption object exists by negated name.
  258. */
  259. public function hasNegation(string $name): bool
  260. {
  261. return isset($this->negations[$name]);
  262. }
  263. /**
  264. * Gets an InputOption by shortcut.
  265. */
  266. public function getOptionForShortcut(string $shortcut): InputOption
  267. {
  268. return $this->getOption($this->shortcutToName($shortcut));
  269. }
  270. /**
  271. * @return array<string|bool|int|float|array|null>
  272. */
  273. public function getOptionDefaults(): array
  274. {
  275. $values = [];
  276. foreach ($this->options as $option) {
  277. $values[$option->getName()] = $option->getDefault();
  278. }
  279. return $values;
  280. }
  281. /**
  282. * Returns the InputOption name given a shortcut.
  283. *
  284. * @throws InvalidArgumentException When option given does not exist
  285. *
  286. * @internal
  287. */
  288. public function shortcutToName(string $shortcut): string
  289. {
  290. if (!isset($this->shortcuts[$shortcut])) {
  291. throw new InvalidArgumentException(\sprintf('The "-%s" option does not exist.', $shortcut));
  292. }
  293. return $this->shortcuts[$shortcut];
  294. }
  295. /**
  296. * Returns the InputOption name given a negation.
  297. *
  298. * @throws InvalidArgumentException When option given does not exist
  299. *
  300. * @internal
  301. */
  302. public function negationToName(string $negation): string
  303. {
  304. if (!isset($this->negations[$negation])) {
  305. throw new InvalidArgumentException(\sprintf('The "--%s" option does not exist.', $negation));
  306. }
  307. return $this->negations[$negation];
  308. }
  309. /**
  310. * Gets the synopsis.
  311. */
  312. public function getSynopsis(bool $short = false): string
  313. {
  314. $elements = [];
  315. if ($short && $this->getOptions()) {
  316. $elements[] = '[options]';
  317. } elseif (!$short) {
  318. foreach ($this->getOptions() as $option) {
  319. $value = '';
  320. if ($option->acceptValue()) {
  321. $value = \sprintf(
  322. ' %s%s%s',
  323. $option->isValueOptional() ? '[' : '',
  324. strtoupper($option->getName()),
  325. $option->isValueOptional() ? ']' : ''
  326. );
  327. }
  328. $shortcut = $option->getShortcut() ? \sprintf('-%s|', $option->getShortcut()) : '';
  329. $negation = $option->isNegatable() ? \sprintf('|--no-%s', $option->getName()) : '';
  330. $elements[] = \sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation);
  331. }
  332. }
  333. if (\count($elements) && $this->getArguments()) {
  334. $elements[] = '[--]';
  335. }
  336. $tail = '';
  337. foreach ($this->getArguments() as $argument) {
  338. $element = '<'.$argument->getName().'>';
  339. if ($argument->isArray()) {
  340. $element .= '...';
  341. }
  342. if (!$argument->isRequired()) {
  343. $element = '['.$element;
  344. $tail .= ']';
  345. }
  346. $elements[] = $element;
  347. }
  348. return implode(' ', $elements).$tail;
  349. }
  350. }