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

Breadcrumb

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

class Parser

Same name in this branch
  1. 11.1.x vendor/open-telemetry/api/Baggage/Propagation/Parser.php \OpenTelemetry\API\Baggage\Propagation\Parser
  2. 11.1.x vendor/sebastian/diff/src/Parser.php \SebastianBergmann\Diff\Parser
  3. 11.1.x vendor/egulias/email-validator/src/Parser.php \Egulias\EmailValidator\Parser
  4. 11.1.x vendor/twig/twig/src/Parser.php \Twig\Parser
  5. 11.1.x vendor/symfony/css-selector/Parser/Parser.php \Symfony\Component\CssSelector\Parser\Parser
  6. 11.1.x vendor/symfony/yaml/Parser.php \Symfony\Component\Yaml\Parser
  7. 11.1.x vendor/mck89/peast/lib/Peast/Selector/Parser.php \Peast\Selector\Parser
  8. 11.1.x vendor/mck89/peast/lib/Peast/Syntax/Parser.php \Peast\Syntax\Parser

Hierarchy

  • class \SebastianBergmann\CliParser\Parser

Expanded class hierarchy of Parser

2 files declare their use of Parser
Builder.php in vendor/phpunit/phpunit/src/TextUI/Configuration/Cli/Builder.php
ExcludeList.php in vendor/phpunit/phpunit/src/Util/ExcludeList.php

File

vendor/sebastian/cli-parser/src/Parser.php, line 34

Namespace

SebastianBergmann\CliParser
View source
final class Parser {
    
    /**
     * @psalm-param list<string> $argv
     * @psalm-param list<string> $longOptions
     *
     * @psalm-return array{0: array, 1: array}
     *
     * @throws AmbiguousOptionException
     * @throws OptionDoesNotAllowArgumentException
     * @throws RequiredOptionArgumentMissingException
     * @throws UnknownOptionException
     */
    public function parse(array $argv, string $shortOptions, ?array $longOptions = null) : array {
        if (empty($argv)) {
            return [
                [],
                [],
            ];
        }
        $options = [];
        $nonOptions = [];
        if ($longOptions) {
            sort($longOptions);
        }
        if (isset($argv[0][0]) && $argv[0][0] !== '-') {
            array_shift($argv);
        }
        reset($argv);
        $argv = array_map('trim', $argv);
        while (false !== ($arg = current($argv))) {
            $i = key($argv);
            assert(is_int($i));
            next($argv);
            if ($arg === '') {
                continue;
            }
            if ($arg === '--') {
                $nonOptions = array_merge($nonOptions, array_slice($argv, $i + 1));
                break;
            }
            if ($arg[0] !== '-' || strlen($arg) > 1 && $arg[1] === '-' && !$longOptions) {
                $nonOptions[] = $arg;
                continue;
            }
            if (strlen($arg) > 1 && $arg[1] === '-' && is_array($longOptions)) {
                $this->parseLongOption(substr($arg, 2), $longOptions, $options, $argv);
                continue;
            }
            $this->parseShortOption(substr($arg, 1), $shortOptions, $options, $argv);
        }
        return [
            $options,
            $nonOptions,
        ];
    }
    
    /**
     * @throws RequiredOptionArgumentMissingException
     */
    private function parseShortOption(string $argument, string $shortOptions, array &$options, array &$argv) : void {
        $argumentLength = strlen($argument);
        for ($i = 0; $i < $argumentLength; $i++) {
            $option = $argument[$i];
            $optionArgument = null;
            if ($argument[$i] === ':' || ($spec = strstr($shortOptions, $option)) === false) {
                throw new UnknownOptionException('-' . $option);
            }
            if (strlen($spec) > 1 && $spec[1] === ':') {
                if ($i + 1 < $argumentLength) {
                    $options[] = [
                        $option,
                        substr($argument, $i + 1),
                    ];
                    break;
                }
                if (!(strlen($spec) > 2 && $spec[2] === ':')) {
                    $optionArgument = current($argv);
                    if (!$optionArgument) {
                        throw new RequiredOptionArgumentMissingException('-' . $option);
                    }
                    assert(is_string($optionArgument));
                    next($argv);
                }
            }
            $options[] = [
                $option,
                $optionArgument,
            ];
        }
    }
    
    /**
     * @psalm-param list<string> $longOptions
     *
     * @throws AmbiguousOptionException
     * @throws OptionDoesNotAllowArgumentException
     * @throws RequiredOptionArgumentMissingException
     * @throws UnknownOptionException
     */
    private function parseLongOption(string $argument, array $longOptions, array &$options, array &$argv) : void {
        $count = count($longOptions);
        $list = explode('=', $argument);
        $option = $list[0];
        $optionArgument = null;
        if (count($list) > 1) {
            $optionArgument = $list[1];
        }
        $optionLength = strlen($option);
        foreach ($longOptions as $i => $longOption) {
            $opt_start = substr($longOption, 0, $optionLength);
            if ($opt_start !== $option) {
                continue;
            }
            $opt_rest = substr($longOption, $optionLength);
            if ($opt_rest !== '' && $i + 1 < $count && $option[0] !== '=' && str_starts_with($longOptions[$i + 1], $option)) {
                throw new AmbiguousOptionException('--' . $option);
            }
            if (str_ends_with($longOption, '=')) {
                if (!str_ends_with($longOption, '==') && !strlen((string) $optionArgument)) {
                    if (false === ($optionArgument = current($argv))) {
                        throw new RequiredOptionArgumentMissingException('--' . $option);
                    }
                    next($argv);
                }
            }
            elseif ($optionArgument) {
                throw new OptionDoesNotAllowArgumentException('--' . $option);
            }
            $fullOption = '--' . preg_replace('/={1,2}$/', '', $longOption);
            $options[] = [
                $fullOption,
                $optionArgument,
            ];
            return;
        }
        throw new UnknownOptionException('--' . $option);
    }

}

Members

Title Sort descending Modifiers Object type Summary
Parser::parse public function @psalm-param list&lt;string&gt; $argv
@psalm-param list&lt;string&gt; $longOptions
Parser::parseLongOption private function @psalm-param list&lt;string&gt; $longOptions
Parser::parseShortOption private function
RSS feed
Powered by Drupal