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

Breadcrumb

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

function Console_Getopt::_parseLongOption

Parse long option

Parameters

string $arg Argument:

string[] $long_options Available long options:

string[][] &$opts:

int &$argIdx:

string[] $args:

Return value

void|PEAR_Error

1 call to Console_Getopt::_parseLongOption()
Console_Getopt::doGetopt in vendor/pear/console_getopt/Console/Getopt.php
The actual implementation of the argument parsing code.

File

vendor/pear/console_getopt/Console/Getopt.php, line 272

Class

Console_Getopt
Command-line options parsing class.

Code

protected static function _parseLongOption($arg, $long_options, &$opts, &$argIdx, $args, $skip_unknown) {
    @(list($opt, $opt_arg) = explode('=', $arg, 2));
    $opt_len = strlen($opt);
    for ($i = 0; $i < count($long_options); $i++) {
        $long_opt = $long_options[$i];
        $opt_start = substr($long_opt, 0, $opt_len);
        $long_opt_name = str_replace('=', '', $long_opt);
        
        /* Option doesn't match. Go on to the next one. */
        if ($long_opt_name != $opt) {
            continue;
        }
        $opt_rest = substr($long_opt, $opt_len);
        
        /* Check that the options uniquely matches one of the allowed
           options. */
        if ($i + 1 < count($long_options)) {
            $next_option_rest = substr($long_options[$i + 1], $opt_len);
        }
        else {
            $next_option_rest = '';
        }
        if ($opt_rest != '' && $opt[0] != '=' && $i + 1 < count($long_options) && $opt == substr($long_options[$i + 1], 0, $opt_len) && $next_option_rest != '' && $next_option_rest[0] != '=') {
            $msg = "Console_Getopt: option --{$opt} is ambiguous";
            return PEAR::raiseError($msg);
        }
        if (substr($long_opt, -1) == '=') {
            if (substr($long_opt, -2) != '==') {
                
                /* Long option requires an argument.
                   Take the next argument if one wasn't specified. */
                if (!strlen($opt_arg)) {
                    if (!isset($args[++$argIdx])) {
                        $msg = "Console_Getopt: option requires an argument --{$opt}";
                        return PEAR::raiseError($msg);
                    }
                    $opt_arg = $args[$argIdx];
                }
                if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) {
                    $msg = "Console_Getopt: option requires an argument --{$opt}";
                    return PEAR::raiseError($msg);
                }
            }
        }
        else {
            if ($opt_arg) {
                $msg = "Console_Getopt: option --{$opt} doesn't allow an argument";
                return PEAR::raiseError($msg);
            }
        }
        $opts[] = array(
            '--' . $opt,
            $opt_arg,
        );
        return;
    }
    if ($skip_unknown === true) {
        return;
    }
    return PEAR::raiseError("Console_Getopt: unrecognized option --{$opt}");
}

API Navigation

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