类 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>
CommandLinePropertySourceimplementation backed by a simple String array.Purpose
This
CommandLinePropertySourceimplementation aims to provide the simplest possible approach to parsing command line arguments. As with allCommandLinePropertySourceimplementations, command line arguments are broken into two distinct groups: option arguments and non-option arguments, as described below (some sections copied from Javadoc forSimpleCommandLineArgsParser):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 theCommandLineArgs.getNonOptionArgs()method.Typical usage
public static void main(String[] args) { PropertySource> ps = new SimpleCommandLinePropertySource(args); // ... }SeeCommandLinePropertySourcefor complete general usage examples.Beyond the basics
When more fully-featured command line parsing is necessary, consider using the provided
JOptCommandLinePropertySource, or implement your ownCommandLinePropertySourceagainst the command line parsing library of your choice.- 从以下版本开始:
- 3.1
- 作者:
- Chris Beams
- 另请参阅:
CommandLinePropertySource,JOptCommandLinePropertySource
嵌套类概要
从类继承的嵌套类/接口 org.springframework.core.env.PropertySource
PropertySource.StubPropertySource
字段概要
从类继承的字段 org.springframework.core.env.CommandLinePropertySource
COMMAND_LINE_PROPERTY_SOURCE_NAME, DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME
从类继承的字段 org.springframework.core.env.PropertySource
logger, name, source
构造器概要
构造器 构造器 说明 SimpleCommandLinePropertySource(String... args)Create a newSimpleCommandLinePropertySourcehaving the default name and backed by the givenString[]of command line arguments.SimpleCommandLinePropertySource(String name, String[] args)Create a newSimpleCommandLinePropertySourcehaving the given name and backed by the givenString[]of command line arguments.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected booleancontainsOption(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.从类继承的方法 org.springframework.core.env.CommandLinePropertySource
containsProperty, getProperty, setNonOptionArgsPropertyName
构造器详细资料
SimpleCommandLinePropertySource
public SimpleCommandLinePropertySource(String... args)
Create a newSimpleCommandLinePropertySourcehaving the default name and backed by the givenString[]of command line arguments.
SimpleCommandLinePropertySource
public SimpleCommandLinePropertySource(String name, String[] args)
Create a newSimpleCommandLinePropertySourcehaving the given name and backed by the givenString[]of command line arguments.
方法详细资料
getPropertyNames
public String[] getPropertyNames()
Get the property names for the option arguments.- 指定者:
getPropertyNames在类中EnumerablePropertySource<org.springframework.core.env.CommandLineArgs>
containsOption
protected boolean containsOption(String name)
从类复制的说明:CommandLinePropertySourceReturn whether the set of option arguments parsed from the command line contains an option with the given name.- 指定者:
containsOption在类中CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>
getOptionValues
@Nullable protected List<String> getOptionValues(String name)
从类复制的说明:CommandLinePropertySourceReturn 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
- 指定者:
getOptionValues在类中CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>
- if the option is present and has no argument (e.g.: "--foo"), return an empty collection (
getNonOptionArgs
protected List<String> getNonOptionArgs()
从类复制的说明:CommandLinePropertySourceReturn the collection of non-option arguments parsed from the command line. Nevernull.- 指定者:
getNonOptionArgs在类中CommandLinePropertySource<org.springframework.core.env.CommandLineArgs>