PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.cli;
  19. import java.util.Arrays;
  20. import java.util.Properties;
  21. import org.apache.commons.cli.GnuParser;
  22. import org.apache.commons.cli.HelpFormatter;
  23. import org.apache.commons.cli.Option;
  24. import org.apache.commons.cli.OptionBuilder;
  25. import org.apache.commons.cli.Options;
  26. import org.apache.commons.cli.ParseException;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. /**
  30. * OptionsProcessor.
  31. *
  32. */
  33. public class OptionsProcessor {
  34. protected static final Log l4j = LogFactory.getLog(OptionsProcessor.class.getName());
  35. private final Options options = new Options();
  36. private org.apache.commons.cli.CommandLine commandLine;
  37. @SuppressWarnings("static-access")
  38. public OptionsProcessor() {
  39. // -e 'quoted-query-string'
  40. options.addOption(OptionBuilder
  41. .hasArg()
  42. .withArgName("quoted-query-string")
  43. .withDescription("SQL from command line")
  44. .create('e'));
  45. // -f <query-file>
  46. options.addOption(OptionBuilder
  47. .hasArg()
  48. .withArgName("filename")
  49. .withDescription("SQL from files")
  50. .create('f'));
  51. // -i <init-query-file>
  52. options.addOption(OptionBuilder
  53. .hasArg()
  54. .withArgName("filename")
  55. .withDescription("Initialization SQL file")
  56. .create('i'));
  57. // -hiveconf x=y
  58. options.addOption(OptionBuilder
  59. .withValueSeparator()
  60. .hasArgs(2)
  61. .withArgName("property=value")
  62. .withLongOpt("hiveconf")
  63. .withDescription("Use value for given property")
  64. .create());
  65. // [-S|--silent]
  66. options.addOption(new Option("S", "silent", false, "Silent mode in interactive shell"));
  67. // [-v|--verbose]
  68. options.addOption(new Option("v", "verbose", false, "Verbose mode (echo executed SQL to the console)"));
  69. // [-h|--help]
  70. options.addOption(new Option("h", "help", false, "Print help information"));
  71. }
  72. public boolean process_stage1(String[] argv) {
  73. try {
  74. commandLine = new GnuParser().parse(options, argv);
  75. Properties confProps = commandLine.getOptionProperties("hiveconf");
  76. for (String propKey : confProps.stringPropertyNames()) {
  77. System.setProperty(propKey, confProps.getProperty(propKey));
  78. }
  79. } catch (ParseException e) {
  80. System.err.println(e.getMessage());
  81. printUsage();
  82. return false;
  83. }
  84. return true;
  85. }
  86. public boolean process_stage2(CliSessionState ss) {
  87. ss.getConf();
  88. if (commandLine.hasOption('h')) {
  89. printUsage();
  90. return false;
  91. }
  92. ss.setIsSilent(commandLine.hasOption('S'));
  93. ss.execString = commandLine.getOptionValue('e');
  94. ss.fileName = commandLine.getOptionValue('f');
  95. ss.setIsVerbose(commandLine.hasOption('v'));
  96. String[] initFiles = commandLine.getOptionValues('i');
  97. if (null != initFiles) {
  98. ss.initFiles = Arrays.asList(initFiles);
  99. }
  100. if (ss.execString != null && ss.fileName != null) {
  101. System.err.println("The '-e' and '-f' options cannot be specified simultaneously");
  102. printUsage();
  103. return false;
  104. }
  105. if (commandLine.hasOption("hiveconf")) {
  106. Properties confProps = commandLine.getOptionProperties("hiveconf");
  107. for (String propKey : confProps.stringPropertyNames()) {
  108. ss.cmdProperties.setProperty(propKey, confProps.getProperty(propKey));
  109. }
  110. }
  111. return true;
  112. }
  113. private void printUsage() {
  114. new HelpFormatter().printHelp("hive", options);
  115. }
  116. }