Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ArgvInput.php

class ArgvInput

ArgvInput represents an input coming from the CLI arguments.

Usage:

$input = new ArgvInput();

By default, the `$_SERVER['argv']` array is used for the input values.

This can be overridden by explicitly passing the input values in the constructor:

$input = new ArgvInput($_SERVER['argv']);

If you pass it yourself, don't forget that the first element of the array is the name of the running application.

When passing an argument to the constructor, be sure that it respects the same rules as the argv one. It's almost always better to use the `StringInput` when you want to provide your own input.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\Console\Input\Input implements \Symfony\Component\Console\Input\InputInterface, \Symfony\Component\Console\Input\StreamableInputInterface
    • class \Symfony\Component\Console\Input\ArgvInput extends \Symfony\Component\Console\Input\Input

Expanded class hierarchy of ArgvInput

See also

http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html

http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#…

2 files declare their use of ArgvInput
Application.php in vendor/symfony/console/Application.php
CompletionInput.php in vendor/symfony/console/Completion/CompletionInput.php

File

vendor/symfony/console/Input/ArgvInput.php, line 41

Namespace

Symfony\Component\Console\Input
View source
class ArgvInput extends Input {
    
    /** @var list<string> */
    private array $tokens;
    private array $parsed;
    
    /** @param list<string>|null $argv */
    public function __construct(?array $argv = null, ?InputDefinition $definition = null) {
        $argv ??= $_SERVER['argv'] ?? [];
        foreach ($argv as $arg) {
            if (!\is_scalar($arg) && !$arg instanceof \Stringable) {
                throw new RuntimeException(\sprintf('Argument values expected to be all scalars, got "%s".', get_debug_type($arg)));
            }
        }
        // strip the application name
        array_shift($argv);
        $this->tokens = $argv;
        parent::__construct($definition);
    }
    
