jline.console.completer.ArgumentCompleter Java Examples

The following examples show how to use jline.console.completer.ArgumentCompleter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CmdlineHelper.java    From clue with Apache License 2.0 7 votes vote down vote up
public CmdlineHelper(Supplier<Collection<String>> commandNameSupplier,
                     Supplier<Collection<String>> fieldNameSupplier) throws IOException {
    consoleReader = new ConsoleReader();
    consoleReader.setBellEnabled(false);

    Collection<String> commands = commandNameSupplier != null
            ? commandNameSupplier.get() : Collections.emptyList();

    Collection<String> fields = fieldNameSupplier != null
            ? fieldNameSupplier.get() : Collections.emptyList();

    LinkedList<Completer> completors = new LinkedList<Completer>();
    completors.add(new StringsCompleter(commands));
    completors.add(new StringsCompleter(fields));
    completors.add(new FileNameCompleter());
    consoleReader.addCompleter(new ArgumentCompleter(completors));
}
 
Example #2
Source File: ConnectDatabaseCommand.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public ArgumentCompleter getArgumentCompleter() {
  return new ArgumentCompleter(
      new StringsCompleter(getCommand()),
      new DbNameCompleter(),
      NullCompleter.INSTANCE);
}
 
Example #3
Source File: ClueCommandClient.java    From clue with Apache License 2.0 5 votes vote down vote up
public void run() throws Exception {
    ConsoleReader consoleReader = new ConsoleReader();
    consoleReader.setBellEnabled(false);

    Collection<String> commands = getCommands();

    LinkedList<Completer> completors = new LinkedList<Completer>();
    completors.add(new StringsCompleter(commands));

    completors.add(new FileNameCompleter());

    consoleReader.addCompleter(new ArgumentCompleter(completors));



    while(true){
        String line = readCommand();
        if (line == null || line.isEmpty()) continue;
        line = line.trim();
        if ("exit".equals(line)) {
            System.exit(0);
        }
        String[] parts = line.split("\\s");
        if (parts.length > 0){
            String cmd = parts[0];
            String[] cmdArgs = new String[parts.length - 1];
            System.arraycopy(parts, 1, cmdArgs, 0, cmdArgs.length);
            handleCommand(cmd, cmdArgs, System.out);
        }
    }
}
 
