SimpleCommandLinePropertySource class

The property source over the program's command-line arguments, so that --server.port=9090 sets the property server.port.

It is why a setting can be overridden at launch without touching the config files: placed first in the environment, it wins over every other source. SpringApplication adds it for you from the args of main, so an application only writes the arguments; it creates one itself only when it builds a StandardEnvironment without SpringApplication. Arguments are parsed like this:

  • --key=value sets key; only the first = separates.
  • --flag alone sets flag to an empty string.
  • A repeated option keeps all its values, joined with commas when read.
  • Other arguments are non-option arguments, readable as nonOptionArgs or as the comma-joined property nonOptionArgsPropertyName.
  • A lone -- ends the options: everything after it is a non-option argument, as in Spring.
// A tool without SpringApplication that still takes `--key=value`.
Future<void> main(List<String> args) async {
  // args: --server.port=9090 --debug input.txt
  final environment = StandardEnvironment()
    ..propertySources.addFirst(SimpleCommandLinePropertySource(args));
  environment.getPropertyAs<int>('server.port'); // 9090
  environment.containsProperty('debug'); // true
}

Spring (Java):

public static void main(String[] args) {
  var environment = new StandardEnvironment();
  environment.getPropertySources()
      .addFirst(new SimpleCommandLinePropertySource(args));
  Integer port = environment.getProperty("server.port", Integer.class);
}

Spring Boot's SpringApplication.run adds this source automatically, and so does SpringApplication here.

Inheritance

Constructors

SimpleCommandLinePropertySource(List<String> args, {String name = defaultName})
Parses args into a source called name (commandLineArgs unless given). Throws ArgumentError for an option without a name (--=value).

Properties

hashCode → int
The hash code for this object.
no setterinherited
name → String
The name of this source, unique within the environment's list. It is how other code finds, replaces or orders the source, and it appears in error messages (Config resource 'classpath:/application.yml').
finalinherited
nonOptionArgs → List<String>
The arguments that are not --options (file names, sub-commands), in order.
final
propertyNames → List<String>
The keys of all properties this source has, as they are written in it.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
source → Map<String, List<String>>
The object the values are read from: a map, the command-line arguments, a client.
finalinherited

Methods

containsProperty(String name) → bool
Whether propertyNames contains name. Override it with a faster lookup when the source has one.
override
getOptionValues(String name) → List<String>?
Every value given for the option name, in order, or null when it was not given. Use it for options that may repeat; reading the property gives them comma-joined instead.
getProperty(String name) → Object?
The value of the property name, or null when this source does not have it (the environment then asks the next source).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
Whether other is a source with the same name, whatever its values, as in Spring.
inherited

Constants

defaultName → const String
The name the source has unless another is given: commandLineArgs.
nonOptionArgsPropertyName → const String
The property that holds the non-option arguments, comma-joined: nonOptionArgs.