addOption() public method

The existing options stored in the builder are preserved.
See also: addOptions()
public addOption ( Option $option ) : static
$option Option The option to add.
return static The current instance.
 public function testGetDefinitionWithBaseDefinition()
 {
     $this->baseFormatBuilder->addCommandName($server = new CommandName('server'));
     $this->baseFormatBuilder->addArgument($argument1 = new Argument('argument1'));
     $this->baseFormatBuilder->addOption($option1 = new Option('option1'));
     $this->builder = new ArgsFormatBuilder($baseDefinition = $this->baseFormatBuilder->getFormat());
     $this->builder->addCommandName($add = new CommandName('add'));
     $this->builder->addArgument($argument2 = new Argument('argument2'));
     $this->builder->addArgument($argument3 = new Argument('argument3'));
     $this->builder->addOption($option2 = new Option('option2'));
     $this->builder->addOption($option3 = new Option('option3'));
     $definition = $this->builder->getFormat();
     $this->assertSame($baseDefinition, $definition->getBaseFormat());
     // base command names are returned first
     $this->assertSame(array($server, $add), $definition->getCommandNames());
     // base arguments are returned first
     $this->assertSame(array('argument1' => $argument1, 'argument2' => $argument2, 'argument3' => $argument3), $definition->getArguments());
     // base options are returned last
     $this->assertSame(array('option2' => $option2, 'option3' => $option3, 'option1' => $option1), $definition->getOptions());
 }
Esempio n. 2
0
 /**
  * Creates a format builder for a set of arguments and options.
  *
  * @param array      $elements   The arguments and options to add to the
  *                               builder.
  * @param ArgsFormat $baseFormat The base format.
  *
  * @return ArgsFormatBuilder The created builder.
  */
 private function createBuilderForElements(array $elements, ArgsFormat $baseFormat = null)
 {
     $builder = new ArgsFormatBuilder($baseFormat);
     foreach ($elements as $element) {
         if ($element instanceof CommandName) {
             $builder->addCommandName($element);
         } elseif ($element instanceof CommandOption) {
             $builder->addCommandOption($element);
         } elseif ($element instanceof Option) {
             $builder->addOption($element);
         } elseif ($element instanceof Argument) {
             $builder->addArgument($element);
         } else {
             throw new InvalidArgumentException(sprintf('Expected instances of CommandName, CommandOption, ' . 'Option or Argument. Got: %s', is_object($element) ? get_class($element) : gettype($element)));
         }
     }
     return $builder;
 }
Esempio n. 3
0
 /**
  * Adds an option.
  *
  * Read {@link Option} for a more detailed description of console options.
  *
  * @param string $longName    The long option name.
  * @param string $shortName   The short option name. Can be `null`.
  * @param int    $flags       A bitwise combination of the flag constants in
  *                            the {@link Option} class.
  * @param string $description A one-line description of the option.
  * @param mixed  $default     The default value. Must be `null` if the
  *                            flags contain {@link Option::REQUIRED_VALUE}.
  * @param string $valueName   The name of the value to be used in usage
  *                            examples of the option.
  *
  * @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance.
  *
  * @see getOptions()
  */
 public function addOption($longName, $shortName = null, $flags = 0, $description = null, $default = null, $valueName = '...')
 {
     $this->formatBuilder->addOption(new Option($longName, $shortName, $flags, $description, $default, $valueName));
     return $this;
 }