Closed tacman closed 8 months ago
Good idea! I think the best to way would be to add a string|callable $suggestions
arg to the Argument/Option attributes. The callable couldn't be a closure but could be a method in the class.
Thinking about the syntax for defining suggestions:
public function __invoke(
// standard array
#[Argument(suggestions: ['foo', 'bar', 'baz'])]
string $username,
// standard callable
#[Argument(suggestions: [self::class, 'someStaticMethod'])]
string $username,
// doesn't work:
#[Argument(suggestions: [$this, 'someInstanceMethod'])]
string $username,
// solution to above - might need some logic to bind $this to the callable
#[Argument(suggestions: [self::class, 'someInstanceMethod'])]
string $username,
) {
// ...
}
WDYT?
I like those ideas. Much cleaner than injecting it into the configure() method. Options 1 and 2 I imagine are fairly straightforward to integrate, yes?
I think so, even 4 would probably just need \Closure::fromCallable($suggestions)->bindTo($this)
.
~Also, I think if the user passes a \Closure
, we should auto-bind to $this
~ Nevermind, you can't pass a closure to an attribute 😕
This will require some fancy footwork to be compatible with 5.4/6.0. The ability to add suggestions to InputOption
and InputArgument
was added in 6.1.
Is there a way to add suggested values (as an array or closure) to invokable commands? From the Symfony console documentation at https://symfony.com/doc/current/console/input.html#adding-argument-option-value-completion
Is there a way to enhance the configuration? Maybe something like
Obviously, I'm trying to see if it's possible to switch completely to invokable commands.