get_cli_args

The timeline below displays how wordpress function get_cli_args has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.6

/**
 * Returns value of command line params.
 * Exits when a required param is not set.
 *
 * @param string $param
 * @param bool   $required
 * @return mixed
 */
function get_cli_args($param, $required = false)
{
    $args = $_SERVER['argv'];
    if (!is_array($args)) {
        $args = array();
    }
    $out = array();
    $last_arg = null;
    $return = null;
    $il = count($args);
    for ($i = 1, $il; $i < $il; $i++) {
        if ((bool) preg_match('/^--(.+)/', $args[$i], $match)) {
            $parts = explode('=', $match[1]);
            $key = preg_replace('/[^a-z0-9]+/', '', $parts[0]);
            if (isset($parts[1])) {
                $out[$key] = $parts[1];
            } else {
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ((bool) preg_match('/^-([a-zA-Z0-9]+)/', $args[$i], $match)) {
            for ($j = 0, $jl = strlen($match[1]); $j < $jl; $j++) {
                $key = $match[1][$j];
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif (null !== $last_arg) {
            $out[$last_arg] = $args[$i];
        }
    }
    // Check array for specified param.
    if (isset($out[$param])) {
        // Set return value.
        $return = $out[$param];
    }
    // Check for missing required param.
    if (!isset($out[$param]) && $required) {
        // Display message and exit.
        echo "\"{$param}\" parameter is required but was not specified\n";
        exit;
    }
    return $return;
}

WordPress Version: 5.4

/**
 * Returns value of command line params.
 * Exits when a required param is not set.
 *
 * @param string $param
 * @param bool   $required
 * @return mixed
 */
function get_cli_args($param, $required = false)
{
    $args = $_SERVER['argv'];
    if (!is_array($args)) {
        $args = array();
    }
    $out = array();
    $last_arg = null;
    $return = null;
    $il = sizeof($args);
    for ($i = 1, $il; $i < $il; $i++) {
        if ((bool) preg_match('/^--(.+)/', $args[$i], $match)) {
            $parts = explode('=', $match[1]);
            $key = preg_replace('/[^a-z0-9]+/', '', $parts[0]);
            if (isset($parts[1])) {
                $out[$key] = $parts[1];
            } else {
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ((bool) preg_match('/^-([a-zA-Z0-9]+)/', $args[$i], $match)) {
            for ($j = 0, $jl = strlen($match[1]); $j < $jl; $j++) {
                $key = $match[1][$j];
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif (null !== $last_arg) {
            $out[$last_arg] = $args[$i];
        }
    }
    // Check array for specified param.
    if (isset($out[$param])) {
        // Set return value.
        $return = $out[$param];
    }
    // Check for missing required param.
    if (!isset($out[$param]) && $required) {
        // Display message and exit.
        echo "\"{$param}\" parameter is required but was not specified\n";
        exit;
    }
    return $return;
}

WordPress Version: 5.1

/**
 * Returns value of command line params.
 * Exits when a required param is not set.
 *
 * @param string $param
 * @param bool   $required
 * @return mixed
 */
function get_cli_args($param, $required = false)
{
    $args = $_SERVER['argv'];
    if (!is_array($args)) {
        $args = array();
    }
    $out = array();
    $last_arg = null;
    $return = null;
    $il = sizeof($args);
    for ($i = 1, $il; $i < $il; $i++) {
        if ((bool) preg_match('/^--(.+)/', $args[$i], $match)) {
            $parts = explode('=', $match[1]);
            $key = preg_replace('/[^a-z0-9]+/', '', $parts[0]);
            if (isset($parts[1])) {
                $out[$key] = $parts[1];
            } else {
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ((bool) preg_match('/^-([a-zA-Z0-9]+)/', $args[$i], $match)) {
            for ($j = 0, $jl = strlen($match[1]); $j < $jl; $j++) {
                $key = $match[1][$j];
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ($last_arg !== null) {
            $out[$last_arg] = $args[$i];
        }
    }
    // Check array for specified param
    if (isset($out[$param])) {
        // Set return value
        $return = $out[$param];
    }
    // Check for missing required param
    if (!isset($out[$param]) && $required) {
        // Display message and exit
        echo "\"{$param}\" parameter is required but was not specified\n";
        exit;
    }
    return $return;
}

WordPress Version: 4.3

/**
 * Returns value of command line params.
 * Exits when a required param is not set.
 *
 * @param string $param
 * @param bool   $required
 * @return mixed
 */
function get_cli_args($param, $required = false)
{
    $args = $_SERVER['argv'];
    $out = array();
    $last_arg = null;
    $return = null;
    $il = sizeof($args);
    for ($i = 1, $il; $i < $il; $i++) {
        if ((bool) preg_match("/^--(.+)/", $args[$i], $match)) {
            $parts = explode("=", $match[1]);
            $key = preg_replace("/[^a-z0-9]+/", "", $parts[0]);
            if (isset($parts[1])) {
                $out[$key] = $parts[1];
            } else {
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ((bool) preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match)) {
            for ($j = 0, $jl = strlen($match[1]); $j < $jl; $j++) {
                $key = $match[1][$j];
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ($last_arg !== null) {
            $out[$last_arg] = $args[$i];
        }
    }
    // Check array for specified param
    if (isset($out[$param])) {
        // Set return value
        $return = $out[$param];
    }
    // Check for missing required param
    if (!isset($out[$param]) && $required) {
        // Display message and exit
        echo "\"{$param}\" parameter is required but was not specified\n";
        exit;
    }
    return $return;
}

WordPress Version: 4.2

/**
 * Returns value of command line params.
 * Exits when a required param is not set.
 *
 * @param string $param
 * @param bool $required
 * @return mixed
 */
function get_cli_args($param, $required = false)
{
    $args = $_SERVER['argv'];
    $out = array();
    $last_arg = null;
    $return = null;
    $il = sizeof($args);
    for ($i = 1, $il; $i < $il; $i++) {
        if ((bool) preg_match("/^--(.+)/", $args[$i], $match)) {
            $parts = explode("=", $match[1]);
            $key = preg_replace("/[^a-z0-9]+/", "", $parts[0]);
            if (isset($parts[1])) {
                $out[$key] = $parts[1];
            } else {
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ((bool) preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match)) {
            for ($j = 0, $jl = strlen($match[1]); $j < $jl; $j++) {
                $key = $match[1][$j];
                $out[$key] = true;
            }
            $last_arg = $key;
        } elseif ($last_arg !== null) {
            $out[$last_arg] = $args[$i];
        }
    }
    // Check array for specified param
    if (isset($out[$param])) {
        // Set return value
        $return = $out[$param];
    }
    // Check for missing required param
    if (!isset($out[$param]) && $required) {
        // Display message and exit
        echo "\"{$param}\" parameter is required but was not specified\n";
        exit;
    }
    return $return;
}

WordPress Version: 3.7

/**
 * Returns value of command line params.
 * Exits when a required param is not set.
 *
 * @param string $param
 * @param bool $required
 * @return mixed
 */
function get_cli_args($param, $required = false)
{
    $args = $_SERVER['argv'];
    $out = array();
    $last_arg = null;
    $return = null;
    $il = sizeof($args);
    for ($i = 1, $il; $i < $il; $i++) {
        if ((bool) preg_match("/^--(.+)/", $args[$i], $match)) {
            $parts = explode("=", $match[1]);
            $key = preg_replace("/[^a-z0-9]+/", "", $parts[0]);
            if (isset($parts[1])) {
                $out[$key] = $parts[1];
            } else {
                $out[$key] = true;
            }
            $last_arg = $key;
        } else if ((bool) preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match)) {
            for ($j = 0, $jl = strlen($match[1]); $j < $jl; $j++) {
                $key = $match[1][$j];
                $out[$key] = true;
            }
            $last_arg = $key;
        } else if ($last_arg !== null) {
            $out[$last_arg] = $args[$i];
        }
    }
    // Check array for specified param
    if (isset($out[$param])) {
        // Set return value
        $return = $out[$param];
    }
    // Check for missing required param
    if (!isset($out[$param]) && $required) {
        // Display message and exit
        echo "\"{$param}\" parameter is required but was not specified\n";
        exit;
    }
    return $return;
}