Skip to content

Commit 932e9d5

Browse files
committed
Update docs
- Completion docs - Relates #433
1 parent 03a02a0 commit 932e9d5

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
== Completion
2+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
3+
4+
Spring Shell can provide completion proposals for both interactive shell
5+
and a command-line. There are differences however as when shell is in
6+
interactive mode we have an active instance of a shell meaning it's
7+
easier to provide more programmatic ways to provide completion hints.
8+
When shell is purely run as a command-line tool a completion can only
9+
be accomplished with integration into OS level shell's like _bash_.
10+
11+
=== Interactive
12+
13+
Hints for completions are calculated with _function_ or _interface_ style
14+
methods which takes `CompletionContext` and returns a list of
15+
`CompletionProposal` instances. `CompletionContext` gives you various
16+
information about a current context like command registration and option.
17+
18+
NOTE: Generic resolvers can be registered as a beans if those are useful
19+
for all commands and scenarious. For example existing completion
20+
implementation `RegistrationOptionsCompletionResolver` handles completions
21+
for a option names.
22+
23+
====
24+
[source, java, indent=0]
25+
----
26+
include::{snippets}/CompletionSnippets.java[tag=resolver-1]
27+
----
28+
====
29+
30+
Option values with builder based command registration can be
31+
defined per option.
32+
33+
====
34+
[source, java, indent=0]
35+
----
36+
include::{snippets}/CompletionSnippets.java[tag=builder-1]
37+
----
38+
====
39+
40+
Option values with annotation based command registration are handled
41+
via `ValueProvider` interface which can be defined with `@ShellOption`
42+
annotation.
43+
44+
====
45+
[source, java, indent=0]
46+
----
47+
include::{snippets}/CompletionSnippets.java[tag=provider-1]
48+
----
49+
====
50+
51+
Actual `ValueProvider` with annotation based command needs to be
52+
registered as a _Bean_.
53+
54+
====
55+
[source, java, indent=0]
56+
----
57+
include::{snippets}/CompletionSnippets.java[tag=anno-method]
58+
----
59+
====
60+
61+
=== Command-Line
62+
63+
Command-line completion currently only support _bash_ and is documented
64+
in a built-in `completion` command <<built-in-commands-completion>>.

spring-shell-docs/src/main/asciidoc/using-shell-components-builtin.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ a placeholder (`{userconfig}`), which resolves to a common shared config directo
122122

123123
TIP: Run the Spring Shell application to see how the sample application works as it uses these options.
124124

125+
[[built-in-commands-completion]]
125126
==== Completion
126127

127128
The `completion` command set lets you create script files that can be used

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ include::using-shell-commands.adoc[]
44

55
include::using-shell-options.adoc[]
66

7+
include::using-shell-completion.adoc[]
8+
79
include::using-shell-building.adoc[]
810

911
include::using-shell-components.adoc[]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2022 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.Arrays;
19+
import java.util.List;
20+
import java.util.stream.Collectors;
21+
22+
import org.springframework.shell.CompletionContext;
23+
import org.springframework.shell.CompletionProposal;
24+
import org.springframework.shell.command.CommandRegistration;
25+
import org.springframework.shell.completion.CompletionResolver;
26+
import org.springframework.shell.standard.ShellMethod;
27+
import org.springframework.shell.standard.ShellOption;
28+
import org.springframework.shell.standard.ValueProvider;
29+
30+
public class CompletionSnippets {
31+
32+
// tag::builder-1[]
33+
void dump1() {
34+
CommandRegistration.builder()
35+
.withOption()
36+
.longNames("arg1")
37+
.completion(ctx -> {
38+
return Arrays.asList("val1", "val2").stream()
39+
.map(CompletionProposal::new)
40+
.collect(Collectors.toList());
41+
})
42+
.and()
43+
.build();
44+
}
45+
// end::builder-1[]
46+
47+
// tag::resolver-1[]
48+
static class MyValuesCompletionResolver implements CompletionResolver {
49+
50+
@Override
51+
public List<CompletionProposal> apply(CompletionContext t) {
52+
return Arrays.asList("val1", "val2").stream()
53+
.map(CompletionProposal::new)
54+
.collect(Collectors.toList());
55+
}
56+
}
57+
// end::resolver-1[]
58+
59+
// tag::provider-1[]
60+
static class MyValuesProvider implements ValueProvider {
61+
62+
@Override
63+
public List<CompletionProposal> complete(CompletionContext completionContext) {
64+
return Arrays.asList("val1", "val2").stream()
65+
.map(CompletionProposal::new)
66+
.collect(Collectors.toList());
67+
}
68+
}
69+
// end::provider-1[]
70+
71+
static class Dump1 {
72+
// tag::anno-method[]
73+
@ShellMethod(value = "complete", key = "complete")
74+
public String complete(
75+
@ShellOption(valueProvider = MyValuesProvider.class) String arg1)
76+
{
77+
return "You said " + arg1;
78+
}
79+
// end::anno-method[]
80+
}
81+
}

0 commit comments

Comments
 (0)