build() public static method

You can optionally pass a base format. The built format inherits all the arguments and options defined in the base format.
public static build ( ArgsFormat $baseFormat = null ) : ArgsFormatBuilder
$baseFormat ArgsFormat The base format.
return ArgsFormatBuilder The created builder.
コード例 #1
0
ファイル: ArgsFormatTest.php プロジェクト: webmozart/console
 public function testBuild()
 {
     $format = ArgsFormat::build()->addCommandName($server = new CommandName('server'))->addCommandOption($add = new CommandOption('add', 'a'))->addArgument($host = new Argument('host'))->addOption($port = new Option('port', 'p'))->getFormat();
     $this->assertSame(array($server), $format->getCommandNames());
     $this->assertSame(array($add), $format->getCommandOptions());
     $this->assertSame(array('host' => $host), $format->getArguments());
     $this->assertSame(array('port' => $port), $format->getOptions());
     $this->assertSame(1, $format->getNumberOfArguments());
     $this->assertSame(0, $format->getNumberOfRequiredArguments());
 }
コード例 #2
0
ファイル: CommandTest.php プロジェクト: webmozart/console
 public function testInheritApplicationArgsFormat()
 {
     $baseFormat = ArgsFormat::build()->addArgument(new Argument('global-argument'))->addOption(new Option('global-option'))->getFormat();
     $this->application->expects($this->any())->method('getGlobalArgsFormat')->willReturn($baseFormat);
     $config = new CommandConfig('command');
     $config->addArgument('argument');
     $config->addOption('option');
     $command = new Command($config, $this->application);
     $argsFormat = $command->getArgsFormat();
     $this->assertSame($baseFormat, $argsFormat->getBaseFormat());
     $this->assertCount(2, $argsFormat->getArguments());
     $this->assertTrue($argsFormat->hasArgument('argument'));
     $this->assertTrue($argsFormat->hasArgument('global-argument'));
     $this->assertCount(2, $argsFormat->getOptions());
     $this->assertTrue($argsFormat->hasOption('option'));
     $this->assertTrue($argsFormat->hasOption('global-option'));
 }
コード例 #3
0
ファイル: ApplicationHelp.php プロジェクト: webmozart/console
 /**
  * {@inheritdoc}
  */
 protected function renderHelp(BlockLayout $layout)
 {
     $help = $this->application->getConfig()->getHelp();
     $commands = $this->application->getNamedCommands();
     $globalArgsFormat = $this->application->getGlobalArgsFormat();
     $argsFormat = ArgsFormat::build()->addArgument(new Argument('command', Argument::REQUIRED, 'The command to execute'))->addArgument(new Argument('arg', Argument::MULTI_VALUED, 'The arguments of the command'))->addOptions($globalArgsFormat->getOptions())->getFormat();
     $this->renderName($layout, $this->application);
     $this->renderUsage($layout, $this->application, $argsFormat);
     $this->renderArguments($layout, $argsFormat->getArguments());
     if ($argsFormat->hasOptions()) {
         $this->renderGlobalOptions($layout, $argsFormat->getOptions());
     }
     if (!$commands->isEmpty()) {
         $this->renderCommands($layout, $commands);
     }
     if ($help) {
         $this->renderDescription($layout, $help);
     }
 }
コード例 #4
0
 public function testBuildAnonymousArgsFormat()
 {
     $baseFormat = new ArgsFormat();
     $this->config->setName('command');
     $this->config->setAliases(array('alias1', 'alias2'));
     $this->config->addOption('option');
     $this->config->addArgument('argument');
     $this->config->markAnonymous();
     $expected = ArgsFormat::build($baseFormat)->addArgument(new Argument('argument'))->addOption(new Option('option'))->getFormat();
     $this->assertEquals($expected, $this->config->buildArgsFormat($baseFormat));
 }
コード例 #5
0
 public function testParseSetsRawArgs()
 {
     $rawArgs = new StringArgs('server');
     $format = ArgsFormat::build()->addCommandName(new CommandName('server'))->getFormat();
     $args = $this->parser->parseArgs($rawArgs, $format);
     $this->assertSame($rawArgs, $args->getRawArgs());
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function buildArgsFormat(ArgsFormat $baseFormat = null)
 {
     $formatBuilder = ArgsFormat::build($baseFormat);
     if (!$this->isAnonymous()) {
         $flags = $this->isLongNamePreferred() ? CommandOption::PREFER_LONG_NAME : CommandOption::PREFER_SHORT_NAME;
         $formatBuilder->addCommandOption(new CommandOption($this->getName(), $this->getShortName(), $this->getAliases(), $flags));
     }
     $formatBuilder->addOptions($this->getOptions());
     $formatBuilder->addArguments($this->getArguments());
     return $formatBuilder->getFormat();
 }
コード例 #7
0
 public function testAdaptOptionWithDescriptionAndDefault()
 {
     $argsFormat = ArgsFormat::build()->addOption(new Option('option', 'o', Option::OPTIONAL_VALUE, 'The description', 'The default'))->getFormat();
     $adapter = new ArgsFormatInputDefinition($argsFormat);
     $this->assertEquals(array(), $adapter->getArguments());
     $this->assertEquals(array('option' => new InputOption('option', 'o', InputOption::VALUE_OPTIONAL, 'The description', 'The default')), $adapter->getOptions());
 }
コード例 #8
0
ファイル: CommandConfig.php プロジェクト: webmozart/console
 /**
  * Builds an {@link ArgsFormat} instance with the given base format.
  *
  * @param ArgsFormat $baseFormat The base format.
  *
  * @return ArgsFormat The built format for the console arguments.
  */
 public function buildArgsFormat(ArgsFormat $baseFormat = null)
 {
     $formatBuilder = ArgsFormat::build($baseFormat);
     if (!$this->anonymous) {
         $formatBuilder->addCommandName(new CommandName($this->name, $this->aliases));
     }
     $formatBuilder->addOptions($this->getOptions());
     $formatBuilder->addArguments($this->getArguments());
     return $formatBuilder->getFormat();
 }
コード例 #9
0
ファイル: ArgsTest.php プロジェクト: webmozart/console
 public function testIsArgumentDefined()
 {
     $format = ArgsFormat::build()->addArgument(new Argument('argument1'))->addArgument(new Argument('argument2'))->getFormat();
     $args = new Args($format);
     $this->assertTrue($args->isArgumentDefined('argument1'));
     $this->assertTrue($args->isArgumentDefined('argument2'));
     $this->assertFalse($args->isArgumentDefined('foo'));
 }