Skip to content

Commit 90ea574

Browse files
czpilarfmbenhassine
authored andcommitted
Fix quoting of option values
Resolves #1291 Signed-off-by: czpilar <[email protected]>
1 parent ec4d1f3 commit 90ea574

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class DefaultCommandParser implements CommandParser {
5252
@Override
5353
public ParsedInput parse(String input) {
5454
log.debug("Parsing input: " + input);
55-
List<String> words = List.of(input.split(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"));
55+
List<String> words = List.of(input.split("\\s+(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"));
5656

5757
// the first word is the (root) command name
5858
String commandName = words.get(0);
@@ -143,15 +143,15 @@ private CommandOption parseOption(String word) {
143143
String value = "";
144144
if (word.startsWith("--")) {
145145
word = word.substring(2);
146-
String[] tokens = word.split("=");
146+
String[] tokens = word.split("=", 2);
147147
longName = tokens[0];
148148
if (tokens.length > 1) {
149149
value = tokens[1];
150150
}
151151
}
152152
else if (word.startsWith("-")) {
153153
word = word.substring(1);
154-
String[] tokens = word.split("=");
154+
String[] tokens = word.split("=", 2);
155155
shortName = tokens[0].charAt(0);
156156
if (tokens.length > 1) {
157157
value = tokens[1];

spring-shell-core/src/test/java/org/springframework/shell/core/command/DefaultCommandParserTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,39 +184,50 @@ static Stream<Arguments> parseWithQuotedOptionData() {
184184
Arguments.of("mycommand --option=\\\"value\\\"", "option", ' ', "\\\"value\\\""),
185185
Arguments.of("mycommand --option=\"value\"", "option", ' ', "value"),
186186
Arguments.of("mycommand --option=\"value1 value2\"", "option", ' ', "value1 value2"),
187+
Arguments.of("mycommand --option=\"value1=value2\"", "option", ' ', "value1=value2"),
187188
Arguments.of("mycommand --option=value1\"inside\"value2", "option", ' ', "value1\"inside\"value2"),
188189
Arguments.of("mycommand --option=\"value1 \\\"inside\\\" value2\"", "option", ' ',
189190
"value1 \"inside\" value2"),
190191
Arguments.of("mycommand --option=value1'inside'value2", "option", ' ', "value1'inside'value2"),
191192
Arguments.of("mycommand --option=\"value1 'inside' value2\"", "option", ' ', "value1 'inside' value2"),
193+
Arguments.of("mycommand --option=value", "option", ' ', "value"),
194+
Arguments.of("mycommand --option=\"value\"", "option", ' ', "value"),
192195

193196
Arguments.of("mycommand --option value", "option", ' ', "value"),
194197
Arguments.of("mycommand --option \\\"value\\\"", "option", ' ', "\\\"value\\\""),
195198
Arguments.of("mycommand --option \"value\"", "option", ' ', "value"),
196199
Arguments.of("mycommand --option \"value1 value2\"", "option", ' ', "value1 value2"),
200+
Arguments.of("mycommand --option \"value1=value2\"", "option", ' ', "value1=value2"),
197201
Arguments.of("mycommand --option value1\"inside\"value2", "option", ' ', "value1\"inside\"value2"),
198202
Arguments.of("mycommand --option \"value1 \\\"inside\\\" value2\"", "option", ' ',
199203
"value1 \"inside\" value2"),
200204
Arguments.of("mycommand --option value1'inside'value2", "option", ' ', "value1'inside'value2"),
201205
Arguments.of("mycommand --option \"value1 'inside' value2\"", "option", ' ', "value1 'inside' value2"),
206+
Arguments.of("mycommand --option value", "option", ' ', "value"),
207+
Arguments.of("mycommand --option \"value\"", "option", ' ', "value"),
202208

203209
Arguments.of("mycommand -o=value", "", 'o', "value"),
204210
Arguments.of("mycommand -o=\\\"value\\\"", "", 'o', "\\\"value\\\""),
205211
Arguments.of("mycommand -o=\"value\"", "", 'o', "value"),
206212
Arguments.of("mycommand -o=\"value1 value2\"", "", 'o', "value1 value2"),
213+
Arguments.of("mycommand -o=\"value1=value2\"", "", 'o', "value1=value2"),
207214
Arguments.of("mycommand -o=value1\"inside\"value2", "", 'o', "value1\"inside\"value2"),
208215
Arguments.of("mycommand -o=\"value1 \\\"inside\\\" value2\"", "", 'o', "value1 \"inside\" value2"),
209216
Arguments.of("mycommand -o=value1'inside'value2", "", 'o', "value1'inside'value2"),
210217
Arguments.of("mycommand -o=\"value1 'inside' value2\"", "", 'o', "value1 'inside' value2"),
218+
Arguments.of("mycommand -o=value", "", 'o', "value"),
219+
Arguments.of("mycommand -o=\"value\"", "", 'o', "value"),
211220

212221
Arguments.of("mycommand -o value", "", 'o', "value"),
213222
Arguments.of("mycommand -o \\\"value\\\"", "", 'o', "\\\"value\\\""),
214223
Arguments.of("mycommand -o \"value\"", "", 'o', "value"),
215224
Arguments.of("mycommand -o \"value1 value2\"", "", 'o', "value1 value2"),
225+
Arguments.of("mycommand -o \"value1=value2\"", "", 'o', "value1=value2"),
216226
Arguments.of("mycommand -o value1\"inside\"value2", "", 'o', "value1\"inside\"value2"),
217227
Arguments.of("mycommand -o \"value1 \\\"inside\\\" value2\"", "", 'o', "value1 \"inside\" value2"),
218228
Arguments.of("mycommand -o value1'inside'value2", "", 'o', "value1'inside'value2"),
219-
Arguments.of("mycommand -o \"value1 'inside' value2\"", "", 'o', "value1 'inside' value2"));
229+
Arguments.of("mycommand -o \"value1 'inside' value2\"", "", 'o', "value1 'inside' value2"),
230+
Arguments.of("mycommand -o \"value\"", "", 'o', "value"));
220231
}
221232

222233
@ParameterizedTest
@@ -236,10 +247,12 @@ static Stream<Arguments> parseWithQuotedArgumentData() {
236247
Arguments.of("mycommand -- \\\"value\\\"", "\\\"value\\\""),
237248
Arguments.of("mycommand -- \"value\"", "value"),
238249
Arguments.of("mycommand -- \"value1 value2\"", "value1 value2"),
250+
Arguments.of("mycommand -- \"value1=value2\"", "value1=value2"),
239251
Arguments.of("mycommand -- value1\"inside\"value2", "value1\"inside\"value2"),
240252
Arguments.of("mycommand -- \"value1 \\\"inside\\\" value2\"", "value1 \"inside\" value2"),
241253
Arguments.of("mycommand -- value1'inside'value2", "value1'inside'value2"),
242-
Arguments.of("mycommand -- \"value1 'inside' value2\"", "value1 'inside' value2"));
254+
Arguments.of("mycommand -- \"value1 'inside' value2\"", "value1 'inside' value2"),
255+
Arguments.of("mycommand -- value", "value"), Arguments.of("mycommand -- \"value\"", "value"));
243256
}
244257

245258
}

0 commit comments

Comments
 (0)