} try { $option = new nbOption('n'); $t->fail('Couldn\'t create option with name shorter than 3 letters'); } catch (InvalidArgumentException $e) { $t->pass('Couldn\'t create option with name shorter than 3 letters'); } try { $option = new nbOption('foo', 'bar'); $t->fail('Couldn\'t create option with shortcut longer than 1 letter'); } catch (InvalidArgumentException $e) { $t->pass('Couldn\'t create option with shortcut longer than 1 letter'); } $t->comment('nbOptionTest - Test getters'); $option = new nbOption('foo'); $t->is($option->getName(), 'foo', '->getName() is "foo"'); $t->is($option->getShortcut(), null, 'shortcut is optional'); $t->is($option->getDescription(), '', 'default description is empty string'); $t->is($option->hasParameter(), false, 'default mode is "nbOption::PARAMETER_NONE"'); $t->is($option->hasShortcut(), false, '->hasShortcut() returns false if option has not shortcut'); $foo = new nbOption('foo', 'f', nbOption::PARAMETER_OPTIONAL, 'foo description'); $bar = new nbOption('bar', 'b', nbOption::PARAMETER_REQUIRED); $t->is($foo->hasShortcut(), true, '->hasShortcut() returns true if option has shortcut'); $t->is($foo->getShortcut(), 'f', '->getShortcut() returns shortcut if present'); $t->is($foo->getDescription(), 'foo description', '->getDescription() returns the description'); $t->comment('nbOptiontest - test mode'); $t->is($foo->hasParameter(), true, '->hasParameter() returns true if mode is "nbOption::PARAMETER_OPTIONAL" or "nbOption::PARAMETER_REQUIRED"'); $t->is($foo->hasOptionalParameter(), true, '->hasOptionalParameter() returns true if mode is "nbOption::PARAMETER_OPTIONAL"'); $t->is($bar->hasRequiredParameter(), true, '->hasRequiredParameter() returns true if mode is "nbOption::PARAMETER_REQUIRED"'); try { $option = new nbOption('foo', 'f', 55);
/** * Add a nbOption object. * * @param nbOption $option A nbOption object */ public function addOption(nbOption $option) { if ($this->hasOption($option->getName())) { throw new InvalidArgumentException(sprintf('[nbOptionSet::addOption] Option "%s" already exists', $option->getName())); } if ($this->hasOption($option->getShortcut())) { throw new InvalidArgumentException(sprintf('[nbOptionSet::addOption] Shortcut "%s" already registered', $option->getShortcut())); } $this->options[$option->getName()] = $option; if ($option->hasShortcut()) { $this->shortcuts[$option->getShortcut()] = $option; } if ($option->hasRequiredParameter()) { ++$this->requiredCount; } }
public function setOption(nbOption $option, $value) { if ($option->isArray()) { $this->optionValues[$option->getName()][] = $value; } else { $this->optionValues[$option->getName()] = $value; } }