Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "--" end of options in SimpleCommandLineArgsParser #31513

Closed
wants to merge 1 commit into from
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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,13 @@
* <em>without spaces</em> by an equals sign ("="). The value may optionally be
* an empty string.
*
* <p>This parser supports the POSIX "end of options" delimiter, meaning that
* any {@code "--"} (empty option name) in the command signals that all remaining
* arguments will non-optional. For example, here {@code "--opt=ignored"} is considered
* as a non-optional argument.
* <pre class="code">
* --foo=bar -- --opt=ignored</pre>
*
* <h4>Valid examples of option arguments</h4>
* <pre class="code">
* --foo
Expand All @@ -53,6 +60,7 @@
*
* @author Chris Beams
* @author Sam Brannen
* @author Brian Clozel
* @since 3.1
*/
class SimpleCommandLineArgsParser {
Expand All @@ -65,23 +73,26 @@ class SimpleCommandLineArgsParser {
*/
public CommandLineArgs parse(String... args) {
CommandLineArgs commandLineArgs = new CommandLineArgs();
boolean endOfOptions = false;
for (String arg : args) {
if (arg.startsWith("--")) {
if (!endOfOptions && arg.startsWith("--")) {
String optionText = arg.substring(2);
String optionName;
String optionValue = null;
int indexOfEqualsSign = optionText.indexOf('=');
if (indexOfEqualsSign > -1) {
optionName = optionText.substring(0, indexOfEqualsSign);
optionValue = optionText.substring(indexOfEqualsSign + 1);
String optionName = optionText.substring(0, indexOfEqualsSign);
String optionValue = optionText.substring(indexOfEqualsSign + 1);
if (optionName.isEmpty()) {
throw new IllegalArgumentException("Invalid argument syntax: " + arg);
}
commandLineArgs.addOptionArg(optionName, optionValue);
}
else {
optionName = optionText;
else if (!optionText.isEmpty()){
commandLineArgs.addOptionArg(optionText, null);
}
if (optionName.isEmpty()) {
throw new IllegalArgumentException("Invalid argument syntax: " + arg);
else {
// '--' End of options delimiter, all remaining args must be non-optional
endOfOptions = true;
}
commandLineArgs.addOptionArg(optionName, optionValue);
}
else {
commandLineArgs.addNonOptionArg(arg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
*
* @author Chris Beams
* @author Sam Brannen
* @author Brian Clozel
*/
class SimpleCommandLineArgsParserTests {

Expand Down Expand Up @@ -66,11 +67,6 @@ void withMixOfOptionsHavingValueAndOptionsHavingNoValue() {
assertThat(args.getOptionValues("o3")).isNull();
}

@Test
void withEmptyOptionText() {
assertThatIllegalArgumentException().isThrownBy(() -> parser.parse("--"));
}

@Test
void withEmptyOptionName() {
assertThatIllegalArgumentException().isThrownBy(() -> parser.parse("--=v1"));
Expand Down Expand Up @@ -112,4 +108,13 @@ void assertNonOptionArgsIsUnmodifiable() {
args.getNonOptionArgs().add("foo"));
}

@Test
void supportsEndOfOptionsDelimiter() {
CommandLineArgs args = parser.parse("--o1=v1", "--", "--o2=v2");
assertThat(args.containsOption("o1")).isTrue();
assertThat(args.containsOption("o2")).isFalse();
assertThat(args.getOptionValues("o1")).containsExactly("v1");
assertThat(args.getNonOptionArgs()).contains("--o2=v2");
}

}