You can either pass the long or the short name of the option.
public hasOption ( string $name, boolean $includeBase = true ) : boolean | ||
$name | string | The long or short option name. |
$includeBase | boolean | Whether to include options in the base format in the search. |
Результат | boolean | Returns `true` if the option with the given name could be found and `false` otherwise. |
/** * {@inheritdoc} */ public function getCommandFrom($className) { if (class_exists($className)) { $accessor = PropertyAccess::createPropertyAccessor(); $reflector = new \ReflectionClass($className); $instance = $reflector->newInstanceWithoutConstructor(); foreach ($reflector->getProperties() as $property) { if ($instance instanceof ConsoleCommandInterface && $property->getName() == 'io') { continue; } if (!$this->format->hasArgument($property->getName()) && !$this->format->hasOption($property->getName())) { throw new \InvalidArgumentException(sprintf("There is not '%s' argument defined in the %s command", $property->getName(), $className)); } $value = null; if ($this->format->hasArgument($property->getName())) { $value = $this->args->getArgument($property->getName()); } elseif ($this->format->hasOption($property->getName())) { $value = $this->args->getOption($property->getName()); } $accessor->setValue($instance, $property->getName(), $value); } return $instance; } return; }
/** * Returns whether the builder contains a specific option. * * You can either pass the long or the short name of the option. * * @param string $name The long or short option name. * @param bool $includeBase Whether to include options in the base format * in the search. * * @return bool Returns `true` if the option with the given name could be * found and `false` otherwise. */ public function hasOption($name, $includeBase = true) { Assert::string($name, 'The option name must be a string or an integer. 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]) || isset($this->optionsByShortName[$name])) { return true; } if ($includeBase && $this->baseFormat) { return $this->baseFormat->hasOption($name); } return false; }
/** * Creates the arguments from the current class state. * * @param ArgsFormat $format The format. * @param RawArgs $rawArgs The raw arguments. * * @return Args The created console arguments. */ private function createArgs(ArgsFormat $format, RawArgs $rawArgs) { $args = new Args($format, $rawArgs); foreach ($this->arguments as $name => $value) { // Filter command names if ($format->hasArgument($name)) { $args->setArgument($name, $value); } } foreach ($this->options as $name => $value) { // Filter command options if ($format->hasOption($name)) { $args->setOption($name, $value); } } return $args; }
private function validateSubCommandName(SubCommandConfig $config) { $name = $config->getName(); if (!$name) { throw CannotAddCommandException::nameEmpty(); } if ($this->subCommands->contains($name)) { throw CannotAddCommandException::nameExists($name); } if ($config instanceof OptionCommandConfig) { if ($this->argsFormat->hasOption($name)) { throw CannotAddCommandException::optionExists($name); } if ($shortName = $config->getShortName()) { if ($this->subCommands->contains($shortName)) { throw CannotAddCommandException::nameExists($name); } if ($this->argsFormat->hasOption($shortName)) { throw CannotAddCommandException::optionExists($shortName); } } } }
/** * Returns whether an option is defined in the format. * * @param string $name The long or short option name. * * @return bool Returns `true` if the option exists and `false` otherwise. */ public function isOptionDefined($name) { return $this->format->hasOption($name); }
/** * @expectedException \InvalidArgumentException */ public function testHasOptionFailsIfIncludeBaseNoBoolean() { $format = new ArgsFormat(); $format->hasOption('option', 1234); }