-
Notifications
You must be signed in to change notification settings - Fork 394
Improved completion for a specific command, including support for resolving key=value pairs in the command line #1249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…olving key=value pairs in the command line. Improved spring-projects#1246 Signed-off-by: czpilar <david@czpilar.net>
|
I’ve just realized that after fixing the completion issue #1246, I completely missed the case where So if you already have Consider the following example: package org.springframework.shell.samples.helloworld.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.core.command.CommandOption;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.shell.core.command.completion.CompletionProposal;
import org.springframework.shell.core.command.completion.CompletionProvider;
import java.util.Collections;
import java.util.stream.Stream;
@SpringBootApplication
public class SpringShellApplication {
public static void main(String[] args) {
SpringApplication.run(SpringShellApplication.class, args);
}
@Command(name = "hello", completionProvider = "helloNameCompletionProvider")
public void sayHello(@Option(longName = "first", shortName = 'f') String first,
@Option(longName = "last", shortName = 'l') String last) {
System.out.println("Hello " + first + " " + last + "!");
}
@Bean
public CompletionProvider helloNameCompletionProvider() {
return completionContext -> {
CommandOption option = completionContext.getCommandOption();
if (option == null) {
return Collections.emptyList();
}
Stream<String> options = Stream.empty();
if ("first".equals(option.longName())) {
options = Stream.of("Peter", "Paul", "Mary");
}
else if ("last".equals(option.longName())) {
options = Stream.of("Chan", "Noris");
}
return options.map(CompletionProposal::new).toList();
};
}
}If you also want completion for the Consider the following example: package org.springframework.shell.samples.helloworld.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.core.command.CommandOption;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.shell.core.command.completion.CompletionProposal;
import org.springframework.shell.core.command.completion.CompletionProvider;
import java.util.Collections;
import java.util.stream.Stream;
@SpringBootApplication
public class SpringShellApplication {
public static void main(String[] args) {
SpringApplication.run(SpringShellApplication.class, args);
}
@Command(name = "hello", completionProvider = "helloNameCompletionProvider")
public void sayHello(@Option(longName = "first", shortName = 'f') String first,
@Option(longName = "last", shortName = 'l') String last) {
System.out.println("Hello " + first + " " + last + "!");
}
@Bean
public CompletionProvider helloNameCompletionProvider() {
return completionContext -> {
CommandOption option = completionContext.getCommandOption();
if (option == null) {
return Collections.emptyList();
}
String word = completionContext.getWords().get(completionContext.getWords().size() - 1);
if (word.contains("=")) {
word = word.substring(0, word.indexOf('='));
}
String prefix = word.isEmpty() ? word : word + "=";
Stream<String> options = Stream.empty();
if ("first".equals(option.longName())) {
options = Stream.of("Peter", "Paul", "Mary");
}
else if ("last".equals(option.longName())) {
options = Stream.of("Chan", "Noris");
}
return options.map(str -> prefix + str).map(CompletionProposal::new).toList();
};
}
}@fmbenhassine Also, it’s great that you improved the default command parser in #1248 - it now works very well with completion. |
3cf436a to
e1964aa
Compare
|
Added tests for Command Completer. |
Signed-off-by: czpilar <david@czpilar.net>
e1964aa to
af0e0ff
Compare
|
Added more tests. |
Signed-off-by: czpilar <david@czpilar.net>
36eb7c7 to
2a158b3
Compare
Signed-off-by: czpilar <david@czpilar.net>
|
Added one more test for command with subcommand |
Signed-off-by: czpilar <david@czpilar.net>
c1b7a74 to
ab14f94
Compare
Signed-off-by: czpilar <david@czpilar.net>
|
Added one more tests for completion with two options where one is subset of other |
|
That's great! I am glad things are getting improved continuously. Thank you very much for your PR 👍 |
Thank you for sharing this example! Very useful for those who want to provide completion values with the Rebased, squashed and merged as 8960cf4. Thank you for your contribution 🙏 |
@fmbenhassine I can take a look at this. You can assign #285 to me... I will look at this later today or tomorrow. |
Improved #1246