cynorr

Learn what I touched.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

简介

中大型Java项目普遍需要调配许多参数,本文引入参数包,用系统快捷的方法管理参数。效果如下:

Poster	[options...]	[arguments...]
 -dictFile VAL       : Specify file of dictionary
 -filterLemmaCount N : Specify the least sum of leammas each word
 -filterLine N       : Specify the least sum of words tagged each line
 -length N           : Specify the sum of source lines
 -sourceFile VAL     : Specify source corpus 
 -workPath VAL       : Specify path of workspace 

外接包

  • 1.下载 args4j-2.0.6.jar 放到工程lib文件夹下.
  • 2.Build Path -> Add External Archives -> 选中项目lib下的args4j包.

使用

  • 1.Options类
package poster;

import org.kohsuke.args4j.*;  //导入包

public class Options{
	
	
	@Option(name = "-workPath", usage = "Specify path of workspace ")
	public String workPath = "/home/cyno/corpus/large2/";
	
	@Option(name = "-sourceFile", usage = "Specify source corpus ")
	public String sourceFile = "large2.en";
	
	@Option(name = "-dictFile", usage = "Specify file of dictionary")
	public String dictFile = "index.adj";
	
	@Option(name = "-length", usage = "Specify the sum of source lines")
	public Integer length = 100000;
	
	@Option(name = "-filterLine", usage = "Specify the least sum of words tagged each line")
	public Integer filterLine = 4;
	
	@Option(name = "-filterLemmaCount", usage = "Specify the least sum of leammas each word")
	public Integer filterLemmaCount = 10;
		
}
  • 2.Main调用
package poster;

import org.kohsuke.args4j.*;

public class Poster{
	public static void main(String [] args){
		
		Options option = new Options();
		CmdLineParser parser = new CmdLineParser(option);
		
		if (args.length == 0){
			System.out.println("Poster\t[options...]\t[arguments...]");
			parser.printUsage(System.out);
			return;
		}
		
             // ...........
	}
}
  • 3.参数使用
public boolean init(Options option) {
		
		if (!option.workPath.endsWith(File.separator)){
			option.workPath += File.separator;
		}
		this.option = option;
		this.sourceFile = option.workPath + option.sourceFile;
		this.length = option.length;
		this.filter = option.filterLine;
		this.coreFile = option.workPath + "core.dat";
		this.withOrderFile = option.workPath + "withOrder.dat";
		this.lemmaPath = option.workPath + "lemmas/";
		try{
			File lemmaPathFile = new File(this.lemmaPath);
			if (!lemmaPathFile.isDirectory()){
				lemmaPathFile.mkdirs();
			}
		}catch(Exception e){
			System.out.println("Error When build lemma path!" + e.getMessage());
			e.printStackTrace();
		}
		
		
		// Read Dictionary
		dict.init(option.workPath+option.dictFile, option.workPath+"wordList.dat", option.filterLemmaCount);
		dict.readDict();

		return true;
	}
posted on 2015-04-22 18:22  cynorr  阅读(355)  评论(0编辑  收藏  举报