/** * Adds a command option to the builder. * * The existing command options stored in the builder are preserved. * * @param CommandOption $commandOption The command option to add. * * @return static The current instance. * * @throws CannotAddOptionException If the option cannot be added. * * @see addCommandOptions() */ public function addCommandOption(CommandOption $commandOption) { $longName = $commandOption->getLongName(); $shortName = $commandOption->getShortName(); $longAliases = $commandOption->getLongAliases(); $shortAliases = $commandOption->getShortAliases(); if ($this->hasOption($longName) || $this->hasCommandOption($longName)) { throw CannotAddOptionException::existsAlready($longName); } foreach ($longAliases as $shortAlias) { if ($this->hasOption($shortAlias) || $this->hasCommandOption($shortAlias)) { throw CannotAddOptionException::existsAlready($shortAlias); } } if ($shortName && ($this->hasOption($shortName) || $this->hasCommandOption($shortName))) { throw CannotAddOptionException::existsAlready($shortName); } foreach ($shortAliases as $shortAlias) { if ($this->hasOption($shortAlias) || $this->hasCommandOption($shortAlias)) { throw CannotAddOptionException::existsAlready($shortAlias); } } $this->commandOptions[$longName] = $commandOption; if ($shortName) { $this->commandOptionsByShortName[$shortName] = $commandOption; } foreach ($longAliases as $longAlias) { $this->commandOptions[$longAlias] = $commandOption; } foreach ($shortAliases as $shortAlias) { $this->commandOptionsByShortName[$shortAlias] = $commandOption; } return $this; }
/** * @dataProvider getValidAliases */ public function testValidAliases($alias, array $longAliases, array $shortAliases) { $option = new CommandOption('delete', null, array($alias)); $this->assertSame($longAliases, $option->getLongAliases()); $this->assertSame($shortAliases, $option->getShortAliases()); }