You are here

class ArgvInput in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Input/ArgvInput.php \Symfony\Component\Console\Input\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

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#t...

4 files declare their use of ArgvInput
Application.php in vendor/symfony/console/Application.php
ApplicationTest.php in vendor/symfony/console/Tests/ApplicationTest.php
ArgvInputTest.php in vendor/symfony/console/Tests/Input/ArgvInputTest.php
DebugHandlersListenerTest.php in vendor/symfony/http-kernel/Tests/EventListener/DebugHandlersListenerTest.php

File

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

Namespace

Symfony\Component\Console\Input
View source
class ArgvInput extends Input {
  private $tokens;
  private $parsed;

  /**
   * Constructor.
   *
   * @param array           $argv       An array of parameters from the CLI (in the argv format)
   * @param InputDefinition $definition A InputDefinition instance
   */
  public function __construct(array $argv = null, InputDefinition $definition = null) {
    if (null === $argv) {
      $argv = $_SERVER['argv'];
    }

    // strip the application name
    array_shift($argv);
    $this->tokens = $argv;
    parent::__construct($definition);
  }
  protected function setTokens(array $tokens) {
    $this->tokens = $tokens;
  }

  /**
   * Processes command line arguments.
   */
  protected function parse() {
    $parseOptions = true;
    $this->parsed = $this->tokens;
    while (null !== ($token = array_shift($this->parsed))) {
      if ($parseOptions && '' == $token) {
        $this
          ->parseArgument($token);
      }
      elseif ($parseOptions && '--' == $token) {
        $parseOptions = false;
      }
      elseif ($parseOptions && 0 === strpos($token, '--')) {
        $this
          ->parseLongOption($token);
      }
      elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
        $this
          ->parseShortOption($token);
      }
      else {
        $this
          ->parseArgument($token);
      }
    }
  }

  /**
   * Parses a short option.
   *
   * @param string $token The current token.
   */
  private function parseShortOption($token) {
    $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.
   *
   * @param string $name The current token
   *
   * @throws \RuntimeException When option given doesn't exist
   */
  private function parseShortOptionSet($name) {
    $len = strlen($name);
    for ($i = 0; $i < $len; ++$i) {
      if (!$this->definition
        ->hasShortcut($name[$i])) {
        throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
      }
      $option = $this->definition
        ->getOptionForShortcut($name[$i]);
      if ($option
        ->acceptValue()) {
        $this
          ->addLongOption($option
          ->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
        break;
      }
      else {
        $this
          ->addLongOption($option
          ->getName(), null);
      }
    }
  }

  /**
   * Parses a long option.
   *
   * @param string $token The current token
   */
  private function parseLongOption($token) {
    $name = substr($token, 2);
    if (false !== ($pos = strpos($name, '='))) {
      $this
        ->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
    }
    else {
      $this
        ->addLongOption($name, null);
    }
  }

  /**
   * Parses an argument.
   *
   * @param string $token The current token
   *
   * @throws \RuntimeException When too many arguments are given
   */
  private function parseArgument($token) {
    $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() ? array(
        $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 {
      throw new \RuntimeException('Too many arguments.');
    }
  }

  /**
   * Adds a short option value.
   *
   * @param string $shortcut The short option key
   * @param mixed  $value    The value for the option
   *
   * @throws \RuntimeException When option given doesn't exist
   */
  private function addShortOption($shortcut, $value) {
    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.
   *
   * @param string $name  The long option key
   * @param mixed  $value The value for the option
   *
   * @throws \RuntimeException When option given doesn't exist
   */
  private function addLongOption($name, $value) {
    if (!$this->definition
      ->hasOption($name)) {
      throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
    }
    $option = $this->definition
      ->getOption($name);

    // Convert empty values to null
    if (!isset($value[0])) {
      $value = null;
    }
    if (null !== $value && !$option
      ->acceptValue()) {
      throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
    }
    if (null === $value && $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]) {
        $value = $next;
      }
      elseif (empty($next)) {
        $value = '';
      }
      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()) {
        $value = $option
          ->isValueOptional() ? $option
          ->getDefault() : true;
      }
    }
    if ($option
      ->isArray()) {
      $this->options[$name][] = $value;
    }
    else {
      $this->options[$name] = $value;
    }
  }

  /**
   * Returns the first argument from the raw parameters (not parsed).
   *
   * @return string The value of the first argument or null otherwise
   */
  public function getFirstArgument() {
    foreach ($this->tokens as $token) {
      if ($token && '-' === $token[0]) {
        continue;
      }
      return $token;
    }
  }

  /**
   * Returns true if the raw parameters (not parsed) contain a value.
   *
   * This method is to be used to introspect the input parameters
   * before they have been validated. It must be used carefully.
   *
   * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
   *
   * @return bool true if the value is contained in the raw parameters
   */
  public function hasParameterOption($values) {
    $values = (array) $values;
    foreach ($this->tokens as $token) {
      foreach ($values as $value) {
        if ($token === $value || 0 === strpos($token, $value . '=')) {
          return true;
        }
      }
    }
    return false;
  }

  /**
   * Returns the value of a raw option (not parsed).
   *
   * This method is to be used to introspect the input parameters
   * before they have been validated. It must be used carefully.
   *
   * @param string|array $values  The value(s) to look for in the raw parameters (can be an array)
   * @param mixed        $default The default value to return if no result is found
   *
   * @return mixed The option value
   */
  public function getParameterOption($values, $default = false) {
    $values = (array) $values;
    $tokens = $this->tokens;
    while (0 < count($tokens)) {
      $token = array_shift($tokens);
      foreach ($values as $value) {
        if ($token === $value || 0 === strpos($token, $value . '=')) {
          if (false !== ($pos = strpos($token, '='))) {
            return substr($token, $pos + 1);
          }
          return array_shift($tokens);
        }
      }
    }
    return $default;
  }

  /**
   * Returns a stringified representation of the args passed to the command.
   *
   * @return string
   */
  public function __toString() {
    $self = $this;
    $tokens = array_map(function ($token) use ($self) {
      if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
        return $match[1] . $self
          ->escapeToken($match[2]);
      }
      if ($token && $token[0] !== '-') {
        return $self
          ->escapeToken($token);
      }
      return $token;
    }, $this->tokens);
    return implode(' ', $tokens);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArgvInput::$parsed private property
ArgvInput::$tokens private property
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::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::setTokens protected function
ArgvInput::__construct public function Constructor. Overrides Input::__construct 1
ArgvInput::__toString public function Returns a stringified representation of the args passed to the command.
Input::$arguments protected property
Input::$definition protected property
Input::$interactive protected property
Input::$options protected property
Input::bind public function Binds the current Input instance with the given arguments and options. Overrides InputInterface::bind
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 the argument 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 the options values. Overrides InputInterface::getOptions
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 Checks if the input is 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::validate public function Validates the input. Overrides InputInterface::validate