main 方法的书写(2)

3、从参数列表中提取参数

代码
/**
* <p>
* Parses value from the argument.
* </p>
*
*
@param arg
* the argument.
*
@param key
* the switch key.
*
*
@return the parsed value.
*/
private static String parseArg(String arg, String key) {

String value
= arg.substring(key.length());

// Value cannot be empty
if (value.trim().length() == 0) {
printAndExit(
false, -1, null, "The value of switch '" + key
+ "' cannot be empty.");
}

return value;
}

4、常用变量的定义

代码

/**
* <p>
* Represents the switch '-config='.
* </p>
*/
private static final String SWITCH_CONFIG = "-config=";

/**
* <p>
* Represents the switch to print usage message.
* </p>
*/
private static final List<String> SWITCH_HELP = Arrays.asList(new String[]{
"-help", "-h"});
/**
* <p>
* Represents the line separator.
* </p>
*/
private static final String LINE_SEP = System.getProperty("line.separator");
原文地址:https://www.cnblogs.com/ITEagle/p/1779195.html