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

Breadcrumb

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

function Console_Getopt::doGetopt

The actual implementation of the argument parsing code.

Parameters

int $version Version to use:

array $args an array of command-line arguments:

string $short_options specifies the list of allowed short options:

array $long_options specifies the list of allowed long options:

boolean $skip_unknown suppresses Console_Getopt: unrecognized option:

Return value

array

2 calls to Console_Getopt::doGetopt()
Console_Getopt::getopt in vendor/pear/console_getopt/Console/Getopt.php
This function expects $args to start with the script name (POSIX-style). Preserved for backwards compatibility.
Console_Getopt::getopt2 in vendor/pear/console_getopt/Console/Getopt.php
Parses the command-line options.

File

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

Class

Console_Getopt
Command-line options parsing class.

Code

public static function doGetopt($version, $args, $short_options, $long_options = null, $skip_unknown = false) {
    // in case you pass directly readPHPArgv() as the first arg
    if (PEAR::isError($args)) {
        return $args;
    }
    if (empty($args)) {
        return array(
            array(),
            array(),
        );
    }
    $non_opts = $opts = array();
    settype($args, 'array');
    if ($long_options) {
        sort($long_options);
    }
    
    /*
     * Preserve backwards compatibility with callers that relied on
     * erroneous POSIX fix.
     */
    if ($version < 2) {
        if (isset($args[0][0]) && $args[0][0] != '-') {
            array_shift($args);
        }
    }
    for ($i = 0; $i < count($args); $i++) {
        $arg = $args[$i];
        
        /* The special element '--' means explicit end of
           options. Treat the rest of the arguments as non-options
           and end the loop. */
        if ($arg == '--') {
            $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
            break;
        }
        if ($arg[0] != '-' || strlen($arg) > 1 && $arg[1] == '-' && !$long_options) {
            $non_opts = array_merge($non_opts, array_slice($args, $i));
            break;
        }
        elseif (strlen($arg) > 1 && $arg[1] == '-') {
            $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $i, $args, $skip_unknown);
            if (PEAR::isError($error)) {
                return $error;
            }
        }
        elseif ($arg == '-') {
            // - is stdin
            $non_opts = array_merge($non_opts, array_slice($args, $i));
            break;
        }
        else {
            $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $i, $args, $skip_unknown);
            if (PEAR::isError($error)) {
                return $error;
            }
        }
    }
    return array(
        $opts,
        $non_opts,
    );
}

API Navigation

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