package org.apache.hive.hplsql;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.cli.CommanLine; //commons-cli 提供的一种套完整的命令行解决方案
import org.apache.commons.cli.GnuParser; //https://blog.csdn.net/clebeg/article/details/38864967
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.OptionBulider;
import org.apache.commons.cli.ParseExceptin;
public class Arguments{
private CommandLine commandLine;
private Options options=new Options();
String execString;
String fileName;
String main;
Map<String,String> vars=new HashMap<String,String>();
@SuppressWarnings("static-access")
Arguments(){
// -e 'query'
options.addOption(OptionBuilder
.hasArg()
.withArgName("quoted-query-string")
.withDescription("HPL/SQL from command line")
.create('e'));
// -f <file>
options.addOption(OptionBuilder
.hasArg()
.withArgName("filename")
.withDescription("HPL/SQL from a file")
.create('f'));
// -main entry_point_name
options.addOption(OptionBuilder
.hasArg()
.withArgName("procname")
.withDescription("Entry point (procedure or function name"))
.create("main"));
// -hiveconf x=y
options.addOption(OptionBuilder
.withValueSeparator()
.hasArgs(2)
.withArgName("property=value")
.withLongOpt("hiveconf")
.withDescription("Value for giver property")
.create());
// Substitution option -d, --define
options.addOption(OptionBuilder
.withValueSeparator()
.hasArgs(2)
.withArgName("key=value")
.withLongOpt("define")
.withDescription("Variable substitution e.g. -d A=B or --define A=B")
.create('d'));
// [-version|--version]
options.addOption(new Option("version","version",false,"Print HPL/SQL version"));
// [-trace|--trace]
options.addOption(new Option("trace","trace",false,"Print debug information"));
// [-offline|--offline]
options.addOption(new Option("offline","offline", false,"Offline mode - skip SQL execution"));
// [-H--help]
options.addOption(new Option("H","help",false,"Print help information"));
}
/**
*Parse the command line arguments
*/
public boolean parse(String[] ages){
try{
commandLine=new GnuParser().parse(options,args);
exexString=commandLine.getOptionValue('e');
fileNam=commandLine.getOptionValue('f');
main=commandLine.getOptionValue("main");
Properties p=commandLine.getOptionPropeerties("hiveconf");
for(String key:p.stringPropertyNames()){
vars.put(key,p.getProperty(key));
}
p=commandLine.getOptionProperties("hivevar");
for(String key:p.stringPropertyNames()){
vars.put(key,p.getProperty(key));
}
p=commandLine.getOptionProperties("define");
for(String key:p.stringPropertyNames()){
vars.put(key,p.getProperty(key));
}
}catch(ParseException e){ //参数异常
System.err.println(e.getMessage());
return false;
}
return true;
}
/**
*Get the value of execution option -e
*/
public String getExecString(){
return exeecString;
}
/**
* Get the value of file option -f
*/
public String getFileName(){
return fileName;
}
/**
*Get the value of -main option
*/
public String getMain(){
return main;
}
/**
*Get the variables
*/
public Map<String,String>getVars(){
return vars;
}
/**
*Test whether version option is set
*/
public boolean hasVersionOption(){
if(commandLine.hasOption("version")){
return true;
}
return false;
}
/**
*Test wher help option is set
*/
public boolean hasHelpOption(){
if(commandLine.hasOption('H')){
return true;
}
return false;
}
/**
*
*/
public void printHelp(){
new HelpFormatter().printHelp("hplsql",options);
}
}
Arguments--手敲hive源码第三天
猜你喜欢
转载自blog.csdn.net/weixin_42474635/article/details/89515581
今日推荐
周排行