Load options from CLI $argv array

@param array $argv CLI arguments

@return null
327264
public function loadCommandLine(array $argv)
  {
    for ($i=0; $i<sizeof($argv); $i++) {
      $arg = $argv[$i];
      
      if (substr($arg, 0, 2) == "--") {
        $arg = substr($arg, 2);
        
        $parts = explode("=", $arg);
        $name = array_shift($parts);
        $value = implode("=", $parts);
        
        $this->{$name} = $value;
      }
    }
  }
299019
protected function optionFor(string $arg)
    {
        foreach ($this->_options as $option) {
            if ($option->is($arg)) {
                return $option;
            }
        }

        return null;
    }
864184
public function process($arguments = null)
    {
        if ($arguments === null) {
            $arguments = isset($_SERVER['argv']) ? array_slice($_SERVER['argv'], 1) : [];
            $arguments = new Arguments($arguments);
        } elseif (is_array($arguments)) {
            $arguments = new Arguments($arguments);
        } elseif (is_string($arguments)) {
            $arguments = Arguments::fromString($arguments);
        } elseif (!$arguments instanceof Arguments) {
            throw new \InvalidArgumentException(
                '$arguments has to be an instance of Arguments, an arguments string, an array of arguments or null'
            );
        }

        $setOption = function ($name, callable $getValue) {
            $option = $this->getOptionObject($name);

            if (!$option) {
                if (!$this->get(self::SETTING_STRICT_OPTIONS)) {
                    $value = $getValue() ?: 1;
                    if (isset($this->additionalOptions[$name]) &&
                        is_int($value) && is_int($this->additionalOptions[$name])
                    ) {
                        $value += $this->additionalOptions[$name];
                    }

                    $this->additionalOptions[$name] = $value;
                    return;
                } else {
                    throw new Unexpected(sprintf(self::translate('option-unknown'), $name));
                }
            }

            $option->setValue($option->getMode() !== GetOpt::NO_ARGUMENT ? $getValue($option) : null);
        };

        $setCommand = function (Command $command) {
            $this->addOptions($command->getOptions());
            $this->addOperands($command->getOperands());
            $this->command = $command;
        };

        $addOperand = function ($value) {
            $operand = $this->nextOperand();
            if ($operand) {
                $operand->setValue($value);
            } elseif ($this->get(self::SETTING_STRICT_OPERANDS)) {
                throw new Unexpected(sprintf(
                    self::translate('no-more-operands'),
                    $value
                ));
            } else {
                $this->additionalOperands[] = $value;
            }
        };

        $this->additionalOptions  = [];
        $this->additionalOperands = [];
        $this->operandsCount = 0;

        $arguments->process($this, $setOption, $setCommand, $addOperand);

        if (($operand = $this->nextOperand()) && $operand->isRequired() &&
            (!$operand->isMultiple() || count($operand->getValue()) === 0)
        ) {
            throw new Missing(sprintf(self::translate('operand-missing'), $operand->getName()));
        }
    }
553333
public function run(array $argv = null)
    {
        // If no $argv is provided then use the global PHP defined $argv.
        if (is_null($argv)) {
            global $argv;
        }

        // handle call
        $this->handle($argv);

        // exit cli with code 0 or even 1 for error
        $this->shutdown();
    }
997680
public function execute(array $args = [], array $options = [])
    {
        $argv = $_SERVER['argv'];
        array_shift($argv);
        array_shift($argv);

        $argvString = '';
        foreach ($argv as $arg) {
            $argvString .= ($argvString ? ' ' : '').'"'.str_replace('"', '\\"', $arg).'"';
        }
        $found = null;
        foreach ($this->getConfig()->get('_shell_scripts_dirs', []) as $dir) {
            $path = sprintf('%s/%s', $dir, $this->script);
            if (!is_file($path)) {
                continue;
            }
            $found = $path;
        }
        if (null === $found) {
            throw new \RuntimeException(sprintf("Unknown shell script '%s'", $this->script), 412);
        }

        $this->executeShellCommand(sprintf('%s %s', $found, $argvString), $options);
    }
