You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

428 lines
12 KiB

3 years ago
  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 An InputArgument object
  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 true if the InputArgument object exists, false otherwise
  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[] An array of InputArgument objects
  141. */
  142. public function getArguments()
  143. {
  144. return $this->arguments;
  145. }
  146. /**
  147. * Returns the number of InputArguments.
  148. *
  149. * @return int The number of InputArguments
  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 The number of required InputArguments
  159. */
  160. public function getArgumentRequiredCount()
  161. {
  162. return $this->requiredCount;
  163. }
  164. /**
  165. * Gets the default values.
  166. *
  167. * @return array An array of default values
  168. */
  169. public function getArgumentDefaults()
  170. {
  171. $values = [];
  172. foreach ($this->arguments as $argument) {
  173. $values[$argument->getName()] = $argument->getDefault();
  174. }
  175. return $values;
  176. }
  177. /**
  178. * Sets the InputOption objects.
  179. *
  180. * @param InputOption[] $options An array of InputOption objects
  181. */
  182. public function setOptions(array $options = [])
  183. {
  184. $this->options = [];
  185. $this->shortcuts = [];
  186. $this->negations = [];
  187. $this->addOptions($options);
  188. }
  189. /**
  190. * Adds an array of InputOption objects.
  191. *
  192. * @param InputOption[] $options An array of InputOption objects
  193. */
  194. public function addOptions(array $options = [])
  195. {
  196. foreach ($options as $option) {
  197. $this->addOption($option);
  198. }
  199. }
  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. * @return InputOption A InputOption object
  236. *
  237. * @throws InvalidArgumentException When option given doesn't exist
  238. */
  239. public function getOption(string $name)
  240. {
  241. if (!$this->hasOption($name)) {
  242. throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  243. }
  244. return $this->options[$name];
  245. }
  246. /**
  247. * Returns true if an InputOption object exists by name.
  248. *
  249. * This method can't be used to check if the user included the option when
  250. * executing the command (use getOption() instead).
  251. *
  252. * @return bool true if the InputOption object exists, false otherwise
  253. */
  254. public function hasOption(string $name)
  255. {
  256. return isset($this->options[$name]);
  257. }
  258. /**
  259. * Gets the array of InputOption objects.
  260. *
  261. * @return InputOption[] An array of InputOption objects
  262. */
  263. public function getOptions()
  264. {
  265. return $this->options;
  266. }
  267. /**
  268. * Returns true if an InputOption object exists by shortcut.
  269. *
  270. * @return bool true if the InputOption object exists, false otherwise
  271. */
  272. public function hasShortcut(string $name)
  273. {
  274. return isset($this->shortcuts[$name]);
  275. }
  276. /**
  277. * Returns true if an InputOption object exists by negated name.
  278. */
  279. public function hasNegation(string $name): bool
  280. {
  281. return isset($this->negations[$name]);
  282. }
  283. /**
  284. * Gets an InputOption by shortcut.
  285. *
  286. * @return InputOption An InputOption object
  287. */
  288. public function getOptionForShortcut(string $shortcut)
  289. {
  290. return $this->getOption($this->shortcutToName($shortcut));
  291. }
  292. /**
  293. * Gets an array of default values.
  294. *
  295. * @return array An array of all default values
  296. */
  297. public function getOptionDefaults()
  298. {
  299. $values = [];
  300. foreach ($this->options as $option) {
  301. $values[$option->getName()] = $option->getDefault();
  302. }
  303. return $values;
  304. }
  305. /**
  306. * Returns the InputOption name given a shortcut.
  307. *
  308. * @throws InvalidArgumentException When option given does not exist
  309. *
  310. * @internal
  311. */
  312. public function shortcutToName(string $shortcut): string
  313. {
  314. if (!isset($this->shortcuts[$shortcut])) {
  315. throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  316. }
  317. return $this->shortcuts[$shortcut];
  318. }
  319. /**
  320. * Returns the InputOption name given a negation.
  321. *
  322. * @throws InvalidArgumentException When option given does not exist
  323. *
  324. * @internal
  325. */
  326. public function negationToName(string $negation): string
  327. {
  328. if (!isset($this->negations[$negation])) {
  329. throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $negation));
  330. }
  331. return $this->negations[$negation];
  332. }
  333. /**
  334. * Gets the synopsis.
  335. *
  336. * @return string The synopsis
  337. */
  338. public function getSynopsis(bool $short = false)
  339. {
  340. $elements = [];
  341. if ($short && $this->getOptions()) {
  342. $elements[] = '[options]';
  343. } elseif (!$short) {
  344. foreach ($this->getOptions() as $option) {
  345. $value = '';
  346. if ($option->acceptValue()) {
  347. $value = sprintf(
  348. ' %s%s%s',
  349. $option->isValueOptional() ? '[' : '',
  350. strtoupper($option->getName()),
  351. $option->isValueOptional() ? ']' : ''
  352. );
  353. }
  354. $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
  355. $negation = $option->isNegatable() ? sprintf('|--no-%s', $option->getName()) : '';
  356. $elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation);
  357. }
  358. }
  359. if (\count($elements) && $this->getArguments()) {
  360. $elements[] = '[--]';
  361. }
  362. $tail = '';
  363. foreach ($this->getArguments() as $argument) {
  364. $element = '<'.$argument->getName().'>';
  365. if ($argument->isArray()) {
  366. $element .= '...';
  367. }
  368. if (!$argument->isRequired()) {
  369. $element = '['.$element;
  370. $tail .= ']';
  371. }
  372. $elements[] = $element;
  373. }
  374. return implode(' ', $elements).$tail;
  375. }
  376. }