public static AddResourceCommand parse()

in odps-console-resource/src/main/java/com/aliyun/openservices/odps/console/resource/CreateResourceCommand.java [56:153]


  public static AddResourceCommand parse(String commandString, ExecutionContext sessionContext)
      throws ODPSConsoleException {

    String[] tokens = new AntlrObject(commandString).getTokenStringArray();

    if (tokens != null && tokens.length >= 2 &&
        tokens[0].toUpperCase().equals("CREATE") && tokens[1].toUpperCase().equals("RESOURCE")) {

      GnuParser parser = new GnuParser();
      Options options = new Options();
      options.addOption("p", "project", true, null);
      options.addOption("c", "comment", true, null);
      options.addOption("f", "force", false, null);

      try {
        CommandLine cl = parser.parse(options, tokens);

        String refName = null;
        String alias = "";
        String comment = null;
        String type = null;
        String partitionSpec = "";
        boolean isUpdate = false;

        List<String> argList = cl.getArgList();
        int size = argList.size();

        if (size < 4) {
          throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Missing parameters");
        }

        ListIterator<String> iter = argList.listIterator();
        iter.next();
        iter.next();
        type = iter.next();
        refName = iter.next();
        if (iter.hasNext()) {
          String item = iter.next();
          if (item.equals("(")) {
            boolean isParenPaired = false;

            while (iter.hasNext()) {
              String s = iter.next();
              if (s.equals(")")) {
                isParenPaired = true;
                break;
              }
              partitionSpec += s;
            }

            if (!isParenPaired) {
              throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Unpaired parenthesis");
            }

            if (!iter.hasNext()) {
              throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Missing parameter: alias");
            }
            item = iter.next();
          }

          alias = item;
        }

        if (iter.hasNext()) {
          throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Illegal parameter: " + iter.next());
        }

        String projectName = null;
        Option[] opts = cl.getOptions();
        for (Option opt : opts) {
          if ("f".equals(opt.getOpt())) {
            isUpdate = true;
          } else if ("c".equals(opt.getOpt())) {
            comment = opt.getValue();
          } else if ("p".equals(opt.getOpt())) {
            projectName = opt.getValue();
          } else {
            throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Illegal option: " + opt.getOpt());
          }
        }

        return new AddResourceCommand(
            commandString,
            sessionContext,
            refName,
            alias,
            comment,
            type,
            partitionSpec,
            isUpdate,
            projectName);
      } catch (ParseException e) {
        throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Invalid parameters");
      }
    } else {
      return null;
    }
  }