Browse Source

Polish SimpleCommandLinePropertySource support

pull/32294/head
Sam Brannen 2 years ago
parent
commit
fc9a118406
  1. 21
      spring-core/src/main/java/org/springframework/core/env/CommandLineArgs.java
  2. 28
      spring-core/src/main/java/org/springframework/core/env/SimpleCommandLineArgsParser.java
  3. 18
      spring-core/src/main/java/org/springframework/core/env/SimpleCommandLinePropertySource.java

21
spring-core/src/main/java/org/springframework/core/env/CommandLineArgs.java vendored

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,8 +26,9 @@ import java.util.Set;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
* A simple representation of command line arguments, broken into "option arguments" and * A simple representation of command line arguments, broken into
* "non-option arguments". * {@linkplain #addOptionArg(String, String) option arguments} and
* {@linkplain #addNonOptionArg(String) non-option arguments}.
* *
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
@ -39,10 +40,10 @@ class CommandLineArgs {
private final List<String> nonOptionArgs = new ArrayList<>(); private final List<String> nonOptionArgs = new ArrayList<>();
/** /**
* Add an option argument for the given option name and add the given value to the * Add an option argument for the given option name, and add the given value to the
* list of values associated with this option (of which there may be zero or more). * list of values associated with this option (of which there may be zero or more).
* The given value may be {@code null}, indicating that the option was specified * <p>The given value may be {@code null}, indicating that the option was specified
* without an associated value (e.g. "--foo" vs. "--foo=bar"). * without an associated value &mdash; for example, "--foo" vs. "--foo=bar".
*/ */
public void addOptionArg(String optionName, @Nullable String optionValue) { public void addOptionArg(String optionName, @Nullable String optionValue) {
if (!this.optionArgs.containsKey(optionName)) { if (!this.optionArgs.containsKey(optionName)) {
@ -54,7 +55,7 @@ class CommandLineArgs {
} }
/** /**
* Return the set of all option arguments present on the command line. * Return the set of the names of all option arguments present on the command line.
*/ */
public Set<String> getOptionNames() { public Set<String> getOptionNames() {
return Collections.unmodifiableSet(this.optionArgs.keySet()); return Collections.unmodifiableSet(this.optionArgs.keySet());
@ -68,9 +69,9 @@ class CommandLineArgs {
} }
/** /**
* Return the list of values associated with the given option. {@code null} signifies * Return the list of values associated with the given option.
* that the option was not present; empty list signifies that no values were associated * <p>{@code null} signifies that the option was not present on the command
* with this option. * line. An empty list signifies that no values were associated with this option.
*/ */
@Nullable @Nullable
public List<String> getOptionValues(String optionName) { public List<String> getOptionValues(String optionName) {

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,13 +30,6 @@ package org.springframework.core.env;
* <em>without spaces</em> by an equals sign ("="). The value may optionally be * <em>without spaces</em> by an equals sign ("="). The value may optionally be
* an empty string. * 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> * <h4>Valid examples of option arguments</h4>
* <pre class="code"> * <pre class="code">
* --foo * --foo
@ -53,15 +46,26 @@ package org.springframework.core.env;
* --foo = bar * --foo = bar
* --foo=bar --foo=baz --foo=biz</pre> * --foo=bar --foo=baz --foo=biz</pre>
* *
* <h3>End of option arguments</h3>
* <p>This parser supports the POSIX "end of options" delimiter, meaning that any
* {@code "--"} (empty option name) in the command line signals that all remaining
* arguments are non-option arguments. For example, {@code "--opt1=ignored"},
* {@code "--opt2"}, and {@code "filename"} in the following command line are
* considered non-option arguments.
* <pre class="code">
* --foo=bar -- --opt1=ignored -opt2 filename</pre>
*
* <h3>Working with non-option arguments</h3> * <h3>Working with non-option arguments</h3>
* <p>Any and all arguments specified at the command line without the "{@code --}" * <p>Any arguments following the "end of options" delimiter ({@code --}) or
* option prefix will be considered as "non-option arguments" and made available * specified without the "{@code --}" option prefix will be considered as
* through the {@link CommandLineArgs#getNonOptionArgs()} method. * "non-option arguments" and made available through the
* {@link CommandLineArgs#getNonOptionArgs()} method.
* *
* @author Chris Beams * @author Chris Beams
* @author Sam Brannen * @author Sam Brannen
* @author Brian Clozel * @author Brian Clozel
* @since 3.1 * @since 3.1
* @see SimpleCommandLinePropertySource
*/ */
class SimpleCommandLineArgsParser { class SimpleCommandLineArgsParser {
@ -90,7 +94,7 @@ class SimpleCommandLineArgsParser {
commandLineArgs.addOptionArg(optionText, null); commandLineArgs.addOptionArg(optionText, null);
} }
else { else {
// '--' End of options delimiter, all remaining args must be non-optional // '--' End of options delimiter, all remaining args are non-option arguments
endOfOptions = true; endOfOptions = true;
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -58,10 +58,20 @@ import org.springframework.util.StringUtils;
* --foo = bar * --foo = bar
* --foo=bar --foo=baz --foo=biz</pre> * --foo=bar --foo=baz --foo=biz</pre>
* *
* <h3>End of option arguments</h3>
* <p>The underlying parser supports the POSIX "end of options" delimiter, meaning
* that any {@code "--"} (empty option name) in the command line signals that all
* remaining arguments are non-option arguments. For example, {@code "--opt1=ignored"},
* {@code "--opt2"}, and {@code "filename"} in the following command line are
* considered non-option arguments.
* <pre class="code">
* --foo=bar -- --opt1=ignored -opt2 filename</pre>
*
* <h3>Working with non-option arguments</h3> * <h3>Working with non-option arguments</h3>
* <p>Any and all arguments specified at the command line without the "{@code --}" * <p>Any arguments following the "end of options" delimiter ({@code --}) or
* option prefix will be considered as "non-option arguments" and made available * specified without the "{@code --}" option prefix will be considered as
* through the {@link CommandLineArgs#getNonOptionArgs()} method. * "non-option arguments" and made available through the
* {@link CommandLineArgs#getNonOptionArgs()} method.
* *
* <h3>Typical usage</h3> * <h3>Typical usage</h3>
* <pre class="code"> * <pre class="code">

Loading…
Cancel
Save