Arguments--手敲hive源码第三天

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);
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值