SimpleCommandLinePropertySource 类

本文将介绍 SimpleCommandLinePropertySource 类的用法

这个 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
我们一定不要当三等公民:等下班、等薪水、等退休。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号