symfony2 console arguments
Solution 1
It is possible:
->addOption('arg', 'a', InputOption::VALUE_NONE)
- my:command =>
$input->getOption('arg') //false
- my:command --arg =>
$input->getOption('arg') //true
- my:command --arg=5 =>
$input->getOption('arg') //5
Solution 2
Answer by corvax is incorrect and doesn't work. As of today, you just cannot achieve this.
It is even stated in the Console documentation: Using Command Options.
See also these issues on GitHub:
- #8135 [Console] Input options with InputOption::VALUE_OPTIONAL and InputOption::VALUE_NONE not working as expected
- #11883 [Console] Added three state long option
- #12769 [Console] Ability to use option default only if the option was passed
- #12773 [Console] Add method to know parsed option
Related videos on Youtube
kirilloid
javascript pirate follow me on @kirilloid_ru for moar pirate JS
Updated on June 27, 2022Comments
-
kirilloid about 2 years
I want to create a single named argument/option for symfony command. And want symfony to distinguish those 3 options:
my:command
, which means something likemy:command --arg=null
my:command --arg
, which meansmy:command --arg=defalutValue
my:command --arg=someValue
, which is fully explicit.
I.e. I want two working modes for code under that command: default one and non-default with additional argument, and that arg should have default value.
I understand, that I could create 2 args, but I'm looking for one-arg-to-rule-them-all solution.
Is it possible to accomplish that with built-in classes or should I create custom ones? If solution is only available with custom classes, please tell me, where to start (i.e. "create subclass of ..." or "install a bundle named ..."), cause I'm not familiar with Symfony2's architecture.
-
kirilloid about 12 yearsI already read the documentation and found no way to create this tree-way option/argument. And I don't need to use this component separately. I need a way this could be solved or approvement, this couldn't be solved.
-
manu almost 10 yearsCorrect me if I'm wrong, it's the first time I use this method, but I think that in order to obtain this result you have to use InputOption::VALUE_OPTIONAL as a third argument.
-
Adrien G over 9 yearsIndeed with the InputOption::VALUE_NONE you get a runtime exception : The option does not accept a value.
-
smarber about 9 yearsWith
InputOption::VALUE_NONE
we get an exception if the options gets a value