Example #4
Source File: Terminal.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Terminal(Configuration configuration) {
    try {
        this.configuration = configuration;

        jline.TerminalFactory.configure("auto");

        reader = new ConsoleReader();
        reader.setHistory(new MemoryHistory());
        reader.addCompleter(new ArgumentCompleter(selectCompleters()));
    
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #5
Source File: CommandCompleter.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
public CommandCompleter(Map<String, Command<StratosCommandContext>> commands) {
    if (logger.isDebugEnabled()) {
        logger.debug("Creating auto complete for {} commands", commands.size());
    }
    fileNameCompleter = new StratosFileNameCompleter();
    argumentMap = new HashMap<String, Collection<String>>();
    defaultCommandCompleter = new StringsCompleter(commands.keySet());
    helpCommandCompleter = new ArgumentCompleter(new StringsCompleter(CliConstants.HELP_ACTION),
            defaultCommandCompleter);
    for (String action : commands.keySet()) {

        Command<StratosCommandContext> command = commands.get(action);
        Options commandOptions = command.getOptions();
        if (commandOptions != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Creating argument completer for command: {}", action);
            }
            List<String> arguments = new ArrayList<String>();
            Collection<?> allOptions = commandOptions.getOptions();
            for (Object o : allOptions) {
                Option option = (Option) o;
                String longOpt = option.getLongOpt();
                String opt = option.getOpt();
                if (StringUtils.isNotBlank(longOpt)) {
                    arguments.add("--" + longOpt);
                } else if (StringUtils.isNotBlank(opt)) {
                    arguments.add("-" + opt);
                }
            }

            argumentMap.put(action, arguments);
        }
    }
}
 
Example #6
Source File: TajoCli.java    From tajo with Apache License 2.0 5 votes vote down vote up
private void initCommands() {
  List<Completer> compList = new ArrayList<>();
  for (Class clazz : registeredCommands) {
    TajoShellCommand cmd = null;

    try {
       Constructor cons = clazz.getConstructor(new Class[] {TajoCliContext.class});
       cmd = (TajoShellCommand) cons.newInstance(context);
    } catch (Exception e) {
      System.err.println(e.getMessage());
      throw new RuntimeException(e.getMessage());
    }

    // make completers for console auto-completion
    compList.add(cmd.getArgumentCompleter());

    commands.put(cmd.getCommand(), cmd);
    for (String alias : cmd.getAliases()) {
      commands.put(alias, cmd);
    }
  }

  cliCompleter = new AggregateCompleter(compList);

  sqlCompleter = new ArgumentCompleter(
      new ArgumentCompleter.AbstractArgumentDelimiter() {
        @Override
        public boolean isDelimiterChar(CharSequence buf, int pos) {
          char c = buf.charAt(pos);
          return Character.isWhitespace(c) || !(Character.isLetterOrDigit(c)) && c != '_';
        }
      },
      new StringsCompleter(getKeywords())
  );
}
 
Example #7
Source File: DescFunctionCommand.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public ArgumentCompleter getArgumentCompleter() {
  return new ArgumentCompleter(
      new StringsCompleter(getCommand()),
      new FunctionNameCompleter(),
      NullCompleter.INSTANCE);
}
 
Example #8
Source File: HelpCommand.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public ArgumentCompleter getArgumentCompleter() {
  List<String> cmds = new ArrayList<>(Arrays.asList(getAliases()));
  cmds.add(getCommand());

  return new ArgumentCompleter(
      new StringsCompleter(cmds.toArray(new String[cmds.size()])),
      new StringsCompleter("copyright", "version", "?", "help", "q", "l", "c", "d", "df", "!", "dfs", "admin",
          "set", "unset", "haadmin", "getconf"), // same order as help string
      NullCompleter.INSTANCE);
}
 
Example #9
Source File: PicocliJLineCompleter.java    From picocli with Apache License 2.0 5 votes vote down vote up
/**
 * Populates the specified list with completion candidates for the specified buffer
 * based on the command specification that this shell was constructed with.
 *
 * @param buffer the command line string
 * @param cursor the position of the cursor in the command line string
 * @param candidates the list to populate with completion candidates that would produce
 *                   valid options, parameters or subcommands at the cursor position
 *                   in the command line
 * @return the cursor position in the buffer for which the completion will be relative,
 *          or {@code -1} if no completions are found
 */
//@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
    // use the jline internal parser to split the line into tokens
    ArgumentCompleter.ArgumentList list =
            new ArgumentCompleter.WhitespaceArgumentDelimiter().delimit(buffer, cursor);

    // let picocli generate completion candidates for the token where the cursor is at
    return AutoComplete.complete(spec,
            list.getArguments(),
            list.getCursorArgumentIndex(),
            list.getArgumentPosition(),
            cursor,
            candidates);
}
 
Example #10
Source File: TajoGetConfCommand.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public ArgumentCompleter getArgumentCompleter() {
  TajoConf.ConfVars[] vars = TajoConf.ConfVars.values();
  List<String> confNames = new ArrayList<>();

  for(TajoConf.ConfVars varname: vars) {
    confNames.add(varname.varname);
  }

  return new ArgumentCompleter(
      new StringsCompleter(getCommand()),
      new ConfCompleter(confNames.toArray(new String[confNames.size()])),
      NullCompleter.INSTANCE);
}
 
Example #11
Source File: SetCommand.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public ArgumentCompleter getArgumentCompleter() {
  return new ArgumentCompleter(
      new StringsCompleter(getCommand()),
      new SessionVarCompleter(),
      NullCompleter.INSTANCE);
}
 
