Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DefaultCommandParser implements CommandParser {
@Override
public ParsedInput parse(String input) {
log.debug("Parsing input: " + input);
List<String> words = List.of(input.split(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"));
List<String> words = List.of(input.split("\\s+(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"));

// the first word is the (root) command name
String commandName = words.get(0);
Expand Down Expand Up @@ -143,15 +143,15 @@ private CommandOption parseOption(String word) {
String value = "";
if (word.startsWith("--")) {
word = word.substring(2);
String[] tokens = word.split("=");
String[] tokens = word.split("=", 2);
longName = tokens[0];
if (tokens.length > 1) {
value = tokens[1];
}
}
else if (word.startsWith("-")) {
word = word.substring(1);
String[] tokens = word.split("=");
String[] tokens = word.split("=", 2);
shortName = tokens[0].charAt(0);
if (tokens.length > 1) {
value = tokens[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,39 +184,50 @@ static Stream<Arguments> parseWithQuotedOptionData() {
Arguments.of("mycommand --option=\\\"value\\\"", "option", ' ', "\\\"value\\\""),
Arguments.of("mycommand --option=\"value\"", "option", ' ', "value"),
Arguments.of("mycommand --option=\"value1 value2\"", "option", ' ', "value1 value2"),
Arguments.of("mycommand --option=\"value1=value2\"", "option", ' ', "value1=value2"),
Arguments.of("mycommand --option=value1\"inside\"value2", "option", ' ', "value1\"inside\"value2"),
Arguments.of("mycommand --option=\"value1 \\\"inside\\\" value2\"", "option", ' ',
"value1 \"inside\" value2"),
Arguments.of("mycommand --option=value1'inside'value2", "option", ' ', "value1'inside'value2"),
Arguments.of("mycommand --option=\"value1 'inside' value2\"", "option", ' ', "value1 'inside' value2"),
Arguments.of("mycommand --option=value", "option", ' ', "value"),
Arguments.of("mycommand --option=\"value\"", "option", ' ', "value"),

Arguments.of("mycommand --option value", "option", ' ', "value"),
Arguments.of("mycommand --option \\\"value\\\"", "option", ' ', "\\\"value\\\""),
Arguments.of("mycommand --option \"value\"", "option", ' ', "value"),
Arguments.of("mycommand --option \"value1 value2\"", "option", ' ', "value1 value2"),
Arguments.of("mycommand --option \"value1=value2\"", "option", ' ', "value1=value2"),
Arguments.of("mycommand --option value1\"inside\"value2", "option", ' ', "value1\"inside\"value2"),
Arguments.of("mycommand --option \"value1 \\\"inside\\\" value2\"", "option", ' ',
"value1 \"inside\" value2"),
Arguments.of("mycommand --option value1'inside'value2", "option", ' ', "value1'inside'value2"),
Arguments.of("mycommand --option \"value1 'inside' value2\"", "option", ' ', "value1 'inside' value2"),
Arguments.of("mycommand --option value", "option", ' ', "value"),
Arguments.of("mycommand --option \"value\"", "option", ' ', "value"),

Arguments.of("mycommand -o=value", "", 'o', "value"),
Arguments.of("mycommand -o=\\\"value\\\"", "", 'o', "\\\"value\\\""),
Arguments.of("mycommand -o=\"value\"", "", 'o', "value"),
Arguments.of("mycommand -o=\"value1 value2\"", "", 'o', "value1 value2"),
Arguments.of("mycommand -o=\"value1=value2\"", "", 'o', "value1=value2"),
Arguments.of("mycommand -o=value1\"inside\"value2", "", 'o', "value1\"inside\"value2"),
Arguments.of("mycommand -o=\"value1 \\\"inside\\\" value2\"", "", 'o', "value1 \"inside\" value2"),
Arguments.of("mycommand -o=value1'inside'value2", "", 'o', "value1'inside'value2"),
Arguments.of("mycommand -o=\"value1 'inside' value2\"", "", 'o', "value1 'inside' value2"),
Arguments.of("mycommand -o=value", "", 'o', "value"),
Arguments.of("mycommand -o=\"value\"", "", 'o', "value"),

Arguments.of("mycommand -o value", "", 'o', "value"),
Arguments.of("mycommand -o \\\"value\\\"", "", 'o', "\\\"value\\\""),
Arguments.of("mycommand -o \"value\"", "", 'o', "value"),
Arguments.of("mycommand -o \"value1 value2\"", "", 'o', "value1 value2"),
Arguments.of("mycommand -o \"value1=value2\"", "", 'o', "value1=value2"),
Arguments.of("mycommand -o value1\"inside\"value2", "", 'o', "value1\"inside\"value2"),
Arguments.of("mycommand -o \"value1 \\\"inside\\\" value2\"", "", 'o', "value1 \"inside\" value2"),
Arguments.of("mycommand -o value1'inside'value2", "", 'o', "value1'inside'value2"),
Arguments.of("mycommand -o \"value1 'inside' value2\"", "", 'o', "value1 'inside' value2"));
Arguments.of("mycommand -o \"value1 'inside' value2\"", "", 'o', "value1 'inside' value2"),
Arguments.of("mycommand -o \"value\"", "", 'o', "value"));
}

@ParameterizedTest
Expand All @@ -236,10 +247,12 @@ static Stream<Arguments> parseWithQuotedArgumentData() {
Arguments.of("mycommand -- \\\"value\\\"", "\\\"value\\\""),
Arguments.of("mycommand -- \"value\"", "value"),
Arguments.of("mycommand -- \"value1 value2\"", "value1 value2"),
Arguments.of("mycommand -- \"value1=value2\"", "value1=value2"),
Arguments.of("mycommand -- value1\"inside\"value2", "value1\"inside\"value2"),
Arguments.of("mycommand -- \"value1 \\\"inside\\\" value2\"", "value1 \"inside\" value2"),
Arguments.of("mycommand -- value1'inside'value2", "value1'inside'value2"),
Arguments.of("mycommand -- \"value1 'inside' value2\"", "value1 'inside' value2"));
Arguments.of("mycommand -- \"value1 'inside' value2\"", "value1 'inside' value2"),
Arguments.of("mycommand -- value", "value"), Arguments.of("mycommand -- \"value\"", "value"));
}

}