public function testExceptionIsThrowWhenTryingToAddRequiredArgAfterOptionalArg() { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('A required argument cannot follow an optional argument'); $definition = new CommandDefinition('animal', [], 'strlen'); $definition->addArgument(CommandArgument::optional('optional-arg'))->addArgument(CommandArgument::required('required-arg')); }
/** * @param string|CommandArgument $argument * @return $this */ public function addArgument($argument) { if (!is_string($argument) && !$argument instanceof CommandArgument) { throw InvalidArgumentException::notValidParameter('argument', ['string', CommandArgument::class], $argument); } if (is_string($argument)) { $argument = new CommandArgument($argument); } if (count($this->args) === 0) { $this->args[] = $argument; return $this; } $previousArgument = end($this->args); if ($previousArgument->isOptional() && $argument->isRequired()) { throw new InvalidArgumentException(sprintf('A required argument cannot follow an optional argument')); } $this->args[] = $argument; return $this; }
/** * Add any extra required arguments to the command. * * @param CommandDefinition $commandDefinition */ public function configureInput(CommandDefinition $commandDefinition) { $commandDefinition->addArgument(CommandArgument::required('program')); }
public function testOptionalArgument() { $arg = new CommandArgument('arg1', true); $this->assertSame('arg1', $arg->getName()); $this->assertTrue($arg->isOptional()); }