1 Star 0 Fork 156

初雪12 / Spring-Analysis

forked from huifer / Code-Analysis 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Spring-SimpleCommandLineArgsParser.md 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
huifer 提交于 2020-11-09 10:08 . :fire:doc: env + PropertyResolver

Spring SimpleCommandLineArgsParser

  • Author: HuiFer

  • 源码阅读仓库: SourceHot-spring

  • 类全路径: `org.springframework.core.env.SimpleCommandLineArgsParser

  • 类作用: 将命令行参数解析成 org.springframework.core.env.CommandLineArgs

  • 完整代码如下.


class SimpleCommandLineArgsParser {

	/**
	 * Parse the given {@code String} array based on the rules described {@linkplain
	 * SimpleCommandLineArgsParser above}, returning a fully-populated
	 * {@link CommandLineArgs} object.
	 * @param args command line arguments, typically from a {@code main()} method
	 */
	public CommandLineArgs parse(String... args) {
		CommandLineArgs commandLineArgs = new CommandLineArgs();
		for (String arg : args) {
			if (arg.startsWith("--")) {
				String optionText = arg.substring(2, arg.length());
				String optionName;
				String optionValue = null;
				if (optionText.contains("=")) {
					optionName = optionText.substring(0, optionText.indexOf('='));
					optionValue = optionText.substring(optionText.indexOf('=') + 1, optionText.length());
				}
				else {
					optionName = optionText;
				}
				if (optionName.isEmpty() || (optionValue != null && optionValue.isEmpty())) {
					throw new IllegalArgumentException("Invalid argument syntax: " + arg);
				}
				commandLineArgs.addOptionArg(optionName, optionValue);
			}
			else {
				commandLineArgs.addNonOptionArg(arg);
			}
		}
		return commandLineArgs;
	}

}
  • 处理流程
    1. 循环命令行参数列表
    2. 去掉 --= 获取参数名称和参数值放入结果集合
Java
1
https://gitee.com/chuxue12/spring-analysis.git
git@gitee.com:chuxue12/spring-analysis.git
chuxue12
spring-analysis
Spring-Analysis
master

搜索帮助