diff --git build-common.xml build-common.xml index 24fac36..49d9489 100644 --- build-common.xml +++ build-common.xml @@ -272,7 +272,7 @@ - + + - + + diff --git cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java index 93a7c95..605c6db 100644 --- cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java +++ cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java @@ -18,171 +18,78 @@ package org.apache.hadoop.hive.cli; -import java.util.ArrayList; -import java.util.List; -import java.util.ListIterator; - -import org.apache.commons.cli2.Argument; -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.WriteableCommandLine; -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.commons.cli2.option.PropertyOption; -import org.apache.commons.cli2.resource.ResourceConstants; +import java.util.Arrays; +import java.util.Properties; + +import org.apache.commons.cli.GnuParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * OptionsProcessor. - * + * */ public class OptionsProcessor { - protected static final Log l4j = LogFactory.getLog(OptionsProcessor.class.getName()); + private final Options options = new Options(); + private org.apache.commons.cli.CommandLine commandLine; - private final Parser parser = new Parser(); - private final Option confOptions, initFilesOption, isSilentOption, - execOption, fileOption, isHelpOption; - - /** - * Shamelessly cloned from Hadoop streaming take in multiple -hiveconf x=y parameters. - */ - class MultiPropertyOption extends PropertyOption { - private String optionString; - private boolean keyValue; - - MultiPropertyOption() { - super(); - } - - MultiPropertyOption(final String optionString, final String description, final int id, boolean keyValue) { - super(optionString, description, id); - this.optionString = optionString; - this.keyValue = keyValue; - } - - @Override - public boolean canProcess(final WriteableCommandLine commandLine, final String argument) { - boolean ret = (argument != null) && argument.startsWith(optionString); - - return ret; - } - - @Override - public void process(final WriteableCommandLine commandLine, final ListIterator arguments) - throws OptionException { - final String arg = (String) arguments.next(); - - if (!canProcess(commandLine, arg)) { - throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg); - } - - ArrayList properties = new ArrayList(); - String next = ""; - while (arguments.hasNext()) { - next = (String) arguments.next(); - if (!next.startsWith("-")) { - - if (keyValue && (next.indexOf("=") == -1)) { - throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, "argument: '" - + next + "' is not of the form x=y"); - } - properties.add(next); - } else { - arguments.previous(); - break; - } - } - - // add to any existing values (support specifying args multiple times) - List oldVal = (List) commandLine.getValue(this); - if (oldVal == null) { - commandLine.addValue(this, properties); - } else { - oldVal.addAll(properties); - } - } - } - - private Option createBoolOption(DefaultOptionBuilder builder, String longName, String shortName, - String desc) { - builder.reset(); - if (longName == null) { - return builder.withShortName(shortName).withDescription(desc).create(); - } else { - return builder.withShortName(shortName).withLongName(longName).withDescription(desc).create(); - } - } - - private Option createOptionWithArg(DefaultOptionBuilder builder, String longName, - String shortName, String desc, Argument arg) { - builder.reset(); - - DefaultOptionBuilder dob = builder.withShortName(shortName).withArgument(arg).withDescription( - desc); - - if (longName != null) { - dob = dob.withLongName(longName); - } - - return dob.create(); - } + @SuppressWarnings("static-access") public OptionsProcessor() { - DefaultOptionBuilder builder = new DefaultOptionBuilder("-", "-", false); - - ArgumentBuilder argBuilder = new ArgumentBuilder(); - - // -e - execOption = createOptionWithArg(builder, "exec", "e", "execute the following command", - argBuilder.withMinimum(1).withMaximum(1).create()); - // -f - fileOption = createOptionWithArg(builder, "file", "f", - "execute commands from the following file", argBuilder.withMinimum(1).withMaximum(1) - .create()); - - // -S - isSilentOption = createBoolOption(builder, "silent", "S", "silent mode"); - - // -help - isHelpOption = createBoolOption(builder, "help", "h", "help"); - - // -hiveconf var=val - confOptions = new MultiPropertyOption("-hiveconf", - "(n=v) Optional. Add or override Hive/Hadoop properties.", 'D', true); - - initFilesOption = new MultiPropertyOption( - "-i", "File to run before other commands", 'I', false); - - new PropertyOption(); - Group allOptions = - new GroupBuilder().withOption(confOptions).withOption(initFilesOption) - .withOption(isSilentOption).withOption(isHelpOption) - .withOption(execOption).withOption(fileOption).create(); - parser.setGroup(allOptions); + // -e 'quoted-query-string' + options.addOption(OptionBuilder + .hasArg() + .withArgName("quoted-query-string") + .withDescription("Sql from command line") + .create('e')); + + // -f + options.addOption(OptionBuilder + .hasArg() + .withArgName("filename") + .withDescription("SQL from files") + .create('f')); + + // -i + options.addOption(OptionBuilder + .hasArg() + .withArgName("filename") + .withDescription("Initialization SQL file") + .create('i')); + + // -hiveconf x=y + options.addOption(OptionBuilder + .withValueSeparator() + .hasArgs(2) + .withArgName("property=value") + .withLongOpt("hiveconf") + .withDescription("Use value for given property") + .create()); + + // [-S|--silent] + options.addOption(new Option("S", "silent", false, "Run CLI in silent mode")); + + // [-h|--help] + options.addOption(new Option("h", "help", false, "Print help information")); } - private CommandLine cmdLine; - public boolean process_stage1(String[] argv) { try { - cmdLine = parser.parse(argv); - - List hiveConfArgs = (List) cmdLine.getValue(confOptions); - if (null != hiveConfArgs) { - for (String s : hiveConfArgs) { - String[] parts = s.split("=", 2); - System.setProperty(parts[0], parts[1]); - } + commandLine = new GnuParser().parse(options, argv); + Properties confProps = commandLine.getOptionProperties("hiveconf"); + for (String propKey : confProps.stringPropertyNames()) { + System.setProperty(propKey, confProps.getProperty(propKey)); } - } catch (OptionException oe) { - System.err.println(oe.getMessage()); + } catch (ParseException e) { + System.err.println(e.getMessage()); + printUsage(); return false; } return true; @@ -190,55 +97,40 @@ public class OptionsProcessor { public boolean process_stage2(CliSessionState ss) { ss.getConf(); - // -S - ss.setIsSilent(cmdLine.hasOption(isSilentOption)); - // -e - ss.execString = (String) cmdLine.getValue(execOption); - // -f - ss.fileName = (String) cmdLine.getValue(fileOption); - // -i - List initFiles = (List) cmdLine.getValue(initFilesOption); - if (null != initFiles) { - ss.initFiles = initFiles; - } - // -h - if (cmdLine.hasOption(isHelpOption)) { - printUsage(null); + + if (commandLine.hasOption('h')) { + printUsage(); return false; } + + ss.setIsSilent(commandLine.hasOption('S')); + + ss.execString = commandLine.getOptionValue('e'); + + ss.fileName = commandLine.getOptionValue('f'); + + String[] initFiles = commandLine.getOptionValues('i'); + if (null != initFiles) { + ss.initFiles = Arrays.asList(initFiles); + } + if (ss.execString != null && ss.fileName != null) { - printUsage("-e and -f option cannot be specified simultaneously"); + System.err.println("The '-e' and '-f' options cannot be specified simultaneously"); + printUsage(); return false; } - List hiveConfArgs = (List) cmdLine.getValue(confOptions); - if (null != hiveConfArgs) { - for (String s : hiveConfArgs) { - String[] parts = s.split("=", 2); - ss.cmdProperties.setProperty(parts[0], parts[1]); + if (commandLine.hasOption("hiveconf")) { + Properties confProps = commandLine.getOptionProperties("hiveconf"); + for (String propKey : confProps.stringPropertyNames()) { + ss.cmdProperties.setProperty(propKey, confProps.getProperty(propKey)); } } return true; } - public void printUsage(String error) { - if (error != null) { - System.err.println("Invalid arguments: " + error); - } - System.err.println(""); - System.err.println( - "Usage: hive [--config confdir] [-hiveconf x=y]* [-i ]*" - + " [-f |-e ] [-S]"); - System.err.println(""); - System.err.println(" -i init Sql file"); - System.err.println(" -e 'quoted query string' Sql from command line"); - System.err.println(" -f Sql from files"); - System.err.println(" -S Silent mode in interactive shell"); - System.err.println(""); - System.err.println("-e and -f cannot be specified together. In the absence of these"); - System.err.println(" options, interactive shell is started."); - System.err.println(""); - + private void printUsage() { + new HelpFormatter().printHelp("hive", options); } } diff --git eclipse-templates/.classpath eclipse-templates/.classpath index d951927..e45ef48 100644 --- eclipse-templates/.classpath +++ eclipse-templates/.classpath @@ -1,4 +1,20 @@ + @@ -10,11 +26,6 @@ - - - - - @@ -29,6 +40,11 @@ + + + + + diff --git ivy/libraries.properties ivy/libraries.properties index 1d3773b..f24d73b 100644 --- ivy/libraries.properties +++ ivy/libraries.properties @@ -27,7 +27,7 @@ datanucleus-core.version=2.0.3 datanucleus-enhancer.version=2.0.3 datanucleus-rdbms.version=2.0.3 checkstyle.version=5.0 -commons-cli.version=2.0-SNAPSHOT +commons-cli.version=1.2 commons-codec.version=1.3 commons-collections.version=3.2.1 commons-dbcp.version=1.4 diff --git lib/commons-cli-2.0-SNAPSHOT.jar lib/commons-cli-2.0-SNAPSHOT.jar deleted file mode 100644 index 0b1d510..0000000 Binary files lib/commons-cli-2.0-SNAPSHOT.jar and /dev/null differ