symfony2 console arguments

10,274

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:

Share:
10,274

Related videos on Youtube

kirilloid
Author by

kirilloid

javascript pirate follow me on @kirilloid_ru for moar pirate JS

Updated on June 27, 2022

Comments

  • kirilloid
    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 like my:command --arg=null
    • my:command --arg, which means my: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
    kirilloid about 12 years
    I 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
    manu almost 10 years
    Correct 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
    Adrien G over 9 years
    Indeed with the InputOption::VALUE_NONE you get a runtime exception : The option does not accept a value.
  • smarber
    smarber about 9 years
    With InputOption::VALUE_NONE we get an exception if the options gets a value