Skip to content
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

Closed
OndraM opened this issue Nov 30, 2014 · 7 comments
Closed

[Console] Ability to use option default only if the option was passed #12769

OndraM opened this issue Nov 30, 2014 · 7 comments
Labels

Comments

@OndraM
Copy link
Contributor

OndraM commented Nov 30, 2014

I'd like to have console option behaving in this way:

#1. The option is not used at all, so don't use any value of it
./app.php command
#2. Use default value of the option
./app.php command --myoption
#3. Use custom value of the option
./app.php command --myoption=myvalue

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.

@blanchonvincent
Copy link
Contributor

@OndraM $input->getOption('myoption') has a falsevalue by default if marked as InoutOption::VALUE_NONE, null otherwise : https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Console/Input/InputOption.php#L173

And if the option is ran, you will get true : https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Console/Input/ArgvInput.php#L246

So you should able to do $input->getOption('myoption') without problems :)

@OndraM
Copy link
Contributor Author

OndraM commented Nov 30, 2014

@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 InputOption::VALUE_NONE, it will only allow me to check whether the option was passed (and use some hard-coded value then) or not. The user won't be able to set its own value (see case 3. in the original description above).

@blanchonvincent
Copy link
Contributor

@OndraM #12773 Can you have a look?

@OndraM
Copy link
Contributor Author

OndraM commented Nov 30, 2014

@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?

@blanchonvincent
Copy link
Contributor

@OndraM Yes, you're right, I'm waiting code review now

@xabbuh
Copy link
Member

xabbuh commented Dec 1, 2014

This looks like the same as #11572.

@fabpot fabpot added the Console label Dec 7, 2014
@stof
Copy link
Member

stof commented Jan 3, 2015

It is indeed the same. Closing as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants