class SimpleCommandLineArgsParser {
public CommandLineArgs parse(String... args) { // --priority1=program-agrs
CommandLineArgs commandLineArgs = new CommandLineArgs();
for (String arg : args) {
if (arg.startsWith("--")) { // 以--開頭
String optionText = arg.substring(2, arg.length()); //把--去掉,得到priority1=program-agrs
String optionName;
String optionValue = null;
if (optionText.contains("=")) { //判斷是否有=存在
optionName = optionText.substring(0, optionText.indexOf('=')); // 得到=之前的值,priority1
optionValue = optionText.substring(optionText.indexOf('=')+1, optionText.length()); //得到=之後的值,program-agrs
}
else {
optionName = optionText;
}
if (optionName.isEmpty() || (optionValue != null && optionValue.isEmpty())) {
throw new IllegalArgumentException("Invalid argument syntax: " + arg);
}
commandLineArgs.addOptionArg(optionName, optionValue); // key: string value: arraylist , Map<String, List<String>> optionArgs
}
else {
commandLineArgs.addNonOptionArg(arg);
}
}
return commandLineArgs;
}
}