This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Shell 3.3.3! |
Command Not Found
On default a missing command is handled via CommandNotFoundResultHandler
and outputs a simple message:
shell:>missing
No command found for 'missing'
Internally CommandNotFoundResultHandler
is using CommandNotFoundMessageProvider
which is a simple function taking a ProviderContext
and returning a text
message. Below is an example what a custom message provider might look like.
class CustomProvider implements CommandNotFoundMessageProvider {
@Override
public String apply(ProviderContext context) {
// parsed commands without options
List<String> commands = context.commands();
// actual error, usually CommandNotFound exception
Throwable error = context.error();
// access to registrations at this time
Map<String, CommandRegistration> registrations = context.registrations();
// raw text input from a user
String text = context.text();
return "My custom message";
}
}
It’s possible to change this implementation by defining it as a bean.
@Bean
CommandNotFoundMessageProvider provider1() {
return new CustomProvider();
}
CommandNotFoundResultHandler
is a functional interface so it can
be writter as a lambda.
@Bean
CommandNotFoundMessageProvider provider2() {
return ctx -> "My custom message";
}