Skip to content

Commit c384fbd

Browse files
committed
Polish 'Support both kebab-case and camelCase as Spring init CLI Options'
Refine the command so that camelCase options are supported but not advertised. See gh-28138
1 parent ad34732 commit c384fbd

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.Collection;
23+
import java.util.Collections;
24+
import java.util.HashMap;
2325
import java.util.List;
26+
import java.util.Map;
2427

2528
import joptsimple.OptionSet;
2629
import joptsimple.OptionSpec;
@@ -72,6 +75,21 @@ public Collection<HelpExample> getExamples() {
7275
*/
7376
static class InitOptionHandler extends OptionHandler {
7477

78+
/**
79+
* Mapping from camelCase options advertised by the service to our kebab-case
80+
* options.
81+
*/
82+
private static final Map<String, String> CAMEL_CASE_OPTIONS;
83+
static {
84+
Map<String, String> options = new HashMap<>();
85+
options.put("--groupId", "--group-id");
86+
options.put("--artifactId", "--artifact-id");
87+
options.put("--packageName", "--package-name");
88+
options.put("--javaVersion", "--java-version");
89+
options.put("--bootVersion", "--boot-version");
90+
CAMEL_CASE_OPTIONS = Collections.unmodifiableMap(options);
91+
}
92+
7593
private final ServiceCapabilitiesReportGenerator serviceCapabilitiesReport;
7694

7795
private final ProjectGenerator projectGenerator;
@@ -113,9 +131,9 @@ static class InitOptionHandler extends OptionHandler {
113131
private OptionSpec<Void> force;
114132

115133
InitOptionHandler(InitializrService initializrService) {
134+
super(InitOptionHandler::processArgument);
116135
this.serviceCapabilitiesReport = new ServiceCapabilitiesReportGenerator(initializrService);
117136
this.projectGenerator = new ProjectGenerator(initializrService);
118-
119137
}
120138

121139
@Override
@@ -129,20 +147,16 @@ protected void options() {
129147
otherOptions();
130148
}
131149

132-
/**
133-
* Supports both kebab-case and camelCase as project CLI Options. camelCase to be
134-
* deprecated as part of future releases
135-
*/
136150
private void projectGenerationOptions() {
137-
this.groupId = option(Arrays.asList("groupId", "group-id", "g"),
138-
"Project coordinates (for example 'org.test')").withRequiredArg();
139-
this.artifactId = option(Arrays.asList("artifactId", "artifact-id", "a"),
151+
this.groupId = option(Arrays.asList("group-id", "g"), "Project coordinates (for example 'org.test')")
152+
.withRequiredArg();
153+
this.artifactId = option(Arrays.asList("artifact-id", "a"),
140154
"Project coordinates; infer archive name (for example 'test')").withRequiredArg();
141155
this.version = option(Arrays.asList("version", "v"), "Project version (for example '0.0.1-SNAPSHOT')")
142156
.withRequiredArg();
143157
this.name = option(Arrays.asList("name", "n"), "Project name; infer application name").withRequiredArg();
144158
this.description = option("description", "Project description").withRequiredArg();
145-
this.packageName = option(Arrays.asList("packageName", "package-name"), "Package name").withRequiredArg();
159+
this.packageName = option(Arrays.asList("package-name"), "Package name").withRequiredArg();
146160
this.type = option(Arrays.asList("type", "t"),
147161
"Project type. Not normally needed if you use --build "
148162
+ "and/or --format. Check the capabilities of the service (--list) for more details")
@@ -153,11 +167,11 @@ private void projectGenerationOptions() {
153167
.defaultsTo("maven");
154168
this.format = option("format", "Format of the generated content (for example 'build' for a build file, "
155169
+ "'project' for a project archive)").withRequiredArg().defaultsTo("project");
156-
this.javaVersion = option(Arrays.asList("javaVersion", "java-version", "j"),
157-
"Language level (for example '1.8')").withRequiredArg();
170+
this.javaVersion = option(Arrays.asList("java-version", "j"), "Language level (for example '1.8')")
171+
.withRequiredArg();
158172
this.language = option(Arrays.asList("language", "l"), "Programming language (for example 'java')")
159173
.withRequiredArg();
160-
this.bootVersion = option(Arrays.asList("bootVersion", "boot-version", "b"),
174+
this.bootVersion = option(Arrays.asList("boot-version", "b"),
161175
"Spring Boot version (for example '1.2.0.RELEASE')").withRequiredArg();
162176
this.dependencies = option(Arrays.asList("dependencies", "d"),
163177
"Comma-separated list of dependency identifiers to include in the generated project")
@@ -254,6 +268,16 @@ protected ProjectGenerationRequest createProjectGenerationRequest(OptionSet opti
254268
return request;
255269
}
256270

271+
private static String processArgument(String argument) {
272+
for (Map.Entry<String, String> entry : CAMEL_CASE_OPTIONS.entrySet()) {
273+
String name = entry.getKey();
274+
if (argument.startsWith(name + " ") || argument.startsWith(name + "=")) {
275+
return entry.getValue() + argument.substring(name.length());
276+
}
277+
}
278+
return argument;
279+
}
280+
257281
}
258282

259283
}

spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/options/OptionHandler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Map;
2929
import java.util.Set;
3030
import java.util.TreeSet;
31+
import java.util.function.Function;
3132

3233
import joptsimple.BuiltinHelpFormatter;
3334
import joptsimple.HelpFormatter;
@@ -49,12 +50,30 @@
4950
*/
5051
public class OptionHandler {
5152

53+
private final Function<String, String> argumentProcessor;
54+
5255
private OptionParser parser;
5356

5457
private String help;
5558

5659
private Collection<OptionHelp> optionHelp;
5760

61+
/**
62+
* Create a new {@link OptionHandler} instance.
63+
*/
64+
public OptionHandler() {
65+
this(Function.identity());
66+
}
67+
68+
/**
69+
* Create a new {@link OptionHandler} instance with an argument processor.
70+
* @param argumentProcessor strategy that can be used to manipulate arguments before
71+
* they are used.
72+
*/
73+
public OptionHandler(Function<String, String> argumentProcessor) {
74+
this.argumentProcessor = argumentProcessor;
75+
}
76+
5877
public OptionSpecBuilder option(String name, String description) {
5978
return getParser().accepts(name, description);
6079
}
@@ -80,6 +99,7 @@ public final ExitStatus run(String... args) throws Exception {
8099
if ("-cp".equals(argsToUse[i])) {
81100
argsToUse[i] = "--cp";
82101
}
102+
argsToUse[i] = this.argumentProcessor.apply(argsToUse[i]);
83103
}
84104
OptionSet options = getParser().parse(argsToUse);
85105
return run(options);

spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,33 @@ void parseProjectOptions() throws Exception {
274274
}
275275

276276
@Test
277-
void parseProjectWithKebabCaseCLIOptions() throws Exception {
277+
void parseProjectWithCamelCaseOptions() throws Exception {
278+
this.handler.disableProjectGeneration();
279+
this.command.run("--groupId=org.demo", "--artifactId=acme", "--version=1.2.3-SNAPSHOT", "--name=acme-sample",
280+
"--description=Acme sample project", "--packageName=demo.foo", "--type=ant-project", "--build=grunt",
281+
"--format=web", "--packaging=war", "--javaVersion=1.9", "--language=groovy",
282+
"--bootVersion=1.2.0.RELEASE", "--dependencies=web,data-jpa");
283+
assertThat(this.handler.lastRequest.getGroupId()).isEqualTo("org.demo");
284+
assertThat(this.handler.lastRequest.getArtifactId()).isEqualTo("acme");
285+
assertThat(this.handler.lastRequest.getVersion()).isEqualTo("1.2.3-SNAPSHOT");
286+
assertThat(this.handler.lastRequest.getName()).isEqualTo("acme-sample");
287+
assertThat(this.handler.lastRequest.getDescription()).isEqualTo("Acme sample project");
288+
assertThat(this.handler.lastRequest.getPackageName()).isEqualTo("demo.foo");
289+
assertThat(this.handler.lastRequest.getType()).isEqualTo("ant-project");
290+
assertThat(this.handler.lastRequest.getBuild()).isEqualTo("grunt");
291+
assertThat(this.handler.lastRequest.getFormat()).isEqualTo("web");
292+
assertThat(this.handler.lastRequest.getPackaging()).isEqualTo("war");
293+
assertThat(this.handler.lastRequest.getJavaVersion()).isEqualTo("1.9");
294+
assertThat(this.handler.lastRequest.getLanguage()).isEqualTo("groovy");
295+
assertThat(this.handler.lastRequest.getBootVersion()).isEqualTo("1.2.0.RELEASE");
296+
List<String> dependencies = this.handler.lastRequest.getDependencies();
297+
assertThat(dependencies).hasSize(2);
298+
assertThat(dependencies.contains("web")).isTrue();
299+
assertThat(dependencies.contains("data-jpa")).isTrue();
300+
}
301+
302+
@Test
303+
void parseProjectWithKebabCaseOptions() throws Exception {
278304
this.handler.disableProjectGeneration();
279305
this.command.run("--group-id=org.demo", "--artifact-id=acme", "--version=1.2.3-SNAPSHOT", "--name=acme-sample",
280306
"--description=Acme sample project", "--package-name=demo.foo", "--type=ant-project", "--build=grunt",

0 commit comments

Comments
 (0)