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
[Console] Ability to use option default only if the option was passed #12769
Comments
@OndraM And if the option is ran, you will get So you should able to do |
@blanchonvincent Thanks for reply. However, this doesn't solve the case. I want to have a) default value if the option is used, but also b) ability to overwrite the default value. If I use |
@blanchonvincent Looks great, thank you! So the usage for my use case should be like this: // configure()
$this->addOption('myoption', null, InputOption::VALUE_OPTIONAL, 'Desc', 'mydefault');
// execute()
$value = null;
if ($input->hasParsedOption('myoption')) {
$value = $input->getOption('myoption');
}
// => null when option not used, 'mydefault' when used without a value, the passed value otherwise am I right? |
@OndraM Yes, you're right, I'm waiting code review now |
This looks like the same as #11572. |
It is indeed the same. Closing as duplicate. |
I'd like to have console option behaving in this way:
However, as far as I know, there is currently not a clean way how to do it - because if you define default value for your option, then using
$input->getOption('myoption')
you will get the default value even for the case 1. (= even if the option was not passed at all).The real-world use case is that I have an option, that is not always needed, but when you use it, you need in 95% of cases the same value (smart default), but for the remaining edge cases you must have an ability how to override the default.
This was already suggested with some workaround in issue #8135 (comment), but this issue was primarily about different topic, so I am creating a separate issue for this.
The text was updated successfully, but these errors were encountered: