Java 参数包

简介

中大型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 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/cyno/p/4448293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值