public static void main(String[] args) { try { ConsoleReader reader = new ConsoleReader(); reader.setPrompt("prompt> "); // set up the completion CliCommands commands = new CliCommands(reader); CommandLine cmd = new CommandLine(commands); reader.addCompleter(new PicocliJLineCompleter(cmd.getCommandSpec())); // start the shell and process input until the user quits with Ctl-D String line; while ((line = reader.readLine()) != null) { ArgumentList list = new WhitespaceArgumentDelimiter().delimit(line, line.length()); CommandLine.run(commands, list.getArguments()); } } catch (Throwable t) { t.printStackTrace(); } } }
String[] args = list.getArguments(); String arg = (args == null || i >= args.length) ? "" : args[i];
String[] args = list.getArguments(); String arg = (args == null || i >= args.length) ? "" : args[i];
public String[] parseArguments(String line) { ArgumentList delimit = delimit(line, 0); return cleanArguments(delimit.getArguments()); }
public String[] parseArguments(String line) { ArgumentList delimit = delimit(line, 0); return cleanArguments(delimit.getArguments()); }
/** * 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); } }
@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(commandSpec, list.getArguments(), list.getCursorArgumentIndex(), list.getArgumentPosition(), cursor, candidates); } }