Use the methods in this class to dynamically build {@link ArgsFormat} instances. When you are done configuring the builder, call {@link getFormat()} to build an immutable {@link ArgsFormat}. For convenience, you can call {@link ArgsFormat::build()} to create a new builder and use its fluent API to configure and build a format: php $format = ArgsFormat::build() ->addCommandName(new CommandName('server')) ->addCommandOption(new CommandOption('add', 'a')) ->addArgument(new Argument('host')) ->addOption(new Option('port', 'p')) ->getFormat(); You can optionally pass a base format to inherit from. The arguments of the base format are prepended to the arguments of the built format. The options of the base format are added to the built options: php $baseFormat = ArgsFormat::build() ->addOption(new Option('verbose', 'v')) ->getFormat(); $format = ArgsFormat::build($baseFormat) ... ->getFormat(); Read {@link ArgsFormat} for a more detailed description of args formats.
See also: ArgsFormat
Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
 public function testBuildEmptyDefinition()
 {
     $definition = $this->builder->getFormat();
     $this->assertSame($this->baseFormat, $definition->getBaseFormat());
     $this->assertCount(0, $definition->getArguments());
     $this->assertCount(0, $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;
 }