Skip to content

Commit 56b9cb2

Browse files
committed
Document String array option type
- Fixes #628
1 parent 25d249c commit 56b9cb2

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,21 @@ include::{snippets}/OptionTypesSnippets.java[tag=option-type-enum-anno]
117117
include::{snippets}/OptionTypesSnippets.java[tag=option-type-enum-reg]
118118
----
119119
====
120+
121+
==== Array
122+
123+
Arrays can be used as is with strings and primitive types.
124+
125+
====
126+
[source, java, indent=0]
127+
----
128+
include::{snippets}/OptionTypesSnippets.java[tag=option-type-string-array-anno]
129+
----
130+
====
131+
132+
====
133+
[source, java, indent=0]
134+
----
135+
include::{snippets}/OptionTypesSnippets.java[tag=option-type-string-array-reg]
136+
----
137+
====

spring-shell-docs/src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,29 @@ void dump() {
154154
}
155155
}
156156

157+
class Dump5 {
158+
// tag::option-type-string-array-anno[]
159+
String example(@ShellOption(value = "arg1") String[] arg1) {
160+
return "Hello " + arg1;
161+
}
162+
// end::option-type-string-array-anno[]
163+
void dump() {
164+
// tag::option-type-string-array-reg[]
165+
CommandRegistration.builder()
166+
.command("example")
167+
.withOption()
168+
.longNames("arg1")
169+
.type(String[].class)
170+
.required()
171+
.and()
172+
.withTarget()
173+
.function(ctx -> {
174+
String[] arg1 = ctx.getOptionValue("arg1");
175+
return "Hello " + arg1;
176+
})
177+
.and()
178+
.build();
179+
// end::option-type-string-array-reg[]
180+
}
181+
}
157182
}

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/BaseE2ECommands.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,11 @@
1515
*/
1616
package org.springframework.shell.samples.e2e;
1717

18+
import java.util.stream.Collectors;
19+
import java.util.stream.IntStream;
20+
21+
import org.springframework.util.StringUtils;
22+
1823
/**
1924
* Base class for all e2e commands.
2025
*
@@ -25,4 +30,26 @@ abstract class BaseE2ECommands {
2530
static final String GROUP = "E2E Commands";
2631
static final String REG = "e2e reg";
2732
static final String LEGACY_ANNO = "e2e anno ";
33+
34+
static String stringOfStrings(String[] values) {
35+
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(values));
36+
}
37+
38+
static String stringOfInts(int[] values) {
39+
String joined = IntStream.range(0, values.length)
40+
.mapToLong(i -> values[i])
41+
.boxed()
42+
.map(d -> d.toString())
43+
.collect(Collectors.joining(","));
44+
return String.format("[%s]", joined);
45+
}
46+
47+
static String stringOfFloats(float[] values) {
48+
String joined = IntStream.range(0, values.length)
49+
.mapToDouble(i -> values[i])
50+
.boxed()
51+
.map(d -> d.toString())
52+
.collect(Collectors.joining(","));
53+
return String.format("[%s]", joined);
54+
}
2855
}

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,65 @@ public CommandRegistration optionTypeEnumRegistration(CommandRegistration.Builde
230230
.build();
231231
}
232232

233+
//
234+
// String[]
235+
//
236+
237+
@ShellMethod(key = LEGACY_ANNO + "option-type-string-array", group = GROUP)
238+
public String optionTypeStringArrayAnnotation(
239+
@ShellOption(help = "Desc arg1") String[] arg1
240+
) {
241+
return "Hello " + stringOfStrings(arg1);
242+
}
243+
244+
@Bean
245+
public CommandRegistration optionTypeStringArrayRegistration(CommandRegistration.BuilderSupplier builder) {
246+
return builder.get()
247+
.command(REG, "option-type-string-array")
248+
.group(GROUP)
249+
.withOption()
250+
.longNames("arg1")
251+
.type(String[].class)
252+
.required()
253+
.and()
254+
.withTarget()
255+
.function(ctx -> {
256+
String[] arg1 = ctx.getOptionValue("arg1");
257+
return "Hello " + stringOfStrings(arg1);
258+
})
259+
.and()
260+
.build();
261+
}
262+
//
263+
// int[]
264+
//
265+
266+
@ShellMethod(key = LEGACY_ANNO + "option-type-int-array", group = GROUP)
267+
public String optionTypeIntArrayAnnotation(
268+
@ShellOption(help = "Desc arg1") int[] arg1
269+
) {
270+
return "Hello " + stringOfInts(arg1);
271+
}
272+
273+
@Bean
274+
public CommandRegistration optionTypeIntArrayRegistration(CommandRegistration.BuilderSupplier builder) {
275+
return builder.get()
276+
.command(REG, "option-type-int-array")
277+
.group(GROUP)
278+
.withOption()
279+
.longNames("arg1")
280+
.type(int[].class)
281+
.required()
282+
.and()
283+
.withTarget()
284+
.function(ctx -> {
285+
int[] arg1 = ctx.getOptionValue("arg1");
286+
return "Hello " + stringOfInts(arg1);
287+
})
288+
.and()
289+
.build();
290+
}
291+
233292
//
234293
// Void
235294
//

0 commit comments

Comments
 (0)