InputDefinition.php 12 KB

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