InputDefinition.php 11 KB

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