Search code examples
spring-shell

Make mandatory one of two options


I have a command like this

@CliCommand("show user")
public String showUser(
        @CliOption(key = {"email"}) String email,
        @CliOption(key = {"id"}) long id) {
    //return user by id or by email
}

I want to make one of the two option mandatory.

show user --id 5 //valid
show user --email user@email.com //valid
show user //not valid
show user id 5 --email user@email.com //not valid

How can I achieve this behavior?


Solution

  • You'll need to handle the validation inside the command implementation itself (and throw some exception if both or none of your options are set).

    The id option parameter should be set to type Long instead of long then.