addArgument($definition); } elseif ($definition instanceof InputOption) { $this->addOption($definition); } else { throw new InvalidArgumentException('Input definition must be an instance of InputArgument or InputOption.'); } } } public function addArgument(InputArgument $argument): void { if (isset($this->arguments[$argument->getName()])) { throw new InvalidArgumentException(sprintf('An argument with name "%s" already exists.', $argument->getName())); } $this->arguments[$argument->getName()] = $argument; } public function addOption(InputOption $option): void { if (isset($this->options[$option->getName()])) { throw new InvalidArgumentException(sprintf('An option with name "%s" already exists.', $option->getName())); } if ($option->getShortcut()) { foreach (explode('|', $option->getShortcut()) as $shortcut) { if (isset($this->shortcuts[$shortcut])) { throw new InvalidArgumentException(sprintf('An option with shortcut "%s" already exists.', $shortcut)); } $this->shortcuts[$shortcut] = $option->getName(); } } $this->options[$option->getName()] = $option; } public function getArguments(): array { return $this->arguments; } public function getArgument(string $name): InputArgument { if (!$this->hasArgument($name)) { throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); } return $this->arguments[$name]; } public function hasArgument(string $name): bool { return isset($this->arguments[$name]); } public function getArgumentCount(): int { return count($this->arguments); } public function getArgumentRequiredCount(): int { $count = 0; foreach ($this->arguments as $argument) { if ($argument->isRequired()) { $count++; } } return $count; } public function getOptions(): array { return $this->options; } public function getOption(string $name): InputOption { if (!$this->hasOption($name)) { throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); } return $this->options[$name]; } public function hasOption(string $name): bool { return isset($this->options[$name]); } public function hasShortcut(string $shortcut): bool { return isset($this->shortcuts[$shortcut]); } public function getOptionForShortcut(string $shortcut): InputOption { if (!$this->hasShortcut($shortcut)) { throw new InvalidArgumentException(sprintf('The "-%s" shortcut does not exist.', $shortcut)); } return $this->getOption($this->shortcuts[$shortcut]); } public function getSynopsis(): string { $elements = []; foreach ($this->getOptions() as $option) { if ($option->isRequired()) { $elements[] = sprintf('--%s', $option->getName()); } } foreach ($this->getArguments() as $argument) { $elements[] = $argument->getName(); } return implode(' ', $elements); } public function setDefinition(array $definitions): void { $this->arguments = []; $this->options = []; $this->shortcuts = []; foreach ($definitions as $definition) { if ($definition instanceof InputArgument) { $this->addArgument($definition); } elseif ($definition instanceof InputOption) { $this->addOption($definition); } } } public function merge(self $definition): void { foreach ($definition->getArguments() as $argument) { $this->addArgument($argument); } foreach ($definition->getOptions() as $option) { $this->addOption($option); } } public function __toString(): string { $synopsis = ''; foreach ($this->getOptions() as $option) { $synopsis .= ' ' . $option->getSynopsis(); } foreach ($this->getArguments() as $argument) { $synopsis .= ' ' . $argument->getSynopsis(); } return trim($synopsis); } }