    /** @param list<string> $tokens */
    protected function setTokens(array $tokens) : void {
        $this->tokens = $tokens;
    }
    protected function parse() : void {
        $parseOptions = true;
        $this->parsed = $this->tokens;
        while (null !== ($token = array_shift($this->parsed))) {
            $parseOptions = $this->parseToken($token, $parseOptions);
        }
    }
    protected function parseToken(string $token, bool $parseOptions) : bool {
        if ($parseOptions && '' == $token) {
            $this->parseArgument($token);
        }
        elseif ($parseOptions && '--' == $token) {
            return false;
        }
        elseif ($parseOptions && str_starts_with($token, '--')) {
            $this->parseLongOption($token);
        }
        elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
            $this->parseShortOption($token);
        }
        else {
            $this->parseArgument($token);
        }
        return $parseOptions;
    }
    
    /**
     * Parses a short option.
     */
    private function parseShortOption(string $token) : void {
        $name = substr($token, 1);
        if (\strlen($name) > 1) {
            if ($this->definition
                ->hasShortcut($name[0]) && $this->definition
                ->getOptionForShortcut($name[0])
                ->acceptValue()) {
                // an option with a value (with no space)
                $this->addShortOption($name[0], substr($name, 1));
            }
            else {
                $this->parseShortOptionSet($name);
            }
        }
        else {
            $this->addShortOption($name, null);
        }
    }
    
    /**
     * Parses a short option set.
     *
     * @throws RuntimeException When option given doesn't exist
     */
    private function parseShortOptionSet(string $name) : void {
        $len = \strlen($name);
        for ($i = 0; $i < $len; ++$i) {
            if (!$this->definition
                ->hasShortcut($name[$i])) {
                $encoding = mb_detect_encoding($name, null, true);
                throw new RuntimeException(\sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding)));
            }
            $option = $this->definition
                ->getOptionForShortcut($name[$i]);
            if ($option->acceptValue()) {
                $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
                break;
            }
            $this->addLongOption($option->getName(), null);
        }
    }
    
    /**
     * Parses a long option.
     */
    private function parseLongOption(string $token) : void {
        $name = substr($token, 2);
        if (false !== ($pos = strpos($name, '='))) {
            if ('' === ($value = substr($name, $pos + 1))) {
                array_unshift($this->parsed, $value);
            }
            $this->addLongOption(substr($name, 0, $pos), $value);
        }
        else {
            $this->addLongOption($name, null);
        }
    }
    
    /**
     * Parses an argument.
     *
     * @throws RuntimeException When too many arguments are given
     */
    private function parseArgument(string $token) : void {
        $c = \count($this->arguments);
        // if input is expecting another argument, add it
        if ($this->definition
            ->hasArgument($c)) {
            $arg = $this->definition
                ->getArgument($c);
            $this->arguments[$arg->getName()] = $arg->isArray() ? [
                $token,
            ] : $token;
            // if last argument isArray(), append token to last argument
        }
        elseif ($this->definition
            ->hasArgument($c - 1) && $this->definition
            ->getArgument($c - 1)
            ->isArray()) {
            $arg = $this->definition
                ->getArgument($c - 1);
            $this->arguments[$arg->getName()][] = $token;
            // unexpected argument
        }
        else {
            $all = $this->definition
                ->getArguments();
            $symfonyCommandName = null;
            if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) {
                $symfonyCommandName = $this->arguments['command'] ?? null;
                unset($all[$key]);
            }
            if (\count($all)) {
                if ($symfonyCommandName) {
                    $message = \sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all)));
                }
                else {
                    $message = \sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all)));
                }
            }
            elseif ($symfonyCommandName) {
                $message = \sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token);
            }
            else {
                $message = \sprintf('No arguments expected, got "%s".', $token);
            }
            throw new RuntimeException($message);
        }
    }
    
    /**
     * Adds a short option value.
     *
     * @throws RuntimeException When option given doesn't exist
     */
    private function addShortOption(string $shortcut, mixed $value) : void {
        if (!$this->definition
            ->hasShortcut($shortcut)) {
            throw new RuntimeException(\sprintf('The "-%s" option does not exist.', $shortcut));
        }
        $this->addLongOption($this->definition
            ->getOptionForShortcut($shortcut)
            ->getName(), $value);
    }
    
    /**
     * Adds a long option value.
     *
     * @throws RuntimeException When option given doesn't exist
     */
    private function addLongOption(string $name, mixed $value) : void {
        if (!$this->definition
            ->hasOption($name)) {
            if (!$this->definition
                ->hasNegation($name)) {
                throw new RuntimeException(\sprintf('The "--%s" option does not exist.', $name));
            }
            $optionName = $this->definition
                ->negationToName($name);
            if (null !== $value) {
                throw new RuntimeException(\sprintf('The "--%s" option does not accept a value.', $name));
            }
            $this->options[$optionName] = false;
            return;
        }
        $option = $this->definition
            ->getOption($name);
        if (null !== $value && !$option->acceptValue()) {
            throw new RuntimeException(\sprintf('The "--%s" option does not accept a value.', $name));
        }
        if (\in_array($value, [
            '',
            null,
        ], true) && $option->acceptValue() && \count($this->parsed)) {
            // if option accepts an optional or mandatory argument
            // let's see if there is one provided
            $next = array_shift($this->parsed);
            if (isset($next[0]) && '-' !== $next[0] || \in_array($next, [
                '',
                null,
            ], true)) {
                $value = $next;
            }
            else {
                array_unshift($this->parsed, $next);
            }
        }
        if (null === $value) {
            if ($option->isValueRequired()) {
                throw new RuntimeException(\sprintf('The "--%s" option requires a value.', $name));
            }
            if (!$option->isArray() && !$option->isValueOptional()) {
                $value = true;
            }
        }
        if ($option->isArray()) {
            $this->options[$name][] = $value;
        }
        else {
            $this->options[$name] = $value;
        }
    }
    public function getFirstArgument() : ?string {
        $isOption = false;
        foreach ($this->tokens as $i => $token) {
            if ($token && '-' === $token[0]) {
                if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) {
                    continue;
                }
                // If it's a long option, consider that everything after "--" is the option name.
                // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator)
                $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1);
                if (!isset($this->options[$name]) && !$this->definition
                    ->hasShortcut($name)) {
                    // noop
                }
                elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition
                    ->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) {
                    $isOption = true;
                }
                continue;
            }
            if ($isOption) {
                $isOption = false;
                continue;
            }
            return $token;
        }
        return null;
    }
    public function hasParameterOption(string|array $values, bool $onlyParams = false) : bool {
        $values = (array) $values;
        foreach ($this->tokens as $token) {
            if ($onlyParams && '--' === $token) {
                return false;
            }
            foreach ($values as $value) {
                // Options with values:
                //   For long options, test for '--option=' at beginning
                //   For short options, test for '-o' at beginning
                $leading = str_starts_with($value, '--') ? $value . '=' : $value;
                if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) {
                    return true;
                }
            }
        }
        return false;
    }
    public function getParameterOption(string|array $values, string|bool|int|float|array|null $default = false, bool $onlyParams = false) : mixed {
        $values = (array) $values;
        $tokens = $this->tokens;
        while (0 < \count($tokens)) {
            $token = array_shift($tokens);
            if ($onlyParams && '--' === $token) {
                return $default;
            }
            foreach ($values as $value) {
                if ($token === $value) {
                    return array_shift($tokens);
                }
                // Options with values:
                //   For long options, test for '--option=' at beginning
                //   For short options, test for '-o' at beginning
                $leading = str_starts_with($value, '--') ? $value . '=' : $value;
                if ('' !== $leading && str_starts_with($token, $leading)) {
                    return substr($token, \strlen($leading));
                }
            }
        }
        return $default;
    }
    
    /**
     * Returns un-parsed and not validated tokens.
     *
     * @param bool $strip Whether to return the raw parameters (false) or the values after the command name (true)
     *
     * @return list<string>
     */
    public function getRawTokens(bool $strip = false) : array {
        if (!$strip) {
            return $this->tokens;
        }
        $parameters = [];
        $keep = false;
        foreach ($this->tokens as $value) {
            if (!$keep && $value === $this->getFirstArgument()) {
                $keep = true;
                continue;
            }
            if ($keep) {
                $parameters[] = $value;
            }
        }
        return $parameters;
    }
    
    /**
     * Returns a stringified representation of the args passed to the command.
     */
    public function __toString() : string {
        $tokens = array_map(function ($token) {
            if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
                return $match[1] . $this->escapeToken($match[2]);
            }
            if ($token && '-' !== $token[0]) {
                return $this->escapeToken($token);
            }
            return $token;
        }, $this->tokens);
        return implode(' ', $tokens);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ArgvInput::$parsed private property
