-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Invokable command adjustments #59493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Still analyzing the last two #59340 (comment) #59340 (comment) |
6094cd1
to
730d374
Compare
Update after latest changes:
I added more tests to cover those scenarios. |
I've a question about non-boolean options: when defined without a default, e.g. |
This makes sense to me. Most important thing is a clear error message. |
I was expecting this topic to arise, not trivial :) I wouldn't be happy with invokable commands not supporting |
730d374
to
d740cfa
Compare
Let's lay out all the scenarios on the table so everyone can see the alternatives and how to implement them effectively with this new invokable approach. We can use input options in three different ways: If a default value is defined, it will be used as the option's value only in case (C). In case (B), the option value will be That's how regular commands work currently. Input options are optional by nature. This means the parameter must either:
However, input options can never be mandatory. This means defining them like The specific mode |
I've updated the code based on this analysis (it's pending throwing an exception for mandatory option parameters) |
I think the only mode I don't support here: https://github.com/zenstruck/console-extra?tab=readme-ov-file#invokable-attributes is |
Requiring |
d740cfa
to
4e96f0a
Compare
would making it nullable be sufficient? That's the approach used here, so you can declare an option with an optional value as But to be honest, I don’t know for which use case it makes sense, because the intention of passing such an option without a value feels more like a boolean option instead. |
To distinguish between an option passed without a value and not passed at all, set a default value: |
@@ -164,9 +168,6 @@ public function isEnabled(): bool | |||
*/ | |||
protected function configure() | |||
{ | |||
if (!$this->code && \is_callable($this)) { | |||
$this->code = new InvokableCommand($this, $this(...)); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to the constructor to allow overriding the configure()
method without requiring a call to the parent implementation
} | ||
} else { | ||
$code = $code(...); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to InvokableCommand
which supports handling static anonymous functions too
The case of I agree about adding the negatable flag only for boolean options that don't have |
That makes sense to me @stof. PR updated! Not entirely sure about the flag though, let’s see what others think |
@@ -99,6 +99,6 @@ public function toInputArgument(): InputArgument | |||
*/ | |||
public function resolveValue(InputInterface $input): mixed | |||
{ | |||
return $input->hasArgument($this->name) ? $input->getArgument($this->name) : null; | |||
return $input->getArgument($this->name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed $input->hasArgument($this->name)
as it's better to get the "argument does not exist" exception than a TypeError or just null (if nullable)
After a deep analysis with Kevin, we decided to simplify the argument/option definition by removing the |
Yea it requires checking both
It's true there are certainly plenty of misuses, yet there are lots of usages and some are valid. Looking only at public github repos: https://github.com/search?q=InputOption%3A%3AVALUE_OPTIONAL&type=code. |
@chalasr after this pr is merged I'm going to open one dedicated to |
476b881
to
e3c2c0d
Compare
e3c2c0d
to
8ab2f32
Compare
Thank you @yceruto. |
Adjustments based on the latest reviews from #59340