Back to Cli class

Method parseArguments

protected void
parseArguments
()
Initialise the options and arguments
Returns
  • void
Since
  • 1.7.0
Deprecated
  • 5.0
Class: Cli
Project: Joomla

Method parseArguments - Source code

/**
 * Initialise the options and arguments
 *
 * Not supported: -abc c-value
 *
 * @return  void
 *
 * @since   1.7.0
 * @deprecated  5.0  Use the `joomla/console` package instead
 */
protected function parseArguments()
{
    $argv = $_SERVER['argv'];
    $this->executable = array_shift($argv);
    $out = array();
    for ($i = 0, $j = \count($argv); $i < $j; $i++) {
        $arg = $argv[$i];
        // --foo --bar=baz
        if (substr($arg, 0, 2) === '--') {
            $eqPos = strpos($arg, '=');
            // --foo
            if ($eqPos === false) {
                $key = substr($arg, 2);
                // --foo value
                if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
                    $value = $argv[$i + 1];
                    $i++;
                } else {
                    $value = $out[$key] ?? true;
                }
                $out[$key] = $value;
            } else {
                $key = substr($arg, 2, $eqPos - 2);
                $value = substr($arg, $eqPos + 1);
                $out[$key] = $value;
            }
        } elseif (substr($arg, 0, 1) === '-') {
            // -k=value -abc
            // -k=value
            if (substr($arg, 2, 1) === '=') {
                $key = substr($arg, 1, 1);
                $value = substr($arg, 3);
                $out[$key] = $value;
            } else {
                // -abc
                $chars = str_split(substr($arg, 1));
                foreach ($chars as $char) {
                    $key = $char;
                    $value = $out[$key] ?? true;
                    $out[$key] = $value;
                }
                // -a a-value
                if (\count($chars) === 1 && $i + 1 < $j && $argv[$i + 1][0] !== '-') {
                    $out[$key] = $argv[$i + 1];
                    $i++;
                }
            }
        } else {
            // Plain-arg
            $this->args[] = $arg;
        }
    }
    $this->data = $out;
}