今天刚刚开始学习mahout源码中的movielens的example,在获取DataModel时,用到了 TasteOptionParser.getRatings静态方法,是来自类 TasteOptionParser。。用于输入给main方法的args参数来给文件路径,但是注意好像路径中不能含有空格。。。
步骤
1、创建builder
DefaultOptionBuilder 创建默认选项
ArgumentBuilder 创建参数实例
GroupBuilder 创建组实例
2、创建option
inputOpt
helpOpt
表示输入、帮助选项
3、将几个options建立成组
4、建立解析 parser
5、将已建好的组添加到parser中
6、获取命令行参数 CommandLine
7、利用CommandLine对象判断是否存在对应的选项 进行对应的操作
代码:
package org.apache.mahout.cf.taste.example;
import java.io.File;
import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.Option;
import org.apache.commons.cli2.OptionException;
import org.apache.commons.cli2.builder.ArgumentBuilder;
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
import org.apache.commons.cli2.builder.GroupBuilder;
import org.apache.commons.cli2.commandline.Parser;
import org.apache.mahout.common.CommandLineUtil;
import org.apache.mahout.common.commandline.DefaultOptionCreator;
/**
* This class provides a common implementation for parsing input parameters for
* all taste examples. Currently they only need the path to the recommendations
* file as input.
*
* The class is safe to be used in threaded contexts.
*/
public final class TasteOptionParser {
private TasteOptionParser() {
}
/**
* Parse the given command line arguments.
* @param args the arguments as given to the application.
* @return the input file if a file was given on the command line, null otherwise.
*/
public static File getRatings(String[] args) throws OptionException {
DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
ArgumentBuilder abuilder = new ArgumentBuilder();
GroupBuilder gbuilder = new GroupBuilder();
Option inputOpt = obuilder.withLongName("input").withRequired(false).withShortName("i")
.withArgument(abuilder.withName("input").withMinimum(1).withMaximum(1).create())
.withDescription("The Path for input data directory.").create();
Option helpOpt = DefaultOptionCreator.helpOption();
Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(helpOpt).create();
Parser parser = new Parser();
parser.setGroup(group);
CommandLine cmdLine = parser.parse(args);
if (cmdLine.hasOption(helpOpt)) {
CommandLineUtil.printHelp(group);
return null;
}
return cmdLine.hasOption(inputOpt) ? new File(cmdLine.getValue(inputOpt).toString()) : null;
}
}
解析:
1、
Option inputOpt = obuilder.withLongName("input").withRequired(false).withShortName("i")
.withArgument(abuilder.withName("input").withMinimum(1).withMaximum(1).create())
.withDescription("The Path for input data directory.").create();
表示 输入文件路径命令 长命令--input 或者 短命令 -i withRequierd==false 表示 该选项不一定在运行时一定要输入的参数
具体DefaultOptionBuilder的方法请参考apache对应的解析
2、如何在myeclipse中输入args参数给main方法
选中要输入args参数的文件,单击右键选择run as-run configurations---(x=)Arguments--Program arguments 即输入给main方法参数的位置
此时 我选择在该包 下新建一个java文件,使用该方法
package org.apache.mahout.cf.taste.example;
import org.apache.commons.cli2.OptionException;
import org.apache.mahout.cf.taste.example.TasteOptionParser;
import java.io.*;
public class TasteOptionParserTest {
public static void main(String []args)throws OptionException
{
File file=TasteOptionParser.getRatings(args);
/* //File file=new File(args[0].toString());*/
if(file==null)
{
System.out.println("file not exist");
}
else
System.out.println(file.getPath());
}
}
在program arguments中输入 -h 或者 --help 得到所有命令的具体介绍 如下所示
Usage:
[--input <input> --help]
Options
--input (-i) input The Path for input data directory.
--help (-h) Print out help
输入 --input 或者 -i + 输入文件路径
学习了~~~