Skip to content

Cannot define completion only for a particular command option #1246

@czpilar

Description

@czpilar

Consider this configuration:

@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 = "name", shortName = 'n') String name) {
        System.out.println("Hello " + name + "!");
    }

    @Bean
    public CompletionProvider helloNameCompletionProvider() {
        return completionContext -> {
            CommandOption option = completionContext.getCommandOption();
            if (option == null) {
                return Collections.emptyList();
            }
            if ("name".equals(option.longName()) || 'n' == option.shortName()) {
                return Stream.of("Peter", "Paul", "Mary").map(CompletionProposal::new).toList();
            }
            return Collections.emptyList();
        };
    }

}

Step to reproduce:

  1. Type hello
  2. Press Tab and select --name or -n option
  3. Press Tab again and observe that no completion proposal is shown.

This is caused because no command option is provided within completion context so completionContext.getCommandOption() is always null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions