InputDefinition.php 11 KB

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