Skip to content

Commit 4376b1d

Browse files
committed
Document CommandNotFoundMessageProvider
- Relates #793
1 parent 4aecebd commit 4376b1d

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[[using-shell-customization-commandnotfound]]
2+
=== Command Not Found
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
4+
5+
On default a missing command is handled via `CommandNotFoundResultHandler`
6+
and outputs a simple message:
7+
8+
====
9+
[source, text]
10+
----
11+
shell:>missing
12+
No command found for 'missing'
13+
----
14+
====
15+
16+
Internally `CommandNotFoundResultHandler` is using `CommandNotFoundMessageProvider`
17+
which is a simple function taking a `ProviderContext` and returning a text
18+
message. Below is an example what a custom message provider might look like.
19+
20+
====
21+
[source, java, indent=0]
22+
----
23+
include::{snippets}/CommandNotFoundSnippets.java[tag=custom-provider]
24+
----
25+
====
26+
27+
It's possible to change this implementation by defining it as a bean.
28+
29+
====
30+
[source, java, indent=0]
31+
----
32+
include::{snippets}/CommandNotFoundSnippets.java[tag=provider-bean-1]
33+
----
34+
====
35+
36+
`CommandNotFoundResultHandler` is a functional interface so it can
37+
be writter as a lambda.
38+
39+
====
40+
[source, java, indent=0]
41+
----
42+
include::{snippets}/CommandNotFoundSnippets.java[tag=provider-bean-2]
43+
----
44+
====

spring-shell-docs/src/main/asciidoc/using-shell-customization.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ This section describes how you can customize the shell.
66
include::using-shell-customization-styling.adoc[]
77

88
include::using-shell-customization-logging.adoc[]
9+
10+
include::using-shell-customization-commandnotfound.adoc[]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.docs;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.shell.command.CommandRegistration;
23+
import org.springframework.shell.result.CommandNotFoundMessageProvider;
24+
25+
@SuppressWarnings("unused")
26+
class CommandNotFoundSnippets {
27+
28+
// tag::custom-provider[]
29+
class CustomProvider implements CommandNotFoundMessageProvider {
30+
31+
@Override
32+
public String apply(ProviderContext context) {
33+
// parsed commands without options
34+
List<String> commands = context.commands();
35+
// actual error, usually CommandNotFound exception
36+
Throwable error = context.error();
37+
// access to registrations at this time
38+
Map<String, CommandRegistration> registrations = context.registrations();
39+
// raw text input from a user
40+
String text = context.text();
41+
return "My custom message";
42+
}
43+
}
44+
// end::custom-provider[]
45+
46+
class Dump1 {
47+
48+
// tag::provider-bean-1[]
49+
@Bean
50+
CommandNotFoundMessageProvider provider1() {
51+
return new CustomProvider();
52+
}
53+
// end::provider-bean-1[]
54+
55+
// tag::provider-bean-2[]
56+
@Bean
57+
CommandNotFoundMessageProvider provider2() {
58+
return ctx -> "My custom message";
59+
}
60+
// end::provider-bean-2[]
61+
}
62+
63+
}

0 commit comments

Comments
 (0)