spring-framework / org.springframework.core.env / SimpleCommandLinePropertySource

SimpleCommandLinePropertySource

open class SimpleCommandLinePropertySource : CommandLinePropertySource<CommandLineArgs>

CommandLinePropertySource implementation backed by a simple String array. Purpose This CommandLinePropertySource implementation aims to provide the simplest possible approach to parsing command line arguments. As with all CommandLinePropertySource implementations, command line arguments are broken into two distinct groups: option arguments and non-option arguments, as described below (some sections copied from Javadoc for SimpleCommandLineArgsParser): Working with option arguments Option arguments must adhere to the exact syntax:

--optName[=optValue]
That is, options must be prefixed with "--", and may or may not specify a value. If a value is specified, the name and value must be separated without spaces by an equals sign ("="). Valid examples of option arguments
 --foo --foo=bar --foo="bar then baz" --foo=bar,baz,biz
Invalid examples of option arguments
 -foo --foo bar --foo = bar --foo=bar --foo=baz --foo=biz
Working with non-option arguments Any and all arguments specified at the command line without the "--" option prefix will be considered as "non-option arguments" and made available through the #getNonOptionArgs() method. Typical usage
 public static void main(String[] args) { PropertySource ps = new SimpleCommandLinePropertySource(args); // ... }
See CommandLinePropertySource for complete general usage examples. Beyond the basics

When more fully-featured command line parsing is necessary, consider using the provided JOptCommandLinePropertySource, or implement your own CommandLinePropertySource against the command line parsing library of your choice!

Author
Chris Beams

Since
3.1

See Also
CommandLinePropertySourceJOptCommandLinePropertySource

Constructors

<init>

SimpleCommandLinePropertySource(vararg args: String)

Create a new SimpleCommandLinePropertySource having the default name and backed by the given String[] of command line arguments.

SimpleCommandLinePropertySource(name: String, args: Array<String>)

Create a new SimpleCommandLinePropertySource having the given name and backed by the given String[] of command line arguments.

Inherited Properties

COMMAND_LINE_PROPERTY_SOURCE_NAME

static val COMMAND_LINE_PROPERTY_SOURCE_NAME: String

The default name given to CommandLinePropertySource instances: {@value}

DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME

static val DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME: String

The default name of the property representing non-option arguments: {@value}

Functions

getPropertyNames

open fun getPropertyNames(): Array<String>

Get the property names for the option arguments.

Inherited Functions

containsProperty

fun containsProperty(name: String): Boolean

This implementation first checks to see if the name specified is the special "non-option arguments" property, and if so delegates to the abstract #getNonOptionArgs() method checking to see whether it returns an empty collection. Otherwise delegates to and returns the value of the abstract #containsOption(String) method.

getProperty

fun getProperty(name: String): String

This implementation first checks to see if the name specified is the special "non-option arguments" property, and if so delegates to the abstract #getNonOptionArgs() method. If so and the collection of non-option arguments is empty, this method returns null. If not empty, it returns a comma-separated String of all non-option arguments. Otherwise delegates to and returns the result of the abstract method.

setNonOptionArgsPropertyName

open fun setNonOptionArgsPropertyName(nonOptionArgsPropertyName: String): Unit

Specify the name of the special "non-option arguments" property. The default is {@value #DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME}.