日期:2014-05-16  浏览次数:20888 次

Apache Commons CLI 的运用

在学习Mahout中Kmeans源码中,了解了大概的结构,了解到对于输入接口,Mahout应该是在apache的Options(org.apache.commons.cli)基础上,实现了自己的Options(org.apache.commons.cli2)。查阅相关资料,大致了解了apache的Options,写了个简单的例子。

?

package Etl;


import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public final class DefaultOptionCreator {
	public static final String INPUT_OPTION = "input";
	
	public static final String OUTPUT_OPTION = "output";
	
	private DefaultOptionCreator(){}
	
	@SuppressWarnings("static-access")
	public static OptionBuilder inputOption(){
	    return OptionBuilder.withLongOpt(INPUT_OPTION)
	    .isRequired(false)
	    .withArgName(INPUT_OPTION)
	    .hasArg()
	    .withDescription("The path of the input file");
	}
	//test-main
	@SuppressWarnings("static-access")
	public static void main(String []args){
		Options opts = new Options();
		opts.addOption(DefaultOptionCreator.inputOption().create("i"));
		BasicParser parser = new BasicParser();
		CommandLine cl;
		try{
			cl = parser.parse(opts, args);
			String inputpath = cl.getOptionValue("i");
			System.out.println(inputpath);
		}catch (ParseException e) { 
			 e.printStackTrace(); 
		 } 
	}
}

?命令行运行:

java -jar *.jar -i jiufehgui

?

?

?

?

参考资料:

?

使用 Apache Commons CLI 开发命令行工具

Apache Commons