// There is a single target of this method, so provide completion services for it
MethodTarget methodTarget = targets.iterator().next();
// Identify the command we're working with
CliCommand cmd = methodTarget.getMethod().getAnnotation(CliCommand.class);
Assert.notNull(cmd, "CliCommand unavailable for '" + methodTarget.getMethod().toGenericString() + "'");
// Make a reasonable attempt at parsing the remainingBuffer
Tokenizer tokenizer = null;
try {
tokenizer = new Tokenizer(methodTarget.getRemainingBuffer(), true);
}
catch (TokenizingException e) {
// Make sure we don't crash the main shell loop just
// because the user specified some option twice
return -1;
}
catch (IllegalArgumentException e) {
// Make sure we don't crash the main shell loop just
// because the user specified some option twice
return -1;
}
Map<String, String> options = tokenizer.getTokens();
// Lookup arguments for this target
Annotation[][] parameterAnnotations = methodTarget.getMethod().getParameterAnnotations();
// If there aren't any parameters for the method, at least ensure they have typed the command properly
if (parameterAnnotations.length == 0) {
for (String value : cmd.value()) {
if (buffer.startsWith(value) || value.startsWith(buffer)) {
results.add(new Completion(value)); // no space at the end, as there's no need to continue the
// command further
}
}
candidates.addAll(results);
return 0;
}
// If they haven't specified any parameters yet, at least verify the command name is fully completed
if (options.isEmpty()) {
for (String value : cmd.value()) {
if (value.startsWith(buffer)) {
// They are potentially trying to type this command
// We only need provide completion, though, if they failed to specify it fully
if (!buffer.startsWith(value)) {
// They failed to specify the command fully