Example #12
Source File: DescTableCommand.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public ArgumentCompleter getArgumentCompleter() {
  return new ArgumentCompleter(
      new StringsCompleter(getCommand()),
      new TableNameCompleter(),
      NullCompleter.INSTANCE);
}
 
Example #13
Source File: UnsetCommand.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public ArgumentCompleter getArgumentCompleter() {
  return new ArgumentCompleter(
      new StringsCompleter(getCommand()),
      new SessionVarCompleter(),
      NullCompleter.INSTANCE);
}
 
Example #14
Source File: JLineDevice.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
private ArgumentCompleter.WhitespaceArgumentDelimiter createArgumentDelimiter() {
    return new ArgumentCompleter.WhitespaceArgumentDelimiter() {
        @Override
        public boolean isDelimiterChar(CharSequence buffer, int pos) {
            return super.isDelimiterChar(buffer, pos) || buffer.charAt(pos) == '\'' || buffer.charAt(pos) == '"' ||
                   buffer.charAt(pos) == '{' || buffer.charAt(pos) == '}' || buffer.charAt(pos) == ',' ||
                   buffer.charAt(pos) == ';';
        }
    };
}
 
Example #15
Source File: JLineDevice.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
public void setCommands(CommandSet.Entry[] entries) throws IOException {
    AggregateCompleter aggregateCompleter = new AggregateCompleter(new SimpleCompletor(getCommandsAsArray(entries)),
                                                                   new ClassNameCompletor(),
                                                                   new FileNameCompleter());

    ArgumentCompleter argumentCompleter = new ArgumentCompleter(createArgumentDelimiter(), aggregateCompleter);
    argumentCompleter.setStrict(false);
    reader.addCompleter(argumentCompleter);
}
 
Example #16
Source File: TajoShellCommand.java    From tajo with Apache License 2.0 4 votes vote down vote up
public ArgumentCompleter getArgumentCompleter() {
  List<String> cmds = new ArrayList<>(Arrays.asList(getAliases()));
  cmds.add(getCommand());

  return new ArgumentCompleter(new StringsCompleter(cmds.toArray(new String[cmds.size()])), NullCompleter.INSTANCE);
}
 
Example #17
Source File: CommandCompleter.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {

    if (buffer.contains(CliConstants.RESOURCE_PATH_LONG_OPTION)) {
        return fileNameCompleter.complete(buffer, cursor, candidates);
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Buffer: {}, cursor: {}", buffer, cursor);
        logger.trace("Candidates {}", candidates);
    }
    if (StringUtils.isNotBlank(buffer)) {
        // User is typing a command
        StrTokenizer strTokenizer = new StrTokenizer(buffer);
        String action = strTokenizer.next();
        Collection<String> arguments = argumentMap.get(action);
        if (arguments != null) {
            if (logger.isTraceEnabled()) {
                logger.trace("Arguments found for {}, Tokens: {}", action, strTokenizer.getTokenList());
                logger.trace("Arguments for {}: {}", action, arguments);
            }
            List<String> args = new ArrayList<String>(arguments);
            List<Completer> completers = new ArrayList<Completer>();
            for (String token : strTokenizer.getTokenList()) {
                boolean argContains = arguments.contains(token);
                if (token.startsWith("-") && !argContains) {
                    continue;
                }
                if (argContains) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Removing argument {}", token);
                    }
                    args.remove(token);
                }
                completers.add(new StringsCompleter(token));
            }
            completers.add(new StringsCompleter(args));
            Completer completer = new ArgumentCompleter(completers);
            return completer.complete(buffer, cursor, candidates);
        } else if (CliConstants.HELP_ACTION.equals(action)) {
            // For help action, we need to display available commands as arguments
            return helpCommandCompleter.complete(buffer, cursor, candidates);
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Using Default Completer...");
    }
    return defaultCommandCompleter.complete(buffer, cursor, candidates);
}