You are here

private function ArgvInput::addLongOption 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::addLongOption()

Adds a long option value.

Parameters

string $name The long option key:

mixed $value The value for the option:

Throws

\RuntimeException When option given doesn't exist

3 calls to ArgvInput::addLongOption()
ArgvInput::addShortOption in vendor/symfony/console/Input/ArgvInput.php
Adds a short option value.
ArgvInput::parseLongOption in vendor/symfony/console/Input/ArgvInput.php
Parses a long option.
ArgvInput::parseShortOptionSet in vendor/symfony/console/Input/ArgvInput.php
Parses a short option set.

File

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

Class

ArgvInput
ArgvInput represents an input coming from the CLI arguments.

Namespace

Symfony\Component\Console\Input

Code

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;
  }
}