Skip to content

Commit 886ca7f

Browse files
committed
Polish contribution and SimpleCommandLineArgs-related code
Closes gh-34282
1 parent c463b93 commit 886ca7f

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,8 +25,7 @@
2525
/**
2626
* Abstract base class for {@link PropertySource} implementations backed by command line
2727
* arguments. The parameterized type {@code T} represents the underlying source of command
28-
* line options. For instance, {@link SimpleCommandLinePropertySource} uses a String
29-
* array.
28+
* line options.
3029
*
3130
* <h3>Purpose and General Usage</h3>
3231
*
@@ -259,10 +258,11 @@ public final boolean containsProperty(String name) {
259258
* This implementation first checks to see if the name specified is the special
260259
* {@linkplain #setNonOptionArgsPropertyName(String) "non-option arguments" property},
261260
* and if so delegates to the abstract {@link #getNonOptionArgs()} method. If so
262-
* and the collection of non-option arguments is empty, this method returns {@code
263-
* null}. If not empty, it returns a comma-separated String of all non-option
264-
* arguments. Otherwise, delegates to and returns the result of the abstract {@link
265-
* #getOptionValues(String)} method.
261+
* and the collection of non-option arguments is empty, this method returns
262+
* {@code null}. If not empty, it returns a comma-separated String of all non-option
263+
* arguments. Otherwise, this method delegates to and returns a comma-separated String
264+
* of the results of the abstract {@link #getOptionValues(String)} method or
265+
* {@code null} if there are no such option values.
266266
*/
267267
@Override
268268
@Nullable

spring-core/src/main/java/org/springframework/core/env/SimpleCommandLineArgsParser.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,8 +28,9 @@
2828
* <p>That is, options must be prefixed with "{@code --}" and may or may not
2929
* specify a value. If a value is specified, the name and value must be separated
3030
* <em>without spaces</em> by an equals sign ("="). The value may optionally be
31-
* an empty string. if the option is present and has multiple values (e. g. "--foo=bar --foo=baz"),
32-
* the values are parsed as a collection.
31+
* an empty string. If an option is present multiple times with different values
32+
* &mdash; for example, {@code --foo=bar --foo=baz} &mdash; all supplied values
33+
* will be stored for the option.
3334
*
3435
* <h4>Valid examples of option arguments</h4>
3536
* <pre class="code">

spring-core/src/main/java/org/springframework/core/env/SimpleCommandLinePropertySource.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,8 @@
2222
import org.springframework.util.StringUtils;
2323

2424
/**
25-
* {@link CommandLinePropertySource} implementation backed by a simple String array.
25+
* {@link CommandLinePropertySource} implementation backed by an instance of
26+
* {@link CommandLineArgs}.
2627
*
2728
* <h3>Purpose</h3>
2829
* <p>This {@code CommandLinePropertySource} implementation aims to provide the simplest
@@ -40,8 +41,9 @@
4041
* <p>That is, options must be prefixed with "{@code --}" and may or may not
4142
* specify a value. If a value is specified, the name and value must be separated
4243
* <em>without spaces</em> by an equals sign ("="). The value may optionally be
43-
* an empty string. if the option is present and has multiple values (e. g. "--foo=bar --foo=baz"),
44-
* the values are parsed as a collection.
44+
* an empty string. If an option is present multiple times with different values
45+
* &mdash; for example, {@code --foo=bar --foo=baz} &mdash; all supplied values
46+
* will be stored for the option.
4547
*
4648
* <h4>Valid examples of option arguments</h4>
4749
* <pre class="code">

spring-core/src/test/java/org/springframework/core/env/SimpleCommandLineArgsParserTests.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ void withNoOptions() {
4646
void withSingleOptionAndNoValue() {
4747
CommandLineArgs args = parser.parse("--o1");
4848
assertThat(args.containsOption("o1")).isTrue();
49-
assertThat(args.getOptionValues("o1")).isEqualTo(Collections.EMPTY_LIST);
49+
assertThat(args.getOptionValues("o1")).isEmpty();
5050
}
5151

5252
@Test
@@ -56,6 +56,20 @@ void withSingleOptionAndValue() {
5656
assertThat(args.getOptionValues("o1")).containsExactly("v1");
5757
}
5858

59+
@Test
60+
void withRepeatedOptionAndSameValues() {
61+
CommandLineArgs args = parser.parse("--o1=v1", "--o1=v1", "--o1=v1");
62+
assertThat(args.containsOption("o1")).isTrue();
63+
assertThat(args.getOptionValues("o1")).containsExactly("v1", "v1", "v1");
64+
}
65+
66+
@Test
67+
void withRepeatedOptionAndDifferentValues() {
68+
CommandLineArgs args = parser.parse("--o1=v1", "--o1=v2", "--o1=v3");
69+
assertThat(args.containsOption("o1")).isTrue();
70+
assertThat(args.getOptionValues("o1")).containsExactly("v1", "v2", "v3");
71+
}
72+
5973
@Test
6074
void withMixOfOptionsHavingValueAndOptionsHavingNoValue() {
6175
CommandLineArgs args = parser.parse("--o1=v1", "--o2");
@@ -95,17 +109,17 @@ void withNonOptionArguments() {
95109
}
96110

97111
@Test
98-
void assertOptionNamesIsUnmodifiable() {
112+
void optionNamesSetIsUnmodifiable() {
99113
CommandLineArgs args = new SimpleCommandLineArgsParser().parse();
100-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
101-
args.getOptionNames().add("bogus"));
114+
assertThatExceptionOfType(UnsupportedOperationException.class)
115+
.isThrownBy(() -> args.getOptionNames().add("bogus"));
102116
}
103117

104118
@Test
105-
void assertNonOptionArgsIsUnmodifiable() {
119+
void nonOptionArgsListIsUnmodifiable() {
106120
CommandLineArgs args = new SimpleCommandLineArgsParser().parse();
107-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
108-
args.getNonOptionArgs().add("foo"));
121+
assertThatExceptionOfType(UnsupportedOperationException.class)
122+
.isThrownBy(() -> args.getNonOptionArgs().add("foo"));
109123
}
110124

111125
@Test

spring-core/src/test/java/org/springframework/core/env/SimpleCommandLinePropertySourceTests.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,15 @@ void withOptionArgsOnly() {
6161
assertThat(ps.getProperty("o3")).isNull();
6262
}
6363

64+
@Test // gh-34282
65+
void withRepeatedOptionArgs() {
66+
CommandLinePropertySource<?> ps = new SimpleCommandLinePropertySource("--o1=v1", "--o1=v2", "--o1=v3");
67+
assertThat(ps.containsProperty("o1")).isTrue();
68+
assertThat(ps.containsProperty("o2")).isFalse();
69+
assertThat(ps.getProperty("o1")).isEqualTo("v1,v2,v3");
70+
assertThat(ps.getProperty("o2")).isNull();
71+
}
72+
6473
@Test // gh-24464
6574
void withOptionalArg_andArgIsEmpty() {
6675
EnumerablePropertySource<?> ps = new SimpleCommandLinePropertySource("--foo=");

0 commit comments

Comments
 (0)