From 373ed42d849c7409d47ba1f4216b235dc2e233f4 Mon Sep 17 00:00:00 2001 From: Robert Greig Date: Wed, 4 Apr 2007 15:18:44 +0000 Subject: Added standard command line handline git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/branches/M2@525531 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/qpid/util/CommandLineParser.java | 42 +++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'java') diff --git a/java/common/src/main/java/org/apache/qpid/util/CommandLineParser.java b/java/common/src/main/java/org/apache/qpid/util/CommandLineParser.java index 6173780aa7..9051d6b470 100644 --- a/java/common/src/main/java/org/apache/qpid/util/CommandLineParser.java +++ b/java/common/src/main/java/org/apache/qpid/util/CommandLineParser.java @@ -143,8 +143,8 @@ public class CommandLineParser String[] nextOptionSpec = config[i]; addOption(nextOptionSpec[0], nextOptionSpec[1], (nextOptionSpec.length > 2) ? nextOptionSpec[2] : null, - (nextOptionSpec.length > 3) ? ("true".equals(nextOptionSpec[3]) ? true : false) : false, - (nextOptionSpec.length > 4) ? nextOptionSpec[4] : null); + (nextOptionSpec.length > 3) ? ("true".equals(nextOptionSpec[3]) ? true : false) : false, + (nextOptionSpec.length > 4) ? nextOptionSpec[4] : null); } } @@ -209,8 +209,9 @@ public class CommandLineParser // Print usage on each of the command line options. for (CommandLineOption optionInfo : optionMap.values()) { - result += optionInfo.option + " " + ((optionInfo.argument != null) ? (optionInfo.argument + " ") : "") - + optionInfo.comment + "\n"; + result += + optionInfo.option + " " + ((optionInfo.argument != null) ? (optionInfo.argument + " ") : "") + + optionInfo.comment + "\n"; } return result; @@ -603,6 +604,37 @@ public class CommandLineParser } } + /** + * Extracts all name=value pairs from the command line, sets them all as system properties and also returns + * a map of properties containing them. + * + * @param args The command line. + * + * @return A set of properties containing all name=value pairs from the command line. + */ + public static Properties processCommandLine(String[] args, CommandLineParser commandLine) + { + // Capture the command line arguments or display errors and correct usage and then exit. + Properties options = null; + + try + { + options = commandLine.parseCommandLine(args); + + // Add all the trailing command line options (name=value pairs) to system properties. They may be picked up + // from there. + commandLine.addCommandLineToSysProperties(); + } + catch (IllegalArgumentException e) + { + System.out.println(commandLine.getErrors()); + System.out.println(commandLine.getUsage()); + System.exit(1); + } + + return options; + } + /** * Holds information about a command line options. This includes what its name is, whether or not it is a flag, * whether or not it is mandatory, what its user comment is, what its argument reminder text is and what its @@ -646,7 +678,7 @@ public class CommandLineParser * @param formatRegexp The regular expression that the argument to this option must meet to be valid. */ public CommandLineOption(String option, boolean expectsArgs, String comment, String argument, boolean mandatory, - String formatRegexp) + String formatRegexp) { this.option = option; this.expectsArgs = expectsArgs; -- cgit v1.2.1