|
16 | 16 | package org.springframework.shell.samples.e2e;
|
17 | 17 |
|
18 | 18 | import java.util.Arrays;
|
19 |
| -import java.util.stream.Collectors; |
20 |
| -import java.util.stream.IntStream; |
21 | 19 |
|
22 | 20 | import org.springframework.context.annotation.Bean;
|
23 | 21 | import org.springframework.shell.command.CommandRegistration;
|
24 | 22 | import org.springframework.shell.command.CommandRegistration.OptionArity;
|
25 | 23 | import org.springframework.shell.standard.ShellComponent;
|
26 | 24 | import org.springframework.shell.standard.ShellMethod;
|
27 | 25 | import org.springframework.shell.standard.ShellOption;
|
| 26 | +import org.springframework.stereotype.Component; |
28 | 27 |
|
29 | 28 | /**
|
30 | 29 | * Commands used for e2e test.
|
31 | 30 | *
|
32 | 31 | * @author Janne Valkealahti
|
33 | 32 | */
|
34 |
| -@ShellComponent |
35 |
| -public class ArityCommands extends BaseE2ECommands { |
| 33 | +public class ArityCommands { |
36 | 34 |
|
37 |
| - @ShellMethod(key = LEGACY_ANNO + "arity-boolean-default-true", group = GROUP) |
38 |
| - public String testArityBooleanDefaultTrueLegacyAnnotation( |
39 |
| - @ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite |
40 |
| - ) { |
41 |
| - return "Hello " + overwrite; |
42 |
| - } |
| 35 | + @ShellComponent |
| 36 | + public static class LegacyAnnotation extends BaseE2ECommands { |
43 | 37 |
|
44 |
| - @Bean |
45 |
| - public CommandRegistration testArityBooleanDefaultTrueRegistration(CommandRegistration.BuilderSupplier builder) { |
46 |
| - return builder.get() |
47 |
| - .command(REG, "arity-boolean-default-true") |
48 |
| - .group(GROUP) |
49 |
| - .withOption() |
50 |
| - .longNames("overwrite") |
51 |
| - .type(Boolean.class) |
52 |
| - .defaultValue("true") |
53 |
| - .arity(OptionArity.ZERO_OR_ONE) |
54 |
| - .and() |
55 |
| - .withTarget() |
56 |
| - .function(ctx -> { |
57 |
| - Boolean overwrite = ctx.getOptionValue("overwrite"); |
58 |
| - return "Hello " + overwrite; |
59 |
| - }) |
60 |
| - .and() |
61 |
| - .build(); |
62 |
| - } |
| 38 | + @ShellMethod(key = LEGACY_ANNO + "arity-boolean-default-true", group = GROUP) |
| 39 | + public String testArityBooleanDefaultTrueLegacyAnnotation( |
| 40 | + @ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite |
| 41 | + ) { |
| 42 | + return "Hello " + overwrite; |
| 43 | + } |
63 | 44 |
|
64 |
| - @ShellMethod(key = LEGACY_ANNO + "arity-string-array", group = GROUP) |
65 |
| - public String testArityStringArrayLegacyAnnotation( |
66 |
| - @ShellOption(value = "--arg1", arity = 3) String[] arg1 |
67 |
| - ) { |
68 |
| - return "Hello " + Arrays.asList(arg1); |
69 |
| - } |
| 45 | + @ShellMethod(key = LEGACY_ANNO + "arity-string-array", group = GROUP) |
| 46 | + public String testArityStringArrayLegacyAnnotation( |
| 47 | + @ShellOption(value = "--arg1", arity = 3) String[] arg1 |
| 48 | + ) { |
| 49 | + return "Hello " + Arrays.asList(arg1); |
| 50 | + } |
70 | 51 |
|
71 |
| - @Bean |
72 |
| - public CommandRegistration testArityStringArrayRegistration(CommandRegistration.BuilderSupplier builder) { |
73 |
| - return builder.get() |
74 |
| - .command(REG, "arity-string-array") |
75 |
| - .group(GROUP) |
76 |
| - .withOption() |
77 |
| - .longNames("arg1") |
78 |
| - .required() |
79 |
| - .type(String[].class) |
80 |
| - .arity(0, 3) |
81 |
| - .position(0) |
82 |
| - .and() |
83 |
| - .withTarget() |
84 |
| - .function(ctx -> { |
85 |
| - String[] arg1 = ctx.getOptionValue("arg1"); |
86 |
| - return "Hello " + Arrays.asList(arg1); |
87 |
| - }) |
88 |
| - .and() |
89 |
| - .build(); |
90 |
| - } |
| 52 | + @ShellMethod(key = LEGACY_ANNO + "arity-float-array", group = GROUP) |
| 53 | + public String testArityFloatArrayLegacyAnnotation( |
| 54 | + @ShellOption(value = "--arg1", arity = 3) float[] arg1 |
| 55 | + ) { |
| 56 | + return "Hello " + stringOfFloats(arg1); |
| 57 | + } |
91 | 58 |
|
92 |
| - @ShellMethod(key = LEGACY_ANNO + "arity-float-array", group = GROUP) |
93 |
| - public String testArityFloatArrayLegacyAnnotation( |
94 |
| - @ShellOption(value = "--arg1", arity = 3) float[] arg1 |
95 |
| - ) { |
96 |
| - return "Hello " + floatsToString(arg1); |
97 | 59 | }
|
98 | 60 |
|
99 |
| - @Bean |
100 |
| - public CommandRegistration testArityFloatArrayRegistration(CommandRegistration.BuilderSupplier builder) { |
101 |
| - return builder.get() |
102 |
| - .command(REG, "arity-float-array") |
103 |
| - .group(GROUP) |
104 |
| - .withOption() |
105 |
| - .longNames("arg1") |
106 |
| - .type(float[].class) |
107 |
| - .arity(0, 3) |
108 |
| - .and() |
109 |
| - .withTarget() |
110 |
| - .function(ctx -> { |
111 |
| - float[] arg1 = ctx.getOptionValue("arg1"); |
112 |
| - return "Hello " + floatsToString(arg1); |
113 |
| - }) |
114 |
| - .and() |
115 |
| - .build(); |
116 |
| - } |
| 61 | + @Component |
| 62 | + public static class Registration extends BaseE2ECommands { |
117 | 63 |
|
118 |
| - @Bean |
119 |
| - public CommandRegistration testArityErrorsRegistration(CommandRegistration.BuilderSupplier builder) { |
120 |
| - return builder.get() |
121 |
| - .command(REG, "arity-errors") |
122 |
| - .group(GROUP) |
123 |
| - .withOption() |
124 |
| - .longNames("arg1") |
125 |
| - .type(String[].class) |
126 |
| - .required() |
127 |
| - .arity(1, 2) |
128 |
| - .and() |
129 |
| - .withTarget() |
130 |
| - .function(ctx -> { |
131 |
| - String[] arg1 = ctx.getOptionValue("arg1"); |
132 |
| - return "Hello " + Arrays.asList(arg1); |
133 |
| - }) |
134 |
| - .and() |
135 |
| - .build(); |
136 |
| - } |
| 64 | + @Bean |
| 65 | + public CommandRegistration testArityBooleanDefaultTrueRegistration() { |
| 66 | + return getBuilder() |
| 67 | + .command(REG, "arity-boolean-default-true") |
| 68 | + .group(GROUP) |
| 69 | + .withOption() |
| 70 | + .longNames("overwrite") |
| 71 | + .type(Boolean.class) |
| 72 | + .defaultValue("true") |
| 73 | + .arity(OptionArity.ZERO_OR_ONE) |
| 74 | + .and() |
| 75 | + .withTarget() |
| 76 | + .function(ctx -> { |
| 77 | + Boolean overwrite = ctx.getOptionValue("overwrite"); |
| 78 | + return "Hello " + overwrite; |
| 79 | + }) |
| 80 | + .and() |
| 81 | + .build(); |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + public CommandRegistration testArityStringArrayRegistration() { |
| 86 | + return getBuilder() |
| 87 | + .command(REG, "arity-string-array") |
| 88 | + .group(GROUP) |
| 89 | + .withOption() |
| 90 | + .longNames("arg1") |
| 91 | + .required() |
| 92 | + .type(String[].class) |
| 93 | + .arity(0, 3) |
| 94 | + .position(0) |
| 95 | + .and() |
| 96 | + .withTarget() |
| 97 | + .function(ctx -> { |
| 98 | + String[] arg1 = ctx.getOptionValue("arg1"); |
| 99 | + return "Hello " + Arrays.asList(arg1); |
| 100 | + }) |
| 101 | + .and() |
| 102 | + .build(); |
| 103 | + } |
| 104 | + |
| 105 | + @Bean |
| 106 | + public CommandRegistration testArityFloatArrayRegistration() { |
| 107 | + return getBuilder() |
| 108 | + .command(REG, "arity-float-array") |
| 109 | + .group(GROUP) |
| 110 | + .withOption() |
| 111 | + .longNames("arg1") |
| 112 | + .type(float[].class) |
| 113 | + .arity(0, 3) |
| 114 | + .and() |
| 115 | + .withTarget() |
| 116 | + .function(ctx -> { |
| 117 | + float[] arg1 = ctx.getOptionValue("arg1"); |
| 118 | + return "Hello " + stringOfFloats(arg1); |
| 119 | + }) |
| 120 | + .and() |
| 121 | + .build(); |
| 122 | + } |
137 | 123 |
|
138 |
| - private static String floatsToString(float[] arg1) { |
139 |
| - return IntStream.range(0, arg1.length) |
140 |
| - .mapToDouble(i -> arg1[i]) |
141 |
| - .boxed() |
142 |
| - .map(d -> d.toString()) |
143 |
| - .collect(Collectors.joining(",")); |
| 124 | + @Bean |
| 125 | + public CommandRegistration testArityErrorsRegistration() { |
| 126 | + return getBuilder() |
| 127 | + .command(REG, "arity-errors") |
| 128 | + .group(GROUP) |
| 129 | + .withOption() |
| 130 | + .longNames("arg1") |
| 131 | + .type(String[].class) |
| 132 | + .required() |
| 133 | + .arity(1, 2) |
| 134 | + .and() |
| 135 | + .withTarget() |
| 136 | + .function(ctx -> { |
| 137 | + String[] arg1 = ctx.getOptionValue("arg1"); |
| 138 | + return "Hello " + Arrays.asList(arg1); |
| 139 | + }) |
| 140 | + .and() |
| 141 | + .build(); |
| 142 | + } |
144 | 143 | }
|
145 | 144 | }
|
0 commit comments