这个 CommandLinePropertySource 实现旨在提供最简单的方法来解析命令行参数。与所有 CommandLinePropertySource 实现一样,命令行参数被分成两个不同的组:选项参数和非选项参数,如下所述:
选项参数必须遵循确切的语法:
--optName[=optValue]
也就是说,选项必须以 “--” 作为前缀,并且可以或不指定值。如果指定了一个值,则名称和值必须用等号(“=”)分隔,不带空格。该值可以选择为空字符串。
选项参数的有效示例:
--foo --foo= --foo="" --foo=bar --foo="bar then baz" --foo=bar,baz,biz
选项参数的无效示例:
-foo --foo bar --foo = bar --foo=bar --foo=baz --foo=biz
在没有 “-” 选项前缀的命令行中指定的任何和所有参数都将被视为 “非选项参数”,并通过 CommandLineArgs.getnonOptionArgs() 方法提供。
public static void main(String[] args) { PropertySource ps = new SimpleCommandLinePropertySource(args); // ... }
有关完整的通用用法示例,请参见 CommandLinePropertySource。
当需要进行更全面的命令行解析时,请考虑使用提供的 JOptCommandLinePropertySource,或者在您选择的命令行解析库中实现您自己的 CommandLinePropertySource。
boolean containsOption(String name):返回从命令行解析的一组选项参数是否包含具有给定名称的选项。
List<String> getNonOptionArgs():返回从命令行解析的非选项参数的集合。
List<String> getOptionValues(String name):返回与具有给定名称的命令行选项关联的值集合。
String[] getPropertyNames():获取选项参数的属性名称。
(1)简单的使用 SimpleCommandLinePropertySource 解析选项参数,获取选项参数名称、值等。代码如下:
package com.springframework.core.env; import org.springframework.core.env.SimpleCommandLinePropertySource; /** * 测试 SimpleCommandLinePropertySource 功能 */ public class SimpleCommandLinePropertySourceTest { public static void main(String[] args) { // Invalid argument syntax: --message= String[] argsData = { "--name=ZhangSan", "--sex=male", "--pag", "--message=hello world", "/images/2020/10/10/face.img", "ChengDu" }; SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(argsData); System.out.println("\n获取选项参数名称:"); for(String optName : source.getPropertyNames()) { System.out.println(optName); } System.out.println("\n是否包含 name 选项参数:" + source.containsProperty("name")); System.out.println("\n获取 name 参数值:" + source.getProperty("name")); } }
运行上面程序输出结果如下:
获取选项参数名称: sex pag name message 是否包含 name 选项参数:true 获取 name 参数值:ZhangSan
(2)通过集成 SimpleCommandLinePropertySource 实现自定义 PropertySource,代码如下:
package com.springframework.core.env; import org.springframework.core.env.SimpleCommandLinePropertySource; import java.util.List; /** * 测试 SimpleCommandLinePropertySource 功能 */ public class SimpleCommandLinePropertySourceTest2 { // 继承 SimpleCommandLinePropertySource 自定义 PropertySource class MySimpleCommandLinePropertySource extends SimpleCommandLinePropertySource { public MySimpleCommandLinePropertySource(String... args) { super(args); } public List<String> getOptionValues(String name) { return super.getOptionValues(name); } public List<String> getNonOptionArgs() { return super.getNonOptionArgs(); } public boolean containsOption(String name) { return super.containsOption(name); } } public static void main(String[] args) { // Invalid argument syntax: --message= String[] argsData = { "--name=ZhangSan", "--sex=male", "--pag", "--message=hello world", "/images/2020/10/10/face.img", "ChengDu" }; SimpleCommandLinePropertySourceTest2 demo = new SimpleCommandLinePropertySourceTest2(); MySimpleCommandLinePropertySource source = demo.new MySimpleCommandLinePropertySource(argsData); System.out.println("\n获取选项参数名称:"); for(String optName : source.getPropertyNames()) { System.out.println(optName); } System.out.println("\n是否包含 name 选项参数:" + source.containsOption("name")); System.out.println("\n获取 name 参数值:" + source.getOptionValues("name")); // 获取非选项参数 System.out.println("\n获取非选项参数:"); for(String nonOpt : source.getNonOptionArgs()) { System.out.println(nonOpt); } } }
运行上面程序,输出结果如下:
获取选项参数名称: sex pag name message 是否包含 name 选项参数:true 获取 name 参数值:[ZhangSan] 获取非选项参数: /images/2020/10/10/face.img ChengDu