Top Description Fields Constructors Methods
org.springframework.core.env

public Class SimpleCommandLinePropertySource

extends CommandLinePropertySource<CommandLineArgs>
Class Inheritance
Imports
java.util.List, org.springframework.lang.Nullable, org.springframework.util.StringUtils

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 ("="). The value may optionally be an empty string.

Valid examples of option arguments

--foo
--foo=
--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 CommandLineArgs#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 implementing your own CommandLinePropertySource against the command line parsing library of your choice.

Author
Chris Beams
Since
3.1
See Also
CommandLinePropertySource

Field Summary

Inherited from org.springframework.core.env.CommandLinePropertySource:
COMMAND_LINE_PROPERTY_SOURCE_NAMEDEFAULT_NON_OPTION_ARGS_PROPERTY_NAME

Constructor Summary

AccessConstructor and Description
public
SimpleCommandLinePropertySource(String... args)

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

public
SimpleCommandLinePropertySource(String name, String[] args)

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

Method Summary

Modifier and TypeMethod and Description
protected boolean
containsOption(String name)

Implements abstract org.springframework.core.env.CommandLinePropertySource.containsOption.

Return whether the set of option arguments parsed from the command line contains an option with the given name.
protected List<String>
getNonOptionArgs()

Implements abstract org.springframework.core.env.CommandLinePropertySource.getNonOptionArgs.

Return the collection of non-option arguments parsed from the command line.
protected List<String>
getOptionValues(String name)

Implements abstract org.springframework.core.env.CommandLinePropertySource.getOptionValues.

Return the collection of values associated with the command line option having the given name.
public String[]
getPropertyNames()

Implements abstract org.springframework.core.env.EnumerablePropertySource.getPropertyNames.

Get the property names for the option arguments.
Inherited from org.springframework.core.env.CommandLinePropertySource:
containsPropertygetPropertysetNonOptionArgsPropertyName

Constructor Detail

SimpleCommandLinePropertySourceback to summary
public SimpleCommandLinePropertySource(String... args)

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

See Also
CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME, CommandLinePropertySource#CommandLinePropertySource(Object)
SimpleCommandLinePropertySourceback to summary
public SimpleCommandLinePropertySource(String name, String[] args)

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

Method Detail

containsOptionback to summary
protected boolean containsOption(String name)

Implements abstract org.springframework.core.env.CommandLinePropertySource.containsOption.

Doc from org.springframework.core.env.CommandLinePropertySource.containsOption.

Return whether the set of option arguments parsed from the command line contains an option with the given name.

Annotations
@Override
getNonOptionArgsback to summary
protected List<String> getNonOptionArgs()

Implements abstract org.springframework.core.env.CommandLinePropertySource.getNonOptionArgs.

Doc from org.springframework.core.env.CommandLinePropertySource.getNonOptionArgs.

Return the collection of non-option arguments parsed from the command line. Never null.

Annotations
@Override
getOptionValuesback to summary
protected List<String> getOptionValues(String name)

Implements abstract org.springframework.core.env.CommandLinePropertySource.getOptionValues.

Doc from org.springframework.core.env.CommandLinePropertySource.getOptionValues.

Return the collection of values associated with the command line option having the given name.

  • if the option is present and has no argument (e.g.: "--foo"), return an empty collection ([])
  • if the option is present and has a single value (e.g. "--foo=bar"), return a collection having one element (["bar"])
  • if the option is present and the underlying command line parsing library supports multiple arguments (e.g. "--foo=bar --foo=baz"), return a collection having elements for each value (["bar", "baz"])
  • if the option is not present, return null
Annotations
@Override
@Nullable
getPropertyNamesback to summary
public String[] getPropertyNames()

Implements abstract org.springframework.core.env.EnumerablePropertySource.getPropertyNames.

Get the property names for the option arguments.

Annotations
@Override