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

Breadcrumb

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

class InputArgument

Same name in this branch
  1. 11.1.x vendor/composer/composer/src/Composer/Console/Input/InputArgument.php \Composer\Console\Input\InputArgument

Represents a command line argument.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\Console\Input\InputArgument

Expanded class hierarchy of InputArgument

23 files declare their use of InputArgument
Application.php in vendor/symfony/console/Application.php
Command.php in vendor/symfony/console/Command/Command.php
DebugCommand.php in vendor/symfony/serializer/Command/DebugCommand.php
DebugCommand.php in vendor/symfony/validator/Command/DebugCommand.php
Descriptor.php in vendor/symfony/console/Descriptor/Descriptor.php

... See full list

File

vendor/symfony/console/Input/InputArgument.php, line 26

Namespace

Symfony\Component\Console\Input
View source
class InputArgument {
    
    /**
     * Providing an argument is required (e.g. just 'app:foo' is not allowed).
     */
    public const REQUIRED = 1;
    
    /**
     * Providing an argument is optional (e.g. 'app:foo' and 'app:foo bar' are both allowed). This is the default behavior of arguments.
     */
    public const OPTIONAL = 2;
    
    /**
     * The argument accepts multiple values and turn them into an array (e.g. 'app:foo bar baz' will result in value ['bar', 'baz']).
     */
    public const IS_ARRAY = 4;
    private int $mode;
    private string|int|bool|array|float|null $default;
    
    /**
     * @param string                                                                        $name            The argument name
     * @param int-mask-of<InputArgument::*>|null                                            $mode            The argument mode: a bit mask of self::REQUIRED, self::OPTIONAL and self::IS_ARRAY
     * @param string                                                                        $description     A description text
     * @param string|bool|int|float|array|null                                              $default         The default value (for self::OPTIONAL mode only)
     * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
     *
     * @throws InvalidArgumentException When argument mode is not valid
     */
    public function __construct(string $name, ?int $mode = null, string $description = '', string|bool|int|float|array|null $default = null, \Closure|array $suggestedValues = []) {
        if (null === $mode) {
            $mode = self::OPTIONAL;
        }
        elseif ($mode >= self::IS_ARRAY << 1 || $mode < 1) {
            throw new InvalidArgumentException(\sprintf('Argument mode "%s" is not valid.', $mode));
        }
        $this->mode = $mode;
        $this->setDefault($default);
    }
    
    /**
     * Returns the argument name.
     */
    public function getName() : string {
        return $this->name;
    }
    
    /**
     * Returns true if the argument is required.
     *
     * @return bool true if parameter mode is self::REQUIRED, false otherwise
     */
    public function isRequired() : bool {
        return self::REQUIRED === (self::REQUIRED & $this->mode);
    }
    
    /**
     * Returns true if the argument can take multiple values.
     *
     * @return bool true if mode is self::IS_ARRAY, false otherwise
     */
    public function isArray() : bool {
        return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
    }
    
    /**
     * Sets the default value.
     */
    public function setDefault(string|bool|int|float|array|null $default) : void {
        if ($this->isRequired() && null !== $default) {
            throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
        }
        if ($this->isArray()) {
            if (null === $default) {
                $default = [];
            }
            elseif (!\is_array($default)) {
                throw new LogicException('A default value for an array argument must be an array.');
            }
        }
        $this->default = $default;
    }
    
    /**
     * Returns the default value.
     */
    public function getDefault() : string|bool|int|float|array|null {
        return $this->default;
    }
    
    /**
     * Returns true if the argument has values for input completion.
     */
    public function hasCompletion() : bool {
        return [] !== $this->suggestedValues;
    }
    
    /**
     * Supplies suggestions when command resolves possible completion options for input.
     *
     * @see Command::complete()
     */
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions) : void {
        $values = $this->suggestedValues;
        if ($values instanceof \Closure && !\is_array($values = $values($input))) {
            throw new LogicException(\sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
        }
        if ($values) {
            $suggestions->suggestValues($values);
        }
    }
    
    /**
     * Returns the description text.
     */
    public function getDescription() : string {
        return $this->description;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
InputArgument::$default private property
InputArgument::$mode private property
InputArgument::complete public function Supplies suggestions when command resolves possible completion options for input. 1
InputArgument::getDefault public function Returns the default value.
InputArgument::getDescription public function Returns the description text.
InputArgument::getName public function Returns the argument name.
InputArgument::hasCompletion public function Returns true if the argument has values for input completion.
InputArgument::isArray public function Returns true if the argument can take multiple values.
InputArgument::isRequired public function Returns true if the argument is required.
InputArgument::IS_ARRAY public constant The argument accepts multiple values and turn them into an array (e.g. &#039;app:foo bar baz&#039; will result in value [&#039;bar&#039;, &#039;baz&#039;]).
InputArgument::OPTIONAL public constant Providing an argument is optional (e.g. &#039;app:foo&#039; and &#039;app:foo bar&#039; are both allowed). This is the default behavior of arguments.
InputArgument::REQUIRED public constant Providing an argument is required (e.g. just &#039;app:foo&#039; is not allowed).
InputArgument::setDefault public function Sets the default value.
InputArgument::__construct public function 1

API Navigation

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