PageRenderTime 23ms CodeModel.GetById 6ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/release-0.1-rc2/hive/external/cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java

#
Java | 141 lines | 88 code | 25 blank | 28 comment | 10 complexity | 0307174e5943580fe31010a12caa373f MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.cli;
 20
 21import java.util.Arrays;
 22import java.util.Properties;
 23
 24import org.apache.commons.cli.GnuParser;
 25import org.apache.commons.cli.HelpFormatter;
 26import org.apache.commons.cli.Option;
 27import org.apache.commons.cli.OptionBuilder;
 28import org.apache.commons.cli.Options;
 29import org.apache.commons.cli.ParseException;
 30import org.apache.commons.logging.Log;
 31import org.apache.commons.logging.LogFactory;
 32
 33/**
 34 * OptionsProcessor.
 35 *
 36 */
 37public class OptionsProcessor {
 38  protected static final Log l4j = LogFactory.getLog(OptionsProcessor.class.getName());
 39  private final Options options = new Options();
 40  private org.apache.commons.cli.CommandLine commandLine;
 41
 42
 43  @SuppressWarnings("static-access")
 44  public OptionsProcessor() {
 45
 46    // -e 'quoted-query-string'
 47    options.addOption(OptionBuilder
 48        .hasArg()
 49        .withArgName("quoted-query-string")
 50        .withDescription("SQL from command line")
 51        .create('e'));
 52
 53    // -f <query-file>
 54    options.addOption(OptionBuilder
 55        .hasArg()
 56        .withArgName("filename")
 57        .withDescription("SQL from files")
 58        .create('f'));
 59
 60    // -i <init-query-file>
 61    options.addOption(OptionBuilder
 62        .hasArg()
 63        .withArgName("filename")
 64        .withDescription("Initialization SQL file")
 65        .create('i'));
 66
 67    // -hiveconf x=y
 68    options.addOption(OptionBuilder
 69        .withValueSeparator()
 70        .hasArgs(2)
 71        .withArgName("property=value")
 72        .withLongOpt("hiveconf")
 73        .withDescription("Use value for given property")
 74        .create());
 75
 76    // [-S|--silent]
 77    options.addOption(new Option("S", "silent", false, "Silent mode in interactive shell"));
 78
 79    // [-v|--verbose]
 80    options.addOption(new Option("v", "verbose", false, "Verbose mode (echo executed SQL to the console)"));
 81
 82    // [-h|--help]
 83    options.addOption(new Option("h", "help", false, "Print help information"));
 84  }
 85
 86  public boolean process_stage1(String[] argv) {
 87    try {
 88      commandLine = new GnuParser().parse(options, argv);
 89      Properties confProps = commandLine.getOptionProperties("hiveconf");
 90      for (String propKey : confProps.stringPropertyNames()) {
 91        System.setProperty(propKey, confProps.getProperty(propKey));
 92      }
 93    } catch (ParseException e) {
 94      System.err.println(e.getMessage());
 95      printUsage();
 96      return false;
 97    }
 98    return true;
 99  }
100
101  public boolean process_stage2(CliSessionState ss) {
102    ss.getConf();
103
104    if (commandLine.hasOption('h')) {
105      printUsage();
106      return false;
107    }
108
109    ss.setIsSilent(commandLine.hasOption('S'));
110
111    ss.execString = commandLine.getOptionValue('e');
112
113    ss.fileName = commandLine.getOptionValue('f');
114
115    ss.setIsVerbose(commandLine.hasOption('v'));
116
117    String[] initFiles = commandLine.getOptionValues('i');
118    if (null != initFiles) {
119      ss.initFiles = Arrays.asList(initFiles);
120    }
121
122    if (ss.execString != null && ss.fileName != null) {
123      System.err.println("The '-e' and '-f' options cannot be specified simultaneously");
124      printUsage();
125      return false;
126    }
127
128    if (commandLine.hasOption("hiveconf")) {
129      Properties confProps = commandLine.getOptionProperties("hiveconf");
130      for (String propKey : confProps.stringPropertyNames()) {
131        ss.cmdProperties.setProperty(propKey, confProps.getProperty(propKey));
132      }
133    }
134
135    return true;
136  }
137
138  private void printUsage() {
139    new HelpFormatter().printHelp("hive", options);
140  }
141}