The existing arguments stored in the builder are preserved.
You cannot add arguments after adding a multi-valued argument. If you do
so, this method throws an exception.
Adding required arguments after optional arguments is not supported.
Also in this case an exception is thrown.
public addArgument ( |
||
$argument | The argument to add. | |
리턴 | 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()); }
/** * 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; }
/** * Adds an argument. * * Read {@link Argument} for a more detailed description of console * arguments. * * @param string $name The argument name. * @param int $flags A bitwise combination of the flag constants in * the {@link Argument} class. * @param string $description A one-line description of the argument. * @param mixed $default The default value. Must be `null` if the * flags contain {@link Argument::REQUIRED}. * * @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. * * @see getArguments() */ public function addArgument($name, $flags = 0, $description = null, $default = null) { $this->formatBuilder->addArgument(new Argument($name, $flags, $description, $default)); return $this; }