InputDefinition.php 11 KB

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