You can either pass the name of the argument or the 0-based position of
the argument.
public hasArgument ( string | integer $name, boolean $includeBase = true ) : boolean | ||
$name | string | integer | The argument name or its 0-based position in the argument list. |
$includeBase | boolean | Whether to include arguments in the base format in the search. |
return | boolean | Returns `true` if the argument with the given name or position 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; }
/** * 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; }
/** * Creates a new adapter. * * @param ArgsFormat $format The adapted format. */ public function __construct(ArgsFormat $format) { parent::__construct(); $i = 1; foreach ($format->getCommandNames() as $commandName) { do { $argName = 'cmd' . $i++; } while ($format->hasArgument($argName)); $this->addArgument($argument = $this->adaptCommandName($commandName, $argName)); $this->commandNames[$argument->getName()] = $commandName; } foreach ($format->getCommandOptions() as $commandOption) { $this->addOption($this->adaptCommandOption($commandOption)); } foreach ($format->getOptions() as $option) { $this->addOption($this->adaptOption($option)); } foreach ($format->getArguments() as $argument) { $this->addArgument($this->adaptArgument($argument)); } }
/** * Returns whether an argument is defined in the format. * * @param string|int $name The argument name or its 0-based position in the * argument list. * * @return bool Returns `true` if the argument exists and `false` otherwise. */ public function isArgumentDefined($name) { return $this->format->hasArgument($name); }
/** * @expectedException \InvalidArgumentException */ public function testHasArgumentFailsIfIncludeBaseNoBoolean() { $format = new ArgsFormat(); $format->hasArgument('argument', 1234); }