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 @@ -42,6 +42,7 @@
* </pre>
*
* @author Mahmoud Ben Hassine
* @author David Pilar
* @since 4.0.0
*/
public class DefaultCommandParser implements CommandParser {
Expand All @@ -51,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(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"));

// the first word is the (root) command name
String commandName = words.get(0);
Expand Down Expand Up @@ -156,11 +157,26 @@ else if (word.startsWith("-")) {
value = tokens[1];
}
}
return CommandOption.with().shortName(shortName).longName(longName).value(value).build();
return CommandOption.with()
.shortName(shortName)
.longName(longName)
.value(unquoteAndUnescapeQuoted(value))
.build();
}

private CommandArgument parseArgument(int index, String word) {
return new CommandArgument(index, word);
return new CommandArgument(index, unquoteAndUnescapeQuoted(word));
}

private String unquoteAndUnescapeQuoted(String s) {
// only process quoted strings
if (s.length() >= 2 && s.startsWith("\"") && s.endsWith("\"")) {
s = s.substring(1, s.length() - 1);

// unescape only inside quoted strings
s = s.replace("\\\"", "\"").replace("\\\\", "\\");
}
return s;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -160,4 +165,81 @@ void testParseWithSubCommandWithoutOptionsAndWithoutSeparator() {
assertEquals("arg2", parsedInput.subCommands().get(2));
}

@ParameterizedTest
@MethodSource("parseWithQuotedOptionData")
void testParseWithQuotedOption(String input, String longName, char shortName, String expectedValue) {
// when
ParsedInput parsedInput = parser.parse(input);

// then
assertEquals("mycommand", parsedInput.commandName());
assertEquals(1, parsedInput.options().size());
assertEquals(longName, parsedInput.options().get(0).longName());
assertEquals(shortName, parsedInput.options().get(0).shortName());
assertEquals(expectedValue, parsedInput.options().get(0).value());
}

static Stream<Arguments> parseWithQuotedOptionData() {
return Stream.of(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\"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 \"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 -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\"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 \"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"));
}

@ParameterizedTest
@MethodSource("parseWithQuotedArgumentData")
void testParseWithQuotedArgument(String input, String expectedValue) {
// when
ParsedInput parsedInput = parser.parse(input);

// then
assertEquals("mycommand", parsedInput.commandName());
assertEquals(expectedValue, parsedInput.arguments().get(0).value());
assertEquals(1, parsedInput.arguments().size());
}

static Stream<Arguments> parseWithQuotedArgumentData() {
return Stream.of(Arguments.of("mycommand -- value", "value"),
Arguments.of("mycommand -- \\\"value\\\"", "\\\"value\\\""),
Arguments.of("mycommand -- \"value\"", "value"),
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"));
}

}