Document String array option type

- Fixes #628
This commit is contained in:
Janne Valkealahti
2023-01-19 11:32:37 +00:00
parent 25d249c28d
commit 56b9cb20ef
4 changed files with 130 additions and 1 deletions

View File

@@ -117,3 +117,21 @@ include::{snippets}/OptionTypesSnippets.java[tag=option-type-enum-anno]
include::{snippets}/OptionTypesSnippets.java[tag=option-type-enum-reg]
----
====
==== Array
Arrays can be used as is with strings and primitive types.
====
[source, java, indent=0]
----
include::{snippets}/OptionTypesSnippets.java[tag=option-type-string-array-anno]
----
====
====
[source, java, indent=0]
----
include::{snippets}/OptionTypesSnippets.java[tag=option-type-string-array-reg]
----
====

View File

@@ -154,4 +154,29 @@ class OptionTypesSnippets {
}
}
class Dump5 {
// tag::option-type-string-array-anno[]
String example(@ShellOption(value = "arg1") String[] arg1) {
return "Hello " + arg1;
}
// end::option-type-string-array-anno[]
void dump() {
// tag::option-type-string-array-reg[]
CommandRegistration.builder()
.command("example")
.withOption()
.longNames("arg1")
.type(String[].class)
.required()
.and()
.withTarget()
.function(ctx -> {
String[] arg1 = ctx.getOptionValue("arg1");
return "Hello " + arg1;
})
.and()
.build();
// end::option-type-string-array-reg[]
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,11 @@
*/
package org.springframework.shell.samples.e2e;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.springframework.util.StringUtils;
/**
* Base class for all e2e commands.
*
@@ -25,4 +30,26 @@ abstract class BaseE2ECommands {
static final String GROUP = "E2E Commands";
static final String REG = "e2e reg";
static final String LEGACY_ANNO = "e2e anno ";
static String stringOfStrings(String[] values) {
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(values));
}
static String stringOfInts(int[] values) {
String joined = IntStream.range(0, values.length)
.mapToLong(i -> values[i])
.boxed()
.map(d -> d.toString())
.collect(Collectors.joining(","));
return String.format("[%s]", joined);
}
static String stringOfFloats(float[] values) {
String joined = IntStream.range(0, values.length)
.mapToDouble(i -> values[i])
.boxed()
.map(d -> d.toString())
.collect(Collectors.joining(","));
return String.format("[%s]", joined);
}
}

View File

@@ -230,6 +230,65 @@ public class OptionTypeCommands extends BaseE2ECommands {
.build();
}
//
// String[]
//
@ShellMethod(key = LEGACY_ANNO + "option-type-string-array", group = GROUP)
public String optionTypeStringArrayAnnotation(
@ShellOption(help = "Desc arg1") String[] arg1
) {
return "Hello " + stringOfStrings(arg1);
}
@Bean
public CommandRegistration optionTypeStringArrayRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type-string-array")
.group(GROUP)
.withOption()
.longNames("arg1")
.type(String[].class)
.required()
.and()
.withTarget()
.function(ctx -> {
String[] arg1 = ctx.getOptionValue("arg1");
return "Hello " + stringOfStrings(arg1);
})
.and()
.build();
}
//
// int[]
//
@ShellMethod(key = LEGACY_ANNO + "option-type-int-array", group = GROUP)
public String optionTypeIntArrayAnnotation(
@ShellOption(help = "Desc arg1") int[] arg1
) {
return "Hello " + stringOfInts(arg1);
}
@Bean
public CommandRegistration optionTypeIntArrayRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type-int-array")
.group(GROUP)
.withOption()
.longNames("arg1")
.type(int[].class)
.required()
.and()
.withTarget()
.function(ctx -> {
int[] arg1 = ctx.getOptionValue("arg1");
return "Hello " + stringOfInts(arg1);
})
.and()
.build();
}
//
// Void
//