22
33namespace Circli \Console ;
44
5+ use Symfony \Component \Console \Completion \CompletionInput ;
6+ use Symfony \Component \Console \Completion \CompletionSuggestions ;
7+ use Symfony \Component \Console \Completion \Suggestion ;
58use Symfony \Component \Console \Exception \InvalidArgumentException ;
69use Symfony \Component \Console \Input \InputArgument ;
710use Symfony \Component \Console \Input \InputDefinition ;
@@ -20,6 +23,8 @@ abstract class Definition
2023 private array $ usages = [];
2124 /** @var string|callable|null */
2225 private $ command ;
26+ /** @var array<string|callable> */
27+ private array $ completions = [];
2328
2429 public function __construct ()
2530 {
@@ -32,6 +37,7 @@ public function __construct()
3237 *
3338 * @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
3439 * @param string|bool|int|float|array<mixed>|null $default The default value (for InputArgument::OPTIONAL mode only)
40+ * @param list<string>|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
3541 *
3642 * @return $this
3743 * @throws InvalidArgumentException When argument mode is not valid
@@ -41,8 +47,9 @@ public function addArgument(
4147 int $ mode = null ,
4248 string $ description = '' ,
4349 string |bool |int |float |array $ default = null ,
50+ \Closure |array $ suggestedValues = [],
4451 ): static {
45- $ this ->definition ->addArgument (new InputArgument ($ name , $ mode , $ description , $ default ));
52+ $ this ->definition ->addArgument (new InputArgument ($ name , $ mode , $ description , $ default, $ suggestedValues ));
4653
4754 return $ this ;
4855 }
@@ -53,6 +60,7 @@ public function addArgument(
5360 * @param string|list<string>|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
5461 * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
5562 * @param bool|int|string|string[]|null $default The default value (must be null for InputOption::VALUE_NONE)
63+ * @param list<string>|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
5664 *
5765 * @throws InvalidArgumentException If option mode is invalid or incompatible
5866 */
@@ -62,8 +70,9 @@ public function addOption(
6270 int $ mode = null ,
6371 string $ description = '' ,
6472 array |bool |int |string $ default = null ,
73+ \Closure |array $ suggestedValues = [],
6574 ): static {
66- $ this ->definition ->addOption (new InputOption ($ name , $ shortcut , $ mode , $ description , $ default ));
75+ $ this ->definition ->addOption (new InputOption ($ name , $ shortcut , $ mode , $ description , $ default, $ suggestedValues ));
6776
6877 return $ this ;
6978 }
@@ -164,6 +173,35 @@ public function setCommand(callable|object|string $command): static
164173 return $ this ;
165174 }
166175
176+ /**
177+ * @param callable(CompletionInput $input, CompletionSuggestions $suggestions, callable $default): void|string $completion
178+ * @return $this
179+ */
180+ public function setCompletion (callable |string $ completion ): static
181+ {
182+ $ this ->completions ['__ROOT ' ] = $ completion ;
183+
184+ return $ this ;
185+ }
186+
187+ /**
188+ * @return array<string, callable|string>
189+ */
190+ public function getCompletions (): array
191+ {
192+ return $ this ->completions ;
193+ }
194+
195+ /**
196+ * @param callable(CompletionInput $input, CompletionSuggestions $suggestions, callable $default): void|string $completion
197+ * @return $this
198+ */
199+ public function addCompletion (string $ argumentOrOption , callable |string $ completion ): static
200+ {
201+ $ this ->completions [$ argumentOrOption ] = $ completion ;
202+ return $ this ;
203+ }
204+
167205 public function getDefinition (): InputDefinition
168206 {
169207 return $ this ->definition ;
0 commit comments