ArgvInput::$tokens private property @var list&lt;string&gt; 1
ArgvInput::addLongOption private function Adds a long option value.
ArgvInput::addShortOption private function Adds a short option value.
ArgvInput::getFirstArgument public function Returns the first argument from the raw parameters (not parsed). Overrides InputInterface::getFirstArgument
ArgvInput::getParameterOption public function Returns the value of a raw option (not parsed). Overrides InputInterface::getParameterOption
ArgvInput::getRawTokens public function Returns un-parsed and not validated tokens.
ArgvInput::hasParameterOption public function Returns true if the raw parameters (not parsed) contain a value. Overrides InputInterface::hasParameterOption
ArgvInput::parse protected function Processes command line arguments. Overrides Input::parse
ArgvInput::parseArgument private function Parses an argument.
ArgvInput::parseLongOption private function Parses a long option.
ArgvInput::parseShortOption private function Parses a short option.
ArgvInput::parseShortOptionSet private function Parses a short option set.
ArgvInput::parseToken protected function 1
ArgvInput::setTokens protected function
ArgvInput::__construct public function Overrides Input::__construct 1
ArgvInput::__toString public function Returns a stringified representation of the args passed to the command. Overrides InputInterface::__toString 1
Input::$arguments protected property
Input::$definition protected property
Input::$interactive protected property
Input::$options protected property
Input::$stream protected property @var resource
Input::bind public function Binds the current Input instance with the given arguments and options. Overrides InputInterface::bind 1
Input::escapeToken public function Escapes a token through escapeshellarg if it contains unsafe chars.
Input::getArgument public function Returns the argument value for a given argument name. Overrides InputInterface::getArgument
Input::getArguments public function Returns all the given arguments merged with the default values. Overrides InputInterface::getArguments
Input::getOption public function Returns the option value for a given option name. Overrides InputInterface::getOption
Input::getOptions public function Returns all the given options merged with the default values. Overrides InputInterface::getOptions
Input::getStream public function Overrides StreamableInputInterface::getStream
Input::hasArgument public function Returns true if an InputArgument object exists by name or position. Overrides InputInterface::hasArgument
Input::hasOption public function Returns true if an InputOption object exists by name. Overrides InputInterface::hasOption
Input::isInteractive public function Is this input means interactive? Overrides InputInterface::isInteractive
Input::setArgument public function Sets an argument value by name. Overrides InputInterface::setArgument
Input::setInteractive public function Sets the input interactivity. Overrides InputInterface::setInteractive
Input::setOption public function Sets an option value by name. Overrides InputInterface::setOption
Input::setStream public function Overrides StreamableInputInterface::setStream
Input::validate public function Validates the input. Overrides InputInterface::validate

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal