InputArgument.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Command\Command;
  12. use Symfony\Component\Console\Completion\CompletionInput;
  13. use Symfony\Component\Console\Completion\CompletionSuggestions;
  14. use Symfony\Component\Console\Completion\Suggestion;
  15. use Symfony\Component\Console\Exception\InvalidArgumentException;
  16. use Symfony\Component\Console\Exception\LogicException;
  17. /**
  18. * Represents a command line argument.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class InputArgument
  23. {
  24. public const REQUIRED = 1;
  25. public const OPTIONAL = 2;
  26. public const IS_ARRAY = 4;
  27. private string $name;
  28. private int $mode;
  29. private string|int|bool|array|null|float $default;
  30. private array|\Closure $suggestedValues;
  31. private string $description;
  32. /**
  33. * @param string $name The argument name
  34. * @param int|null $mode The argument mode: a bit mask of self::REQUIRED, self::OPTIONAL and self::IS_ARRAY
  35. * @param string $description A description text
  36. * @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
  37. * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
  38. *
  39. * @throws InvalidArgumentException When argument mode is not valid
  40. */
  41. public function __construct(string $name, ?int $mode = null, string $description = '', string|bool|int|float|array|null $default = null, \Closure|array $suggestedValues = [])
  42. {
  43. if (null === $mode) {
  44. $mode = self::OPTIONAL;
  45. } elseif ($mode > 7 || $mode < 1) {
  46. throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
  47. }
  48. $this->name = $name;
  49. $this->mode = $mode;
  50. $this->description = $description;
  51. $this->suggestedValues = $suggestedValues;
  52. $this->setDefault($default);
  53. }
  54. /**
  55. * Returns the argument name.
  56. */
  57. public function getName(): string
  58. {
  59. return $this->name;
  60. }
  61. /**
  62. * Returns true if the argument is required.
  63. *
  64. * @return bool true if parameter mode is self::REQUIRED, false otherwise
  65. */
  66. public function isRequired(): bool
  67. {
  68. return self::REQUIRED === (self::REQUIRED & $this->mode);
  69. }
  70. /**
  71. * Returns true if the argument can take multiple values.
  72. *
  73. * @return bool true if mode is self::IS_ARRAY, false otherwise
  74. */
  75. public function isArray(): bool
  76. {
  77. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  78. }
  79. /**
  80. * Sets the default value.
  81. *
  82. * @return void
  83. *
  84. * @throws LogicException When incorrect default value is given
  85. */
  86. public function setDefault(string|bool|int|float|array|null $default = null)
  87. {
  88. if (1 > \func_num_args()) {
  89. trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  90. }
  91. if ($this->isRequired() && null !== $default) {
  92. throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
  93. }
  94. if ($this->isArray()) {
  95. if (null === $default) {
  96. $default = [];
  97. } elseif (!\is_array($default)) {
  98. throw new LogicException('A default value for an array argument must be an array.');
  99. }
  100. }
  101. $this->default = $default;
  102. }
  103. /**
  104. * Returns the default value.
  105. */
  106. public function getDefault(): string|bool|int|float|array|null
  107. {
  108. return $this->default;
  109. }
  110. public function hasCompletion(): bool
  111. {
  112. return [] !== $this->suggestedValues;
  113. }
  114. /**
  115. * Adds suggestions to $suggestions for the current completion input.
  116. *
  117. * @see Command::complete()
  118. */
  119. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  120. {
  121. $values = $this->suggestedValues;
  122. if ($values instanceof \Closure && !\is_array($values = $values($input))) {
  123. throw new LogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
  124. }
  125. if ($values) {
  126. $suggestions->suggestValues($values);
  127. }
  128. }
  129. /**
  130. * Returns the description text.
  131. */
  132. public function getDescription(): string
  133. {
  134. return $this->description;
  135. }
  136. }