code: public void parseAndExecuteCommand() {
      CommandLineParser parser = new DefaultParser();
      try {
        CommandLine parsedOpts = parser.parse(this.options, this.args, true);
        GlobalOptions globalOptions = createGlobalOptions(parsedOpts);
  
        // Fetch the command and fail if there is ambiguity
        String[] remainingArgs = parsedOpts.getArgs();
        if (remainingArgs.length == 0) {
          printHelpAndExit("Command not specified!");
        }
  
        String commandName = remainingArgs[0].toLowerCase();
        remainingArgs = remainingArgs.length > 1 ?
              Arrays.copyOfRange(remainingArgs, 1, remainingArgs.length) :
              new String[]{};
  
        Command command = commandList.get(commandName);
        if (command == null) {
          System.out.println("Command " + commandName + " not known.");
          printHelpAndExit();
        } else {
          command.execute(globalOptions, remainingArgs);
        }
      } catch (ParseException e) {
        printHelpAndExit("Ran into an error parsing args.");
      }
    }
docs: Parse and execute the appropriate command based on the args.
  The general flow looks like this:
  
  1. Parse a set of global options (eg host/port for the admin server)
  2. Parse out the command name
  3. Pass the global options and any left over parameters to a command handler