public static SuggestionContext flag(Character flag) { return new SuggestionContext(flag, false); }
@Override public String toString() { return forFlag() ? ("-" + getFlag()) : (forHangingValue() ? "hanging" : "last"); }
this.locals = locals != null ? locals : new CommandLocals(); boolean isHanging = false; SuggestionContext suggestionContext = SuggestionContext.hangingValue(); suggestionContext = SuggestionContext.hangingValue(); suggestionContext = SuggestionContext.lastValue(); suggestionContext = SuggestionContext.flag(flagName); break; } else { suggestionContext = SuggestionContext.flag(flagName);
@Override public String toString() { return isArgument() ? "argument " + getIndex() : "flag -" + getFlag(); } }
/** * If the command is being completed from anywhere at or after the given argument index, * generate suggestions for the entire command from that index, based on the given choices. */ public void suggestJoinedArguments(int start, Iterable<String> choices) throws SuggestException { final SuggestionContext ctx = getSuggestionContext(); if(ctx != null && ctx.isArgument()) { if(start == ctx.getIndex()) { ctx.suggestArgument(start, choices); } else if(start < ctx.getIndex()) { final String prefix = String.join(" ", parsedArgs.subList(start, ctx.getIndex())).toLowerCase() + " "; final List<String> filtered = new ArrayList<>(); for(String choice : choices) { if(choice.toLowerCase().startsWith(prefix)) { filtered.add(choice.substring(prefix.length())); } } ctx.suggestArgument(ctx.getIndex(), filtered); } } }
/** * Is the given argument being completed? */ public boolean isArgument(int index) { return isArgument() && getIndex() == index; }
/** * If the given argument is being completed, generate suggestions from the given choices, * by throwing a {@link SuggestException}. */ public void suggestArgument(int index, Iterable<String> choices) throws SuggestException { if(isArgument(index)) { suggest(choices); } }
/** * If the given argument is being completed, generate suggestions based on the given choices. */ public void suggestArgument(int index, Iterable<String> choices) throws SuggestException { if(isSuggesting() && getSuggestionContext().isArgument()) { getSuggestionContext().suggestArgument(index, choices); } }
/** * Return the sorted subset of the given choices that are valid completions, * i.e. that start with {@link #getPrefix()}. */ public List<String> complete(Iterable<String> choices) { return StringUtil.complete(getPrefix(), choices); }
/** * Suggest completions based on the given choices, by throwing a {@link SuggestException}. * * Only valid choices are used, and they are sorted alphabetically. */ public void suggest(Iterable<String> choices) throws SuggestException { throw new SuggestException(complete(choices)); }
/** * Is the given argument being completed? */ public boolean isSuggestingArgument(int index) { return isSuggesting() && getSuggestionContext().isArgument(index); }
context += args[i] + ' '; suggestionContext = new SuggestionContext(context, args[args.length - 1], completingIndex, completingFlag); } else { suggestionContext = null;