From 98aaa38ec8ae50cd6b95279e432ab0b606d81d41 Mon Sep 17 00:00:00 2001 From: Jonathan Ellis Date: Wed, 19 Jan 2011 23:02:51 +0000 Subject: [PATCH] add short options for CLI flags patch by Christoph Hack and Pavel Yaskevich for CASSANDRA-1565 git-svn-id: https://svn.apache.org/repos/asf/cassandra/branches/cassandra-0.7@1061049 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 1 + .../org/apache/cassandra/cli/CliOptions.java | 114 ++++++++++++------ 2 files changed, 80 insertions(+), 35 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 2ee908b831..e26f41a6b6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -27,6 +27,7 @@ * fix writing SSTable column count statistics (CASSANDRA-1976) * fix streaming of multiple CFs during bootstrap (CASSANDRA-1992) * explicitly set JVM GC new generation size with -Xmn (CASSANDRA-1968) + * add short options for CLI flags (CASSANDRA-1565) 0.7.0-final diff --git a/src/java/org/apache/cassandra/cli/CliOptions.java b/src/java/org/apache/cassandra/cli/CliOptions.java index bc10880795..8fc7a0e1bd 100644 --- a/src/java/org/apache/cassandra/cli/CliOptions.java +++ b/src/java/org/apache/cassandra/cli/CliOptions.java @@ -24,9 +24,12 @@ import org.apache.commons.cli.*; * Used to process, and act upon the arguments passed to the CLI. * */ -public class CliOptions { +public class CliOptions +{ + private static CLIOptions options = null; // Info about command line options - private static Options options = null; // Info about command line options + // Name of the command line tool (used for error messages) + private static final String TOOL_NAME = "cassandra-cli"; // Command line options private static final String HOST_OPTION = "host"; @@ -48,48 +51,46 @@ public class CliOptions { // whether they take an extra argument, etc. static { - options = new Options(); - options.addOption(HOST_OPTION, true, "cassandra server's host name"); - options.addOption(PORT_OPTION, true, "cassandra server's thrift port"); - options.addOption(UNFRAME_OPTION, false, "cassandra server's framed transport"); - options.addOption(DEBUG_OPTION, false, "display stack traces"); - options.addOption(USERNAME_OPTION, true, "username for cassandra authentication"); - options.addOption(PASSWORD_OPTION, true, "password for cassandra authentication"); - options.addOption(KEYSPACE_OPTION, true, "cassandra keyspace user is authenticated against"); - options.addOption(BATCH_OPTION, false, "enabled batch mode (supress output; errors are fatal)"); - options.addOption(FILE_OPTION, true, "load statements from the specific file."); - options.addOption(JMX_PORT_OPTION, true, "JMX service port."); - options.addOption(HELP_OPTION, false, "usage help."); + options = new CLIOptions(); + + options.addOption("h", HOST_OPTION, "HOSTNAME", "cassandra server's host name"); + options.addOption("p", PORT_OPTION, "PORT", "cassandra server's thrift port"); + options.addOption("u", USERNAME_OPTION, "USERNAME", "user name for cassandra authentication"); + options.addOption("pw", PASSWORD_OPTION, "PASSWORD", "password for cassandra authentication"); + options.addOption("k", KEYSPACE_OPTION, "KEYSPACE", "cassandra keyspace user is authenticated against"); + options.addOption("f", FILE_OPTION, "FILENAME", "load statements from the specific file."); + options.addOption(null, JMX_PORT_OPTION, "JMX-PORT", "JMX service port"); + + // options without argument + options.addOption("B", BATCH_OPTION, "enabled batch mode (suppress output; errors are fatal)"); + options.addOption(null, UNFRAME_OPTION, "use cassandra server's unframed transport"); + options.addOption(null, DEBUG_OPTION, "display stack traces"); + options.addOption("?", HELP_OPTION, "usage help."); } private static void printUsage() { - System.err.println("Usage: cassandra-cli --host hostname [--port ] [--jmxport ] [--file ] [--unframed] [--debug]"); - System.err.println("\t[--username username] [--password password] [--keyspace keyspace] [--batch] [--help]"); + new HelpFormatter().printHelp(TOOL_NAME, options); } public void processArgs(CliSessionState css, String[] args) { - CommandLineParser parser = new PosixParser(); + CommandLineParser parser = new GnuParser(); + try { - CommandLine cmd = parser.parse(options, args); - - if (!cmd.hasOption(HOST_OPTION)) + CommandLine cmd = parser.parse(options, args, false); + + if (cmd.hasOption(HOST_OPTION)) + { + css.hostName = cmd.getOptionValue(HOST_OPTION); + } + else { // host name not specified in command line. // In this case, we don't implicitly connect at CLI startup. In this case, // the user must use the "connect" CLI statement to connect. - // css.hostName = null; - - // HelpFormatter formatter = new HelpFormatter(); - // formatter.printHelp("java com.facebook.infrastructure.cli.CliMain ", options); - // System.exit(1); - } - else - { - css.hostName = cmd.getOptionValue(HOST_OPTION); } // Look to see if frame has been specified @@ -113,23 +114,24 @@ public class CliOptions { { css.thriftPort = DEFAULT_THRIFT_PORT; } - + // Look for authentication credentials (username and password) - if (cmd.hasOption(USERNAME_OPTION)) + if (cmd.hasOption(USERNAME_OPTION)) { css.username = cmd.getOptionValue(USERNAME_OPTION); } + if (cmd.hasOption(PASSWORD_OPTION)) { css.password = cmd.getOptionValue(PASSWORD_OPTION); } - + // Look for keyspace - if (cmd.hasOption(KEYSPACE_OPTION)) + if (cmd.hasOption(KEYSPACE_OPTION)) { css.keyspace = cmd.getOptionValue(KEYSPACE_OPTION); } - + if (cmd.hasOption(BATCH_OPTION)) { css.batch = true; @@ -150,12 +152,54 @@ public class CliOptions { printUsage(); System.exit(1); } + + // Abort if there are any unrecognized arguments left + if (cmd.getArgs().length > 0) + { + System.err.printf("Unknown argument: %s\n", cmd.getArgs()[0]); + System.err.println(); + printUsage(); + System.exit(1); + } } catch (ParseException e) { + System.err.println(e.getMessage()); + System.err.println(); printUsage(); - System.err.println("\n" + e.getMessage()); System.exit(1); } } + + private static class CLIOptions extends Options + { + /** + * Add option with argument and argument name + * @param opt shortcut for option name + * @param longOpt complete option name + * @param argName argument name + * @param description description of the option + * @return updated Options object + */ + public Options addOption(String opt, String longOpt, String argName, String description) + { + Option option = new Option(opt, longOpt, true, description); + option.setArgName(argName); + + return addOption(option); + } + + /** + * Add option without argument + * @param opt shortcut for option name + * @param longOpt complete option name + * @param description description of the option + * @return updated Options object + */ + public Options addOption(String opt, String longOpt, String description) + { + return addOption(new Option(opt, longOpt, false, description)); + } + } + }