Option Validation
Spring Shell integrates with the Bean Validation API to support automatic and self-documenting constraints on command parameters.
Annotations found on command parameters are honored and trigger validation prior to the command executing. Consider the following command:
@Command(name = "change-password", description = "Change password", group = "User Management",
help = "A command that changes the user password. Usage: change-password [-p | --password]=<password>")
public String changePassword(
@Option(shortName = 'p', longName = "password", required = true) @Size(min = 8, max = 40) String password) {
// Update the password here. Do not print it to the terminal.
return "Password successfully set";
}
From the preceding example, you get the following behavior for free:
$>change-password --password=hello
The following constraints were not met:
--password: size must be between 8 and 40
Error while executing command change-password: USAGE_ERROR