public static CliOptions parse(final IPrinter p, final String[] args)
      throws ParseException {
    CliOptions cli = new CliOptions();

    boolean gotCommand = false;
    for (String arg : args) {
      if (!gotCommand && arg.startsWith("-")) {
        while (arg.startsWith("-")) {
          arg = arg.substring(1);
        }
        boolean match = false;
        for (GlobalOptions a : GlobalOptions.values()) {
          if (arg.toUpperCase().startsWith(a.name().toUpperCase())) {
            // Match
            cli.processArgument(p, a, arg.substring(a.name().length()));
            match = true;
            break;
          }
        }
        if (!match) {
          throw new ParseException("Invalid option: " + arg, -1);
        }

      } else {
        if (!gotCommand) {
          String cmdName = arg.toLowerCase();
          ICliCommand cmd = CommandRegistry.instance().command(cmdName);
          if (cmd == null) {
            throw new ParseException("Invalid command: " + arg, -1);
          }
          cli.setCommand(cmdName, cmd);
          gotCommand = true;
        } else {
          cli.addTarget(arg);
        }
      }
    }
    return cli;
  }