net.gdface.cli.AbstractConfiguration Maven / Gradle / Ivy
package net.gdface.cli;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.TypeHandler;
/**
* 参数配置抽象类
* @author guyadong
*
*/
public abstract class AbstractConfiguration extends Context implements CommonCliConstants, CmdConfig {
/**
* 是否使用 trace 选项,为{@code true}时会将'{@code -X --trace}'自动添加到命令行参数定义中
*/
protected final boolean withTrace;
@SuppressWarnings("serial")
protected final Set CONTROL_OPTIONS = new HashSet() {
{
this.add(HELP_OPTION);
this.add(HELP_OPTION_LONG);
this.add(DEFINE_OPTION);
}
};
private final Map fields;
/** 子类提供命令行参数的默认值 */
protected abstract Map getDefaultValueMap();
protected AbstractConfiguration() {
this(false);
}
/**
* 构造方法
* @param withTrace 是否使用 trace 选项,为{@code true}时会将'{@code -X --trace}'自动添加到命令行参数定义中
*/
protected AbstractConfiguration(boolean withTrace) {
this.withTrace = withTrace;
if(withTrace){
CONTROL_OPTIONS.add(TRACE_OPTION);
CONTROL_OPTIONS.add(TRACE_OPTION_LONG);
}
fields = initFields();
}
@Override
public void loadConfig(Options options, CommandLine cmd) throws ParseException {
Iterator