1068838
public function handleCli($argv)
    {
        if (!$this->run) {
            if (!(count($argv) >= 2)) {
                return;
            }

            $route = str_replace(':', '/', $argv[1]);
            unset($argv[0]);

            $this->getRouter()->handle($route);
            $this->callCli(array_values($argv));
            $this->run = true;
        } else {
            throw new BaseException('Application is run.');
        }
    }
190694
private function read_php_argv()
  {
    global $argv;
    if (!is_array($argv))
    {
      if (!@is_array($_SERVER['argv']))
      {
        if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv']))
        {
          throw new pakeException("pakeGetopt: Could not read cmd args (register_argc_argv=Off?).");
        }

        return $GLOBALS['HTTP_SERVER_VARS']['argv'];
      }
      return $_SERVER['argv'];
    }
    return $argv;
  }
210841
public function parseArgv(array $argv = null, $strict = true) {
        if ( ! is_array($argv)) {
            global $argv;
        }

        return $this->parseArgs(array_slice($argv, 1), $strict);
    }
39300
public function parseParams($argv)
    {
        if (is_string($argv)) {
            $argv = gplcart_string_explode_whitespace($argv);
        }

        array_shift($argv);

        $out = array();
        for ($i = 0, $j = count($argv); $i < $j; $i++) {

            $key = null;
            $arg = $argv[$i];

            if (substr($arg, 0, 2) === '--') {

                $pos = strpos($arg, '=');

                if ($pos === false) {
                    $key = substr($arg, 2);
                    if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
                        $value = $argv[$i + 1];
                        $i++;
                    } else {
                        $value = isset($out[$key]) ? $out[$key] : true;
                    }

                    $out[$key] = $value;
                    continue;
                }

                $key = substr($arg, 2, $pos - 2);
                $value = substr($arg, $pos + 1);
                $out[$key] = $value;
                continue;
            }

            if (substr($arg, 0, 1) === '-') {

                if (substr($arg, 2, 1) === '=') {
                    $key = substr($arg, 1, 1);
                    $value = substr($arg, 3);
                    $out[$key] = $value;
                    continue;
                }

                foreach (str_split(substr($arg, 1)) as $char) {
                    $key = $char;
                    $value = isset($out[$key]) ? $out[$key] : true;
                    $out[$key] = $value;
                }

                if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
                    $out[$key] = $argv[$i + 1];
                    $i++;
                }

                continue;
            }

            $value = $arg;
            $out[] = $value;
        }

        return $out;
    }
680562
private function callCli(array $argv)
    {
        if ($this->getRouter()->isMatched()) {
            $cliMethod = 'cliMethod';
            $cliConfig = 'cliConfig';
            $actionNamespace = $this->getRouter()->getActionNamespace();

            $reflection = new ReflectionClass($actionNamespace);
            if (!$reflection->implementsInterface('App\Action\AppAction')) {
                throw new BaseException(
                    sprintf('"%s" action must extend "App\Action\AppAction"', $actionNamespace)
                );
            }

            // Check Action::cliMethod exist or callable
            if (method_exists($actionNamespace, $cliMethod) && is_callable([$actionNamespace, $cliMethod])) {
                $responderNamespace = $this->getRouter()->getResponderNamespace();
                $responder = null;
                
                $reflection = new ReflectionClass($responderNamespace);
                if (class_exists($responderNamespace) &&
                    $reflection->implementsInterface('App\Responder\AppResponder')
                ) {
                    $responder = new $responderNamespace();
                }

                /** @var ContainerAwareInterface|EventSubscriberInterface $actionInstance */
                $actionInstance = new $actionNamespace($responder);
                $actionInstance->setContainer($this->container);
                $this->getEventManager()->addSubscriber($actionInstance);

                $climate = new CLImate();

                if (method_exists($actionNamespace, $cliConfig) && is_callable([$actionNamespace, $cliConfig])) {
                    $argumentManager = new Manager();
                    $climate->setArgumentManager($argumentManager);

                    $this->getEventManager()->dispatch(Action::EVENT_BEFORE_CLI_CONFIG);
                    call_user_func([$actionInstance, 'cliConfig'], $argumentManager);
                    $this->getEventManager()->dispatch(Action::EVENT_AFTER_CLI_CONFIG);

                    try {
                        $argumentManager->parse($argv);
                    } catch (\Exception $e) {
                        $climate->error($e->getMessage());
                        $climate->usage();
                    }
                }

                $this->getEventManager()->dispatch(Action::EVENT_BEFORE_CLI_METHOD);
                call_user_func([$actionInstance, $cliMethod], $climate);
                $this->getEventManager()->dispatch(Action::EVENT_AFTER_CLI_METHOD);

                // Check Responder::cliMethod exist or callable
                if (method_exists($responder, $cliMethod) && is_callable([$responder, $cliMethod])) {
                    $this->getEventManager()->dispatch(self::EVENT_BEFORE_RESPONDER);
                    call_user_func([$responder, $cliMethod]);
                    $this->getEventManager()->dispatch(self::EVENT_AFTER_RESPONDER);
                }
            } else {
                throw new MissingMethodException(
                    sprintf(
                        'Method %s::%s() could not be found, or is not accessible.',
                        $actionNamespace,
                        $cliMethod
                    )
                );
            }
        } else {
            throw new BaseException(sprintf('Route "%s" does not found', $argv[0]));
        }
    }
512186
public function getOptsValues($argv=null) {
    $parsed_args = ArgumentsParser::parseArgvWithSpec($this->resolveArgv($argv), $this->arguments_spec);
    return new ArgumentValues($this->arguments_spec, $parsed_args);
  }
557268
public function get_arg($name, array $argv = null)
    {
        if ($this->climate->arguments->defined($name, $argv)) {
            return $this->climate->arguments->get($name);
        }
        return false;
    }
1125589
public function parse($argv, $override = true)
    {
        $exists = [];
        $override ? $this->_values = $this->_defaults : $exists = array_fill_keys(array_keys($this->_values), true);

        foreach ($argv as $arg) {
            if ($arg === '--') {
                break;
            }
            if ($arg[0] === '-') {
                list($name, $value) = $this->_parse(ltrim($arg, '-'));
                if ($override || !isset($exists[$name])) {
                    $this->add($name, $value);
                }
            }
        }

        return $this->get();
    }
223597
public function dispatch($argv){
        $name = reset($argv);

        if(!$this->hasCommand($name)){
            throw new \InvalidArgumentException('Command not found');
        }

        // load command
        $command = $this->commands[$name];

        // parse arguments
        $arguments = $command->getArguments();
        $arguments->parse($argv);

        // execute command
        $handler = $command->getHandler();
        if(is_array($handler)){
            $class = $handler[0];
//            if($this->container->has($class)){
//                $this->container->add($class);
//            }
            $obj = $this->container->get($class);
            $handler[0] = $obj;
        }
        call_user_func_array($handler, [$arguments]);
    }
575947
protected function validateArguments(array $argv = null)
    {
        $argc = count($argv);

        if (1 == $argc && is_array($argv[0])) {
            return $argv[0];
        }

        if (2 == $argc) {
            if (is_array($argv[0]) && (is_numeric($argv[1]) || is_null($argv[1]) || is_string($argv[1]))) {
                $argv[0][] = $argv[1];

                return $argv[0];
            }

            if ((is_numeric($argv[0]) || is_string($argv[0])) && (is_numeric($argv[1]) || is_string($argv[1]))) {
                return $argv;
            }
        }

        if (3 == $argc) {
            if ((is_numeric($argv[0]) || is_string($argv[0])) && (is_numeric($argv[1]) || is_string($argv[1])) && (is_numeric($argv[2]) || is_null($argv[2]) || is_string($argv[2]))) {
                return $argv;
            }
        }

        array_walk($argv, function (&$value) {
            if (is_array($value)) {
                $value = 'Array';
            } else {
                $value = sprintf('"%s"', $value);
            }
        });

        throw new InvalidValueException(sprintf('Invalid parameters passed to %s::%s: %s', get_class($this), '__construct', implode(', ', $argv)));
    }
1146230
public static void processCommandLine(TextUICommandLine commandLine, String[] argv, IFindBugsEngine findBugs)
            throws IOException, FilterException {
        // Expand option files in command line.
        // An argument beginning with "@" is treated as specifying
        // the name of an option file.
        // Each line of option files are treated as a single argument.
        // Blank lines and comment lines (beginning with "#")
        // are ignored.
        try {
            argv = commandLine.expandOptionFiles(argv, true, true);
        } catch (HelpRequestedException e) {
            showHelp(commandLine);
        }

        int argCount = 0;
        try {
            argCount = commandLine.parse(argv);
        } catch (IllegalArgumentException e) {
            LOG.severe(e.getMessage());
            showHelp(commandLine);
        } catch (HelpRequestedException e) {
            showHelp(commandLine);
        }

        Project project = commandLine.getProject();
        for (int i = argCount; i < argv.length; ++i) {
            project.addFile(argv[i]);
        }
        commandLine.handleXArgs();

        commandLine.configureEngine(findBugs);
        if (commandLine.getProject().getFileCount() == 0 &&
                !commandLine.justPrintConfiguration() && !commandLine.justPrintVersion()) {
            LOG.warning("No files to be analyzed");

            showHelp(commandLine);
        }
    }
433379
function handleswitch($i, $argv)
    {
        $lv = 0;
        $dv = 0.0;
        $sv = $end = $cp = '';
        $j; // int
        $errcnt = 0;
        $cp = strstr($argv[$i], '=');
        if (!$cp) {
            throw new Exception('INTERNAL ERROR: handleswitch passed bad argument, no "=" in arg');
        }
        $argv[$i] = substr($argv[$i], 0, strlen($argv[$i]) - strlen($cp));
        if (!isset(self::$_options[$argv[$i]])) {
            throw new Exception('Command line syntax error: undefined option "' .  $argv[$i] .
                $cp . '"');
        }
        $cp = substr($cp, 1);
        switch (self::$_options[$argv[$i]]['type']) {
        case self::OPT_FLAG:
        case self::OPT_FFLAG:
            throw new Exception('Command line syntax error: option requires an argument "' .
                $argv[$i] . '=' . $cp . '"');
        case self::OPT_DBL:
        case self::OPT_FDBL:
            $dv = (double) $cp;
            break;
        case self::OPT_INT:
        case self::OPT_FINT:
            $lv = (int) $cp;
            break;
        case self::OPT_STR:
        case self::OPT_FSTR:
            $sv = $cp;
            break;
        }
        switch(self::$_options[$argv[$i]]['type']) {
        case self::OPT_FLAG:
        case self::OPT_FFLAG:
            break;
        case self::OPT_DBL:
            $this->{self::$_options[$argv[$i]]['arg']} = $dv;
            break;
        case self::OPT_FDBL:
            $this->{self::$_options[$argv[$i]]['arg']}($dv);
            break;
        case self::OPT_INT:
            $this->{self::$_options[$argv[$i]]['arg']} = $lv;
            break;
        case self::OPT_FINT:
            $this->{self::$_options[$argv[$i]]['arg']}($lv);
            break;
        case self::OPT_STR:
            $this->{self::$_options[$argv[$i]]['arg']} = $sv;
            break;
        case self::OPT_FSTR:
            $this->{self::$_options[$argv[$i]]['arg']}($sv);
            break;
        }
        return 0;
    }