Skip to content

Commit e337e34

Browse files
committed
Fix handling of required options
1 parent 29e27f3 commit e337e34

File tree

2 files changed

+22
-9
lines changed
  • spring-shell-core/src/main/java/org/springframework/shell/core/command/adapter
  • spring-shell-samples/spring-shell-sample-petclinic/src/main/java/org/springframework/shell/samples/petclinic/commands

2 files changed

+22
-9
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/command/adapter/MethodInvokerCommandAdapter.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,33 @@ private List<Object> prepareArguments(CommandContext commandContext) {
135135
log.debug("Processing option for parameter: " + parameters[i].getName());
136136
char shortName = optionAnnotation.shortName();
137137
String longName = optionAnnotation.longName();
138+
boolean required = optionAnnotation.required();
138139
CommandOption commandOption = commandContext
139140
.getOptionByName(longName.isEmpty() ? String.valueOf(shortName) : longName);
140-
if (commandOption == null) {
141-
// Option not provided, use default value
142-
String defaultValue = optionAnnotation.defaultValue();
143-
Class<?> parameterType = parameterTypes[i];
144-
Object value = this.conversionService.convert(defaultValue, parameterType);
145-
args.add(value);
146-
}
147-
else {
141+
if (commandOption != null) {
148142
String rawValue = commandOption.value();
149143
Class<?> parameterType = parameterTypes[i];
150144
Object value = this.conversionService.convert(rawValue, parameterType);
151145
args.add(value);
152146
}
147+
else {
148+
if (required) {
149+
throw new IllegalArgumentException(
150+
"Required option '--" + (longName.isEmpty() ? shortName : longName) + "' is missing.");
151+
}
152+
else {
153+
// try to use default value
154+
String defaultValue = optionAnnotation.defaultValue();
155+
if (defaultValue.isEmpty()) {
156+
log.warn("No value provided for optional option '--"
157+
+ (longName.isEmpty() ? shortName : longName)
158+
+ "' and no default value specified.");
159+
}
160+
Class<?> parameterType = parameterTypes[i];
161+
Object value = this.conversionService.convert(defaultValue, parameterType);
162+
args.add(value);
163+
}
164+
}
153165
continue;
154166
}
155167
// Handle Argument injection

spring-shell-samples/spring-shell-sample-petclinic/src/main/java/org/springframework/shell/samples/petclinic/commands/PetCommands.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public void listPets(CommandContext commandContext) {
5252

5353
@Command(name = { "pets", "info" }, description = "Show detail about a given pet", group = "Pets",
5454
help = "Show the details about a given pet")
55-
public void showPet(@Option(longName = "petId", description = "The pet ID") int id, CommandContext commandContext) {
55+
public void showPet(@Option(longName = "petId", description = "The pet ID", required = true) int id,
56+
CommandContext commandContext) {
5657
try {
5758
Pet pet = this.jdbcClient.sql("SELECT * FROM PETS where id = " + id)
5859
.query(new DataClassRowMapper<>(Pet.class))

0 commit comments

Comments
 (0)