int initArgs()

in kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java [134:234]


  int initArgs(String[] args) {
    List<String> commands = Collections.emptyList();

    CommandLine cl;
    BeelineParser beelineParser;
    boolean connSuccessful;
    boolean exit;
    DynFields.BoundField<Boolean> exitField;

    try {
      Options options =
          DynFields.builder()
              .hiddenImpl(BeeLine.class, "options")
              .<Options>buildStaticChecked()
              .get();

      beelineParser =
          new BeelineParser() {
            @SuppressWarnings("rawtypes")
            @Override
            protected void processOption(String arg, ListIterator iter) throws ParseException {
              if (PYTHON_MODE_PREFIX.equals(arg)) {
                pythonMode = true;
              } else {
                super.processOption(arg, iter);
              }
            }
          };
      cl = beelineParser.parse(options, args);

      connSuccessful =
          DynMethods.builder("connectUsingArgs")
              .hiddenImpl(BeeLine.class, BeelineParser.class, CommandLine.class)
              .buildChecked(this)
              .invoke(beelineParser, cl);

      exitField = DynFields.builder().hiddenImpl(BeeLine.class, "exit").buildChecked(this);
      exit = exitField.get();

    } catch (ParseException e1) {
      output(e1.getMessage());
      usage();
      return -1;
    } catch (Exception t) {
      error(t.getMessage());
      return 1;
    }

    // checks if default hs2 connection configuration file is present
    // and uses it to connect if found
    // no-op if the file is not present
    if (!connSuccessful && !exit) {
      try {
        connSuccessful =
            DynMethods.builder("defaultBeelineConnect")
                .hiddenImpl(BeeLine.class, CommandLine.class)
                .buildChecked(this)
                .invoke(cl);

      } catch (Exception t) {
        error(t.getMessage());
        return 1;
      }
    }

    // see HIVE-19048 : InitScript errors are ignored
    if (exit) {
      return 1;
    }

    int code = 0;
    if (cl.getOptionValues('e') != null) {
      commands = Arrays.asList(cl.getOptionValues('e'));
      // When using -e, command is always a single line, see HIVE-19018
      getOpts().setAllowMultiLineCommand(false);
    }

    if (!commands.isEmpty() && getOpts().getScriptFile() != null) {
      error("The '-e' and '-f' options cannot be specified simultaneously");
      return 1;
    } else if (!commands.isEmpty() && !connSuccessful) {
      error("Cannot run commands specified using -e. No current connection");
      return 1;
    }
    if (!commands.isEmpty()) {
      for (String command : commands) {
        debug(loc("executing-command", command));
        if (!dispatch(command)) {
          code++;
        }
      }
      try {
        exit = true;
        exitField.set(exit);
      } catch (Exception e) {
        error(e.getMessage());
        return 1;
      }
    }
    return code;
  }