Package org.springframework.core.env

# Class SimpleCommandLinePropertySource

java.lang.Object
org.springframework.core.env.PropertySource<T>
org.springframework.core.env.EnumerablePropertySource<T>
org.springframework.core.env.CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>
org.springframework.core.env.SimpleCommandLinePropertySource

public class SimpleCommandLinePropertySource extends CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>
`CommandLinePropertySource` implementation backed by an instance of `CommandLineArgs`.

### 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. If an option is present multiple times with different values — for example, `--foo=bar --foo=baz` — all supplied values will be stored for the option.

#### Valid examples of option arguments

     --foo
     --foo=
     --foo=""
     --foo=bar
     --foo="bar then baz"
     --foo=bar,baz,biz
     --foo=bar --foo=baz --foo=biz

#### Invalid examples of option arguments

     -foo
     --foo bar
     --foo = bar

### End of option arguments

The underlying parser supports the POSIX "end of options" delimiter, meaning that any `"--"` (empty option name) in the command line signals that all remaining arguments are non-option arguments. For example, `"--opt1=ignored"`, `"--opt2"`, and `"filename"` in the following command line are considered non-option arguments.

     --foo=bar -- --opt1=ignored -opt2 filename

### Working with non-option arguments

Any arguments following the "end of options" delimiter (`--`) or specified 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.

Since:
  3.1
Author:
  Chris Beams
See Also:
  - `CommandLinePropertySource`

## Nested Class Summary

## Nested classes/interfaces inherited from class org.springframework.core.env.PropertySource

`PropertySource.StubPropertySource`

## Field Summary

### Fields inherited from class org.springframework.core.env.CommandLinePropertySource

`COMMAND_LINE_PROPERTY_SOURCE_NAME, DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME`

### Fields inherited from class org.springframework.core.env.PropertySource

`logger, name, source`

## Constructor Summary

Constructors
Constructor
Description
`SimpleCommandLinePropertySource(String... args)`
Create a new `SimpleCommandLinePropertySource` having the default name and backed by the given `String[]` of command line arguments.
`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 Type
Method
Description
`protected boolean`
`containsOption(String name)`
Return whether the set of option arguments parsed from the command line contains an option with the given name.
`protected List<String>`
`getNonOptionArgs()`
Return the collection of non-option arguments parsed from the command line.
`protected List<String>`
`getOptionValues(String name)`
Return the collection of values associated with the command line option having the given name.
`String[]`
`getPropertyNames()`
Get the property names for the option arguments.

### Methods inherited from class org.springframework.core.env.CommandLinePropertySource

`containsProperty, getProperty, setNonOptionArgsPropertyName`

### Methods inherited from class org.springframework.core.env.PropertySource

`equals, getName, getSource, hashCode, named, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## Constructor Details

### SimpleCommandLinePropertySource

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(Object)`

### SimpleCommandLinePropertySource

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 Details

### getPropertyNames

public String[] getPropertyNames()
Get the property names for the option arguments.

Specified by:
  `getPropertyNames` in class `EnumerablePropertySource<org.springframework.core.env.CommandLineArgs>`

### containsOption

protected boolean containsOption(String name)
Description copied from class: `CommandLinePropertySource`
Return whether the set of option arguments parsed from the command line contains an option with the given name.

Specified by:
  `containsOption` in class `CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>`

### getOptionValues

@Nullable protected List<String> getOptionValues(String name)
Description copied from class: `CommandLinePropertySource`
Return the collection of values associated with the command line option having the given name.

- if the option is present and has no argument (for example: "--foo"), return an empty collection (`[]`)
- if the option is present and has a single value (for example, "--foo=bar"), return a collection having one element (`["bar"]`)
- if the option is present and the underlying command line parsing library supports multiple arguments (for example, "--foo=bar --foo=baz"), return a collection having elements for each value (`["bar", "baz"]`)
- if the option is not present, return `null`

Specified by:
  `getOptionValues` in class `CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>`

### getNonOptionArgs

protected List<String> getNonOptionArgs()
Description copied from class: `CommandLinePropertySource`
Return the collection of non-option arguments parsed from the command line. Never `null`.

Specified by:
  `getNonOptionArgs` in class `CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>`