/** * Returns an option by its long or short name. * * @param string $name The long or short option name. * @param bool $includeBase Whether to include options in the base format * in the search. * * @return Option The option. * * @throws NoSuchOptionException If the option with the given name does not * not exist. */ public function getOption($name, $includeBase = true) { Assert::string($name, 'The option name must be a string. Got: %s'); Assert::notEmpty($name, 'The option name must not be empty.'); Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s'); if (isset($this->options[$name])) { return $this->options[$name]; } if (isset($this->optionsByShortName[$name])) { return $this->optionsByShortName[$name]; } if ($includeBase && $this->baseFormat) { return $this->baseFormat->getOption($name); } throw NoSuchOptionException::forOptionName($name); }
/** * Sets an option. * * For options with values, you can pass the value in the second argument. * The value is converted to the type defined by the argument format. * * For options without values, you can omit the second argument. Optionally, * you can pass `true`/`false` explicitly to enable/disable the option. * * @param string $name The long or short option name. * @param mixed $value The value to set for the option. * * @return static The current instance. * * @throws NoSuchOptionException If the option does not exist. */ public function setOption($name, $value = true) { $option = $this->format->getOption($name); if ($option->isMultiValued()) { $value = (array) $value; foreach ($value as $k => $v) { $value[$k] = $option->parseValue($v); } } elseif ($option->acceptsValue()) { $value = $option->parseValue($value); } elseif (false === $value) { unset($this->options[$option->getLongName()]); return $this; } else { $value = true; } $this->options[$option->getLongName()] = $value; return $this; }
/** * @expectedException \InvalidArgumentException */ public function testGetOptionFailsIfIncludeBaseNoBoolean() { $format = new ArgsFormat(); $format->getOption('argument', 1234); }