Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 64106 invoked from network); 9 Dec 2002 23:47:44 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 9 Dec 2002 23:47:44 -0000 Received: (qmail 16886 invoked by uid 97); 9 Dec 2002 23:48:46 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 16795 invoked by uid 97); 9 Dec 2002 23:48:45 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 16784 invoked by uid 97); 9 Dec 2002 23:48:44 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 9 Dec 2002 23:47:26 -0000 Message-ID: <20021209234726.42339.qmail@icarus.apache.org> From: jkeyes@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli TestHelpFormatter.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N jkeyes 2002/12/09 15:47:26 Modified: cli/src/java/org/apache/commons/cli AlreadySelectedException.java BasicParser.java CommandLine.java CommandLineParser.java GnuParser.java HelpFormatter.java MissingArgumentException.java MissingOptionException.java Option.java OptionBuilder.java OptionGroup.java OptionValidator.java Options.java ParseException.java Parser.java PatternOptionBuilder.java PosixParser.java TypeHandler.java UnrecognizedOptionException.java Util.java cli/src/test/org/apache/commons/cli TestHelpFormatter.java Log: checkstyle fixes Revision Changes Path 1.5 +6 -4 jakarta-commons/cli/src/java/org/apache/commons/cli/AlreadySelectedException.java Index: AlreadySelectedException.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/AlreadySelectedException.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- AlreadySelectedException.java 19 Sep 2002 22:59:43 -0000 1.4 +++ AlreadySelectedException.java 9 Dec 2002 23:47:24 -0000 1.5 @@ -67,7 +67,8 @@ * @author John Keyes ( john at integralsource.com ) * @see ParseException */ -public class AlreadySelectedException extends ParseException { +public class AlreadySelectedException + extends ParseException { /** *

Construct a new AlreadySelectedException @@ -75,7 +76,8 @@ * * @param message the detail message */ - public AlreadySelectedException( String message ) { - super( message ); + public AlreadySelectedException(String message) + { + super(message); } -} +} \ No newline at end of file 1.4 +5 -6 jakarta-commons/cli/src/java/org/apache/commons/cli/BasicParser.java Index: BasicParser.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/BasicParser.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- BasicParser.java 19 Sep 2002 22:59:43 -0000 1.3 +++ BasicParser.java 9 Dec 2002 23:47:24 -0000 1.4 @@ -82,9 +82,8 @@ * when an non option is found. * @return The arguments String array. */ - protected String[] flatten( Options options, - String[] arguments, - boolean stopAtNonOption ) + protected String[] flatten(Options options, String[] arguments, + boolean stopAtNonOption) { // just echo the arguments return arguments; 1.17 +100 -67 jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java Index: CommandLine.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- CommandLine.java 19 Nov 2002 22:47:11 -0000 1.16 +++ CommandLine.java 9 Dec 2002 23:47:24 -0000 1.17 @@ -60,12 +60,11 @@ */ package org.apache.commons.cli; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.LinkedList; +import java.util.List; import java.util.Map; /** @@ -84,15 +83,15 @@ * @author John Keyes (john at integralsource.com) */ public class CommandLine { - + /** the unrecognised options/arguments */ - private List args = new LinkedList(); + private List args = new LinkedList(); /** the processed options */ private Map options = new HashMap(); /** the option name map */ - private Map names = new HashMap(); + private Map names = new HashMap(); /** Map of unique options for ease to get complete list of options */ private Map hashcodeMap = new HashMap(); @@ -103,17 +102,19 @@ /** *

Creates a command line.

*/ - CommandLine() { + CommandLine() + { } - + /** *

Query to see if an option has been set.

* * @param opt Short name of the option * @return true if set, false if not */ - public boolean hasOption(String opt) { - return options.containsKey( opt ); + public boolean hasOption(String opt) + { + return options.containsKey(opt); } /** @@ -122,8 +123,9 @@ * @param opt character name of the option * @return true if set, false if not */ - public boolean hasOption( char opt ) { - return hasOption( String.valueOf( opt ) ); + public boolean hasOption(char opt) + { + return hasOption(String.valueOf(opt)); } /** @@ -132,14 +134,18 @@ * @param opt the name of the option * @return the type of this Option */ - public Object getOptionObject( String opt ) { - String res = getOptionValue( opt ); - - if( !options.containsKey( opt ) ) { + public Object getOptionObject(String opt) + { + String res = getOptionValue(opt); + + if (!options.containsKey(opt)) + { return null; } - Object type = ((Option)options.get(opt)).getType(); - return res == null ? null : TypeHandler.createValue(res, type); + + Object type = ((Option) options.get(opt)).getType(); + + return (res == null) ? null : TypeHandler.createValue(res, type); } /** @@ -148,8 +154,9 @@ * @param opt the name of the option * @return the type of opt */ - public Object getOptionObject( char opt ) { - return getOptionObject( String.valueOf( opt ) ); + public Object getOptionObject(char opt) + { + return getOptionObject(String.valueOf(opt)); } /** @@ -159,8 +166,10 @@ * @return Value of the argument if option is set, and has an argument, * otherwise null. */ - public String getOptionValue( String opt ) { + public String getOptionValue(String opt) + { String[] values = getOptionValues(opt); + return (values == null) ? null : values[0]; } @@ -171,8 +180,9 @@ * @return Value of the argument if option is set, and has an argument, * otherwise null. */ - public String getOptionValue( char opt ) { - return getOptionValue( String.valueOf( opt ) ); + public String getOptionValue(char opt) + { + return getOptionValue(String.valueOf(opt)); } /** @@ -182,19 +192,24 @@ * @return Values of the argument if option is set, and has an argument, * otherwise null. */ - public String[] getOptionValues( String opt ) { + public String[] getOptionValues(String opt) + { List values = new java.util.ArrayList(); - opt = Util.stripLeadingHyphens( opt ); + opt = Util.stripLeadingHyphens(opt); String key = opt; - if( names.containsKey( opt ) ) { - key = (String)names.get( opt ); + + if (names.containsKey(opt)) + { + key = (String) names.get(opt); } - if( options.containsKey( key ) ) { - return ((Option)options.get(key)).getValues(); + if (options.containsKey(key)) + { + return ((Option) options.get(key)).getValues(); } + return null; } @@ -205,33 +220,39 @@ * @return Values of the argument if option is set, and has an argument, * otherwise null. */ - public String[] getOptionValues( char opt ) { - return getOptionValues( String.valueOf( opt ) ); + public String[] getOptionValues(char opt) + { + return getOptionValues(String.valueOf(opt)); } - + /** *

Retrieve the argument, if any, of an option.

* * @param opt name of the option - * @param defaultValue is the default value to be returned if the option is not specified + * @param defaultValue is the default value to be returned if the option + * is not specified * @return Value of the argument if option is set, and has an argument, * otherwise defaultValue. */ - public String getOptionValue( String opt, String defaultValue ) { - String answer = getOptionValue( opt ); - return ( answer != null ) ? answer : defaultValue; + public String getOptionValue(String opt, String defaultValue) + { + String answer = getOptionValue(opt); + + return (answer != null) ? answer : defaultValue; } - + /** *

Retrieve the argument, if any, of an option.

* * @param opt character name of the option - * @param defaultValue is the default value to be returned if the option is not specified + * @param defaultValue is the default value to be returned if the option + * is not specified * @return Value of the argument if option is set, and has an argument, * otherwise defaultValue. */ - public String getOptionValue( char opt, String defaultValue ) { - return getOptionValue( String.valueOf( opt ), defaultValue ); + public String getOptionValue(char opt, String defaultValue) + { + return getOptionValue(String.valueOf(opt), defaultValue); } /** @@ -239,21 +260,25 @@ * * @return remaining items passed in but not parsed as an array */ - public String[] getArgs() { - String[] answer = new String[ args.size() ]; - args.toArray( answer ); + public String[] getArgs() + { + String[] answer = new String[args.size()]; + + args.toArray(answer); + return answer; } - + /** *

Retrieve any left-over non-recognized options and arguments

* * @return remaining items passed in but not parsed as a List. */ - public List getArgList() { + public List getArgList() + { return args; } - + /** * jkeyes * - commented out until it is implemented properly @@ -261,16 +286,17 @@ * * @return Stringified form of this object */ + /* public String toString() { StringBuffer buf = new StringBuffer(); - - buf.append( "[ CommandLine: [ options: " ); - buf.append( options.toString() ); - buf.append( " ] [ args: "); - buf.append( args.toString() ); - buf.append( " ] ]" ); - + + buf.append("[ CommandLine: [ options: "); + buf.append(options.toString()); + buf.append(" ] [ args: "); + buf.append(args.toString()); + buf.append(" ] ]"); + return buf.toString(); } */ @@ -280,28 +306,33 @@ * * @param arg the unrecognised option/argument. */ - void addArg(String arg) { - args.add( arg ); + void addArg(String arg) + { + args.add(arg); } - + /** *

Add an option to the command line. The values of * the option are stored.

* * @param opt the processed option */ - void addOption( Option opt ) { - hashcodeMap.put( new Integer( opt.hashCode() ), opt ); + void addOption(Option opt) + { + hashcodeMap.put(new Integer(opt.hashCode()), opt); String key = opt.getKey(); - if( key == null ) { + + if (key == null) + { key = opt.getLongOpt(); } - else { - names.put( opt.getLongOpt(), key ); + else + { + names.put(opt.getLongOpt(), key); } - options.put( key, opt ); + options.put(key, opt); } /** @@ -310,7 +341,8 @@ * @return an Iterator over the processed {@link Option} * members of this {@link CommandLine} */ - public Iterator iterator( ) { + public Iterator iterator() + { return hashcodeMap.values().iterator(); } @@ -319,14 +351,15 @@ * * @return an array of the processed {@link Option}s. */ - public Option[] getOptions( ) { + public Option[] getOptions() + { Collection processed = options.values(); + // reinitialise array - optionsArray = new Option[ processed.size() ]; + optionsArray = new Option[processed.size()]; // return the array - return (Option[]) processed.toArray( optionsArray ); + return (Option[]) processed.toArray(optionsArray); } - -} +} \ No newline at end of file 1.6 +15 -12 jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLineParser.java Index: CommandLineParser.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLineParser.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- CommandLineParser.java 18 Nov 2002 08:41:26 -0000 1.5 +++ CommandLineParser.java 9 Dec 2002 23:47:24 -0000 1.6 @@ -70,7 +70,7 @@ * @author John Keyes (john at integralsource.com) */ public interface CommandLineParser { - + /** * Parse the arguments according to the specified options. * @@ -81,8 +81,8 @@ * @throws ParseException if there are any problems encountered * while parsing the command line tokens. */ - public CommandLine parse( Options options, String[] arguments ) - throws ParseException; + CommandLine parse(Options options, String[] arguments) + throws ParseException; /** * Parse the arguments according to the specified options and @@ -96,8 +96,9 @@ * @throws ParseException if there are any problems encountered * while parsing the command line tokens. */ - public CommandLine parse( Options options, String[] arguments, Properties props ) - throws ParseException; + CommandLine parse(Options options, String[] arguments, + Properties properties) + throws ParseException; /** * Parse the arguments according to the specified options. @@ -111,8 +112,9 @@ * @throws ParseException if there are any problems encountered * while parsing the command line tokens. */ - public CommandLine parse( Options options, String[] arguments, boolean stopAtNonOption ) - throws ParseException; + CommandLine parse(Options options, String[] arguments, + boolean stopAtNonOption) + throws ParseException; /** * Parse the arguments according to the specified options and @@ -127,6 +129,7 @@ * @throws ParseException if there are any problems encountered * while parsing the command line tokens. */ - public CommandLine parse( Options options, String[] arguments, Properties properties, boolean stopAtNonOption) - throws ParseException; + CommandLine parse(Options options, String[] arguments, + Properties properties, boolean stopAtNonOption) + throws ParseException; } 1.11 +90 -59 jakarta-commons/cli/src/java/org/apache/commons/cli/GnuParser.java Index: GnuParser.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/GnuParser.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- GnuParser.java 19 Sep 2002 22:59:43 -0000 1.10 +++ GnuParser.java 9 Dec 2002 23:47:24 -0000 1.11 @@ -60,10 +60,7 @@ */ package org.apache.commons.cli; -import java.util.Arrays; import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; /** * The class GnuParser provides an implementation of the @@ -82,7 +79,8 @@ *

Resets the members to their original state i.e. remove * all of tokens entries. */ - private void init() { + private void init() + { tokens.clear(); } @@ -98,90 +96,123 @@ * * *

+ * + * @param options The Options to parse the arguments by. + * @param arguments The arguments that have to be flattened. + * @param stopAtNonOption specifies whether to stop + * flattening when a non option has been encountered + * @return a String array of the flattened arguments */ - protected String[] flatten( Options options, - String[] arguments, - boolean stopAtNonOption ) + protected String[] flatten(Options options, String[] arguments, + boolean stopAtNonOption) { init(); + boolean eatTheRest = false; Option currentOption = null; - for( int i = 0; i < arguments.length; i++ ) { - if( "--".equals( arguments[i] ) ) { + for (int i = 0; i < arguments.length; i++) + { + if ("--".equals(arguments[i])) + { eatTheRest = true; - tokens.add( "--" ); + tokens.add("--"); } - else if ( "-".equals( arguments[i] ) ) { - tokens.add( "-" ); + else if ("-".equals(arguments[i])) + { + tokens.add("-"); } - else if( arguments[i].startsWith( "-" ) ) { - Option option = options.getOption( arguments[i] ); + else if (arguments[i].startsWith("-")) + { + Option option = options.getOption(arguments[i]); // this is not an Option - if( option == null ) { + if (option == null) + { // handle special properties Option - Option specialOption = options.getOption( arguments[i].substring(0,2) ); - if( specialOption != null ) { - tokens.add( arguments[i].substring(0,2) ); - tokens.add( arguments[i].substring(2) ); + Option specialOption = + options.getOption(arguments[i].substring(0, 2)); + + if (specialOption != null) + { + tokens.add(arguments[i].substring(0, 2)); + tokens.add(arguments[i].substring(2)); } - else if( stopAtNonOption ) { + else if (stopAtNonOption) + { eatTheRest = true; - tokens.add( arguments[i] ); + tokens.add(arguments[i]); } - else { - tokens.add( arguments[i] ); + else + { + tokens.add(arguments[i]); } } - else { + else + { currentOption = option; + // special option - Option specialOption = options.getOption( arguments[i].substring(0,2) ); - if( specialOption != null && option == null ) { - tokens.add( arguments[i].substring(0,2) ); - tokens.add( arguments[i].substring(2) ); - } - else if( currentOption != null && currentOption.hasArg() ) { - if( currentOption.hasArg() ) { - tokens.add( arguments[i] ); - currentOption= null; + Option specialOption = + options.getOption(arguments[i].substring(0, 2)); + + if ((specialOption != null) && (option == null)) + { + tokens.add(arguments[i].substring(0, 2)); + tokens.add(arguments[i].substring(2)); + } + else if ((currentOption != null) && currentOption.hasArg()) + { + if (currentOption.hasArg()) + { + tokens.add(arguments[i]); + currentOption = null; } - else if ( currentOption.hasArgs() ) { - tokens.add( arguments[i] ); + else if (currentOption.hasArgs()) + { + tokens.add(arguments[i]); } - else if ( stopAtNonOption ) { + else if (stopAtNonOption) + { eatTheRest = true; - tokens.add( "--" ); - tokens.add( arguments[i] ); + tokens.add("--"); + tokens.add(arguments[i]); } - else { - tokens.add( arguments[i] ); + else + { + tokens.add(arguments[i]); } - } - else if (currentOption != null ) { - tokens.add( arguments[i] ); - } - else if ( stopAtNonOption ) { + } + else if (currentOption != null) + { + tokens.add(arguments[i]); + } + else if (stopAtNonOption) + { eatTheRest = true; - tokens.add( "--" ); - tokens.add( arguments[i] ); + tokens.add("--"); + tokens.add(arguments[i]); } - else { - tokens.add( arguments[i] ); + else + { + tokens.add(arguments[i]); } } } - else { - tokens.add( arguments[i] ); + else + { + tokens.add(arguments[i]); } - if( eatTheRest ) { - for( i++; i < arguments.length; i++ ) { - tokens.add( arguments[i] ); + if (eatTheRest) + { + for (i++; i < arguments.length; i++) + { + tokens.add(arguments[i]); } } } - return (String[])tokens.toArray( new String[] {} ); + + return (String[]) tokens.toArray(new String[] { }); } } 1.12 +875 -479 jakarta-commons/cli/src/java/org/apache/commons/cli/HelpFormatter.java Index: HelpFormatter.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/HelpFormatter.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- HelpFormatter.java 19 Nov 2002 22:50:19 -0000 1.11 +++ HelpFormatter.java 9 Dec 2002 23:47:24 -0000 1.12 @@ -58,10 +58,10 @@ * . * */ - package org.apache.commons.cli; import java.io.PrintWriter; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -75,503 +75,899 @@ * @author Slawek Zachcial * @author John Keyes (john at integralsource.com) **/ -public class HelpFormatter -{ - // --------------------------------------------------------------- Constants - - public static final int DEFAULT_WIDTH = 74; - public static final int DEFAULT_LEFT_PAD = 1; - public static final int DEFAULT_DESC_PAD = 3; - public static final String DEFAULT_SYNTAX_PREFIX = "usage: "; - public static final String DEFAULT_OPT_PREFIX = "-"; - public static final String DEFAULT_LONG_OPT_PREFIX = "--"; - public static final String DEFAULT_ARG_NAME = "arg"; - - // ------------------------------------------------------------------ Static - - // -------------------------------------------------------------- Attributes - - public int defaultWidth; - public int defaultLeftPad; - public int defaultDescPad; - public String defaultSyntaxPrefix; - public String defaultNewLine; - public String defaultOptPrefix; - public String defaultLongOptPrefix; - public String defaultArgName; - - // ------------------------------------------------------------ Constructors - public HelpFormatter() - { - defaultWidth = DEFAULT_WIDTH; - defaultLeftPad = DEFAULT_LEFT_PAD; - defaultDescPad = DEFAULT_DESC_PAD; - defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX; - defaultNewLine = System.getProperty("line.separator"); - defaultOptPrefix = DEFAULT_OPT_PREFIX; - defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX; - defaultArgName = DEFAULT_ARG_NAME; - } - - // ------------------------------------------------------------------ Public - - public void printHelp( String cmdLineSyntax, - Options options ) - { - printHelp( defaultWidth, cmdLineSyntax, null, options, null, false ); - } - - public void printHelp( String cmdLineSyntax, - Options options, - boolean autoUsage ) - { - printHelp( defaultWidth, cmdLineSyntax, null, options, null, autoUsage ); - } - - public void printHelp( String cmdLineSyntax, - String header, - Options options, - String footer ) - { - printHelp( cmdLineSyntax, header, options, footer, false ); - } - - public void printHelp( String cmdLineSyntax, - String header, - Options options, - String footer, - boolean autoUsage ) - { - printHelp(defaultWidth, cmdLineSyntax, header, options, footer, autoUsage ); - } - - public void printHelp( int width, - String cmdLineSyntax, - String header, - Options options, - String footer ) - { - printHelp( width, cmdLineSyntax, header, options, footer, false ); - } - - public void printHelp( int width, - String cmdLineSyntax, - String header, - Options options, - String footer, - boolean autoUsage ) - { - PrintWriter pw = new PrintWriter(System.out); - printHelp( pw, width, cmdLineSyntax, header, - options, defaultLeftPad, defaultDescPad, footer, autoUsage ); - pw.flush(); - } - public void printHelp( PrintWriter pw, - int width, - String cmdLineSyntax, - String header, - Options options, - int leftPad, - int descPad, - String footer ) - throws IllegalArgumentException - { - printHelp( pw, width, cmdLineSyntax, header, options, leftPad, descPad, footer, false ); - } - - public void printHelp( PrintWriter pw, - int width, - String cmdLineSyntax, - String header, - Options options, - int leftPad, - int descPad, - String footer, - boolean autoUsage ) - throws IllegalArgumentException - { - if ( cmdLineSyntax == null || cmdLineSyntax.length() == 0 ) - { - throw new IllegalArgumentException("cmdLineSyntax not provided"); - } - - if ( autoUsage ) { - printUsage( pw, width, cmdLineSyntax, options ); - } - else { - printUsage( pw, width, cmdLineSyntax ); - } - - if ( header != null && header.trim().length() > 0 ) - { - printWrapped( pw, width, header ); - } - printOptions( pw, width, options, leftPad, descPad ); - if ( footer != null && footer.trim().length() > 0 ) - { - printWrapped( pw, width, footer ); - } - } - - /** - *

Prints the usage statement for the specified application.

- * - * @param pw The PrintWriter to print the usage statement - * @param width ?? - * @param appName The application name - * @param options The command line Options - * @see #appendOptionGroup(StringBuffer,OptionGroup) - * @see #appendOption(StringBuffer,Option,boolean) - * - */ - public void printUsage( PrintWriter pw, int width, String app, Options options ) - { - // initialise the string buffer - StringBuffer buff = new StringBuffer( defaultSyntaxPrefix ).append( app ).append( " " ); - - // create a list for processed option groups - final Collection processedGroups = new ArrayList(); - - // temp variable - Option option; - - // iterate over the options - for ( Iterator i = options.getOptions().iterator(); i.hasNext(); ) - { - // get the next Option - option = (Option) i.next(); - - // check if the option is part of an OptionGroup - OptionGroup group = options.getOptionGroup( option ); - - // if the option is part of a group - if( group != null) { - // and if the group has not already been processed - if( !processedGroups.contains(group) ) { - // add the group to the processed list - processedGroups.add( group ); - // add the usage clause - appendOptionGroup( buff, group ); - } - // otherwise the option was displayed in the group - // previously so ignore it. - } - // if the Option is not part of an OptionGroup - else { - appendOption( buff, option, option.isRequired() ); - } - - if( i.hasNext() ){ - buff.append( " " ); - } - } - - // call printWrapped - printWrapped( pw, width, buff.toString().indexOf(' ')+1, - buff.toString() ); - } - - /** - * Appends the usage clause for an OptionGroup to a StringBuffer. - * The clause is wrapped in square brackets if the group is required. - * The display of the options is handled by appendOption - * @param buff the StringBuffer to append to - * @param group the group to append - * @see #appendOption(StringBuffer,Option,boolean) - */ - private static void appendOptionGroup( final StringBuffer buff, final OptionGroup group ) - { - if( !group.isRequired() ) { - buff.append( "[" ); - } - - // for each option in the OptionGroup - for( Iterator i = group.getOptions().iterator(); i.hasNext(); ) { - // whether the option is required or not is handled at group level - appendOption( buff, (Option)i.next(), true); - if( i.hasNext() ) { - buff.append( " | " ); - } - } - - if( !group.isRequired() ) { - buff.append( "]" ); - } - } - - /** - * Appends the usage clause for an Option to a StringBuffer. - * The clause is wrapped in square brackets if the group is required. - * The display of the options is handled by appendOption - * @param buff the StringBuffer to append to - * @param group the group to append - * @see #appendOption(StringBuffer,Option,boolean) - */ - private static void appendOption( final StringBuffer buff, final Option option, final boolean required) - { - if( !required ) { - buff.append( "[" ); - } - - if( option.getOpt() != null ) { - buff.append( "-" ).append( option.getOpt() ); - } - else { - buff.append( "--" ).append( option.getLongOpt() ); - } - - // if the Option has a value - if( option.hasArg() && option.getArgName() != null ) { - buff.append( " <" ).append( option.getArgName() ).append( ">" ); - } - - // if the Option is not a required option - if( !required ) { - buff.append( "]" ); - } - } - - - public void printUsage( PrintWriter pw, int width, String cmdLineSyntax ) - { - int argPos = cmdLineSyntax.indexOf(' ') + 1; - printWrapped(pw, width, defaultSyntaxPrefix.length() + argPos, - defaultSyntaxPrefix + cmdLineSyntax); - } - - public void printOptions( PrintWriter pw, int width, Options options, int leftPad, int descPad ) - { - StringBuffer sb = new StringBuffer(); - renderOptions(sb, width, options, leftPad, descPad); - pw.println(sb.toString()); - } - - public void printWrapped( PrintWriter pw, int width, String text ) - { - printWrapped(pw, width, 0, text); - } - - public void printWrapped( PrintWriter pw, int width, int nextLineTabStop, String text ) - { - StringBuffer sb = new StringBuffer(text.length()); - renderWrappedText(sb, width, nextLineTabStop, text); - pw.println(sb.toString()); - } - - // --------------------------------------------------------------- Protected - - protected StringBuffer renderOptions( StringBuffer sb, - int width, - Options options, - int leftPad, - int descPad ) - { - final String lpad = createPadding(leftPad); - final String dpad = createPadding(descPad); - - //first create list containing only -a,--aaa where -a is opt and --aaa is - //long opt; in parallel look for the longest opt string - //this list will be then used to sort options ascending - int max = 0; - StringBuffer optBuf; - List prefixList = new ArrayList(); - Option option; - List optList = options.helpOptions(); - Collections.sort( optList, new StringBufferComparator() ); - for ( Iterator i = optList.iterator(); i.hasNext(); ) - { - option = (Option) i.next(); - optBuf = new StringBuffer(8); - - if ( option.getOpt() == null ) - { - optBuf.append(lpad).append(" " + defaultLongOptPrefix).append(option.getLongOpt()); - } - else - { - optBuf.append(lpad).append(defaultOptPrefix).append(option.getOpt()); - if ( option.hasLongOpt() ) - { - optBuf.append(',').append(defaultLongOptPrefix).append(option.getLongOpt()); - } - - } - - if( option.hasArg() ) { - if( option.hasArgName() ) { - optBuf.append(" <").append( option.getArgName() ).append( ">" ); - } - else { - optBuf.append(' '); - } - } - - prefixList.add(optBuf); - max = optBuf.length() > max ? optBuf.length() : max; - } - int x = 0; - for ( Iterator i = optList.iterator(); i.hasNext(); ) - { - option = (Option) i.next(); - optBuf = new StringBuffer( prefixList.get( x++ ).toString() ); - - if ( optBuf.length() < max ) - { - optBuf.append(createPadding(max - optBuf.length())); - } - optBuf.append( dpad ); - - int nextLineTabStop = max + descPad; - - if( option.getDescription() != null ) { - optBuf.append( option.getDescription() ); - } - renderWrappedText(sb, width, nextLineTabStop, - optBuf.toString()); - if ( i.hasNext() ) - { - sb.append(defaultNewLine); - } - } - - return sb; - } - - protected StringBuffer renderWrappedText( StringBuffer sb, - int width, - int nextLineTabStop, - String text ) - { - int pos = findWrapPos( text, width, 0); - if ( pos == -1 ) - { - sb.append(rtrim(text)); - return sb; - } - else - { - sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine); - } - - //all following lines must be padded with nextLineTabStop space characters - final String padding = createPadding(nextLineTabStop); - - while ( true ) - { - text = padding + text.substring(pos).trim(); - pos = findWrapPos( text, width, nextLineTabStop ); - if ( pos == -1 ) - { - sb.append(text); +public class HelpFormatter { + // --------------------------------------------------------------- Constants + + /** default number of characters per line */ + public static final int DEFAULT_WIDTH = 74; + + /** default padding to the left of each line */ + public static final int DEFAULT_LEFT_PAD = 1; + + /** ?? */ + public static final int DEFAULT_DESC_PAD = 3; + + /** the string to display at the begining of the usage statement */ + public static final String DEFAULT_SYNTAX_PREFIX = "usage: "; + + /** default prefix for shortOpts */ + public static final String DEFAULT_OPT_PREFIX = "-"; + + /** default prefix for long Option */ + public static final String DEFAULT_LONG_OPT_PREFIX = "--"; + + /** default name for an argument */ + public static final String DEFAULT_ARG_NAME = "arg"; + + // -------------------------------------------------------------- Attributes + + /** number of characters per line */ + private int defaultWidth = DEFAULT_WIDTH; + + /** amount of padding to the left of each line */ + private int defaultLeftPad = DEFAULT_LEFT_PAD; + + /** ?? */ + private int defaultDescPad = DEFAULT_DESC_PAD; + + /** the string to display at the begining of the usage statement */ + private String defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX; + + /** the new line character/string ?? */ + private String defaultNewLine = System.getProperty("line.separator"); + + /** the shortOpt prefix */ + private String defaultOptPrefix = DEFAULT_OPT_PREFIX; + + /** the long Opt prefix */ + private String defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX; + + /** the name of the argument */ + private String defaultArgName = DEFAULT_ARG_NAME; + + /** + * Sets the 'width'. + * + * @param width the new value of 'width' + */ + public void setWidth(int width) + { + this.defaultWidth = width; + } + + /** + * Returns the 'width'. + * + * @return the 'width' + */ + public int getWidth() + { + return this.defaultWidth; + } + + /** + * Sets the 'leftPadding'. + * + * @param padding the new value of 'leftPadding' + */ + public void setLeftPadding(int padding) + { + this.defaultLeftPad = padding; + } + + /** + * Returns the 'leftPadding'. + * + * @return the 'leftPadding' + */ + public int getLeftPadding() + { + return this.defaultLeftPad; + } + + /** + * Sets the 'descPadding'. + * + * @param padding the new value of 'descPadding' + */ + public void setDescPadding(int padding) + { + this.defaultDescPad = padding; + } + + /** + * Returns the 'descPadding'. + * + * @return the 'descPadding' + */ + public int getDescPadding() + { + return this.defaultDescPad; + } + + /** + * Sets the 'syntaxPrefix'. + * + * @param prefix the new value of 'syntaxPrefix' + */ + public void setSyntaxPrefix(String prefix) + { + this.defaultSyntaxPrefix = prefix; + } + + /** + * Returns the 'syntaxPrefix'. + * + * @return the 'syntaxPrefix' + */ + public String getSyntaxPrefix() + { + return this.defaultSyntaxPrefix; + } + + /** + * Sets the 'newLine'. + * + * @param newline the new value of 'newLine' + */ + public void setNewLine(String newline) + { + this.defaultNewLine = newline; + } + + /** + * Returns the 'newLine'. + * + * @return the 'newLine' + */ + public String getNewLine() + { + return this.defaultNewLine; + } + + /** + * Sets the 'optPrefix'. + * + * @param prefix the new value of 'optPrefix' + */ + public void setOptPrefix(String prefix) + { + this.defaultOptPrefix = prefix; + } + + /** + * Returns the 'optPrefix'. + * + * @return the 'optPrefix' + */ + public String getOptPrefix() + { + return this.defaultOptPrefix; + } + + /** + * Sets the 'longOptPrefix'. + * + * @param prefix the new value of 'longOptPrefix' + */ + public void setLongOptPrefix(String prefix) + { + this.defaultLongOptPrefix = prefix; + } + + /** + * Returns the 'longOptPrefix'. + * + * @return the 'longOptPrefix' + */ + public String getLongOptPrefix() + { + return this.defaultLongOptPrefix; + } + + /** + * Sets the 'argName'. + * + * @param name the new value of 'argName' + */ + public void setArgName(String name) + { + this.defaultArgName = name; + } + + /** + * Returns the 'argName'. + * + * @return the 'argName' + */ + public String getArgName() + { + return this.defaultArgName; + } + + + // ------------------------------------------------------------------ Public + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param cmdLineSyntax the syntax for this application + * @param options the Options instance + */ + public void printHelp(String cmdLineSyntax, Options options) + { + printHelp(defaultWidth, cmdLineSyntax, null, options, null, false); + } + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param cmdLineSyntax the syntax for this application + * @param options the Options instance + * @param autoUsage whether to print an automatically generated + * usage statement + */ + public void printHelp(String cmdLineSyntax, Options options, + boolean autoUsage) + { + printHelp(defaultWidth, cmdLineSyntax, null, options, null, autoUsage); + } + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param cmdLineSyntax the syntax for this application + * @param header the banner to display at the begining of the help + * @param options the Options instance + * @param footer the banner to display at the end of the help + */ + public void printHelp(String cmdLineSyntax, String header, Options options, + String footer) + { + printHelp(cmdLineSyntax, header, options, footer, false); + } + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param cmdLineSyntax the syntax for this application + * @param header the banner to display at the begining of the help + * @param options the Options instance + * @param footer the banner to display at the end of the help + * @param autoUsage whether to print an automatically generated + * usage statement + */ + public void printHelp(String cmdLineSyntax, String header, Options options, + String footer, boolean autoUsage) + { + printHelp(defaultWidth, cmdLineSyntax, header, options, footer, + autoUsage); + } + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param width the number of characters to be displayed on each line + * @param cmdLineSyntax the syntax for this application + * @param header the banner to display at the begining of the help + * @param options the Options instance + * @param footer the banner to display at the end of the help + */ + public void printHelp(int width, String cmdLineSyntax, String header, + Options options, String footer) + { + printHelp(width, cmdLineSyntax, header, options, footer, false); + } + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param width the number of characters to be displayed on each line + * @param cmdLineSyntax the syntax for this application + * @param header the banner to display at the begining of the help + * @param options the Options instance + * @param footer the banner to display at the end of the help + * @param autoUsage whether to print an automatically generated + * usage statement + */ + public void printHelp(int width, String cmdLineSyntax, String header, + Options options, String footer, boolean autoUsage) + { + PrintWriter pw = new PrintWriter(System.out); + + printHelp(pw, width, cmdLineSyntax, header, options, defaultLeftPad, + defaultDescPad, footer, autoUsage); + pw.flush(); + } + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param pw the writer to which the help will be written + * @param width the number of characters to be displayed on each line + * @param cmdLineSyntax the syntax for this application + * @param header the banner to display at the begining of the help + * @param options the Options instance + * @param leftPad the number of characters of padding to be prefixed + * to each line + * @param descPad the number of characters of padding to be prefixed + * to each description line + * @param footer the banner to display at the end of the help + */ + public void printHelp(PrintWriter pw, int width, String cmdLineSyntax, + String header, Options options, int leftPad, + int descPad, String footer) + { + printHelp(pw, width, cmdLineSyntax, header, options, leftPad, descPad, + footer, false); + } + + + /** + *

Print the help for options with the specified + * command line syntax.

+ * + * @param pw the writer to which the help will be written + * @param width the number of characters to be displayed on each line + * @param cmdLineSyntax the syntax for this application + * @param header the banner to display at the begining of the help + * @param options the Options instance + * @param leftPad the number of characters of padding to be prefixed + * to each line + * @param descPad the number of characters of padding to be prefixed + * to each description line + * @param footer the banner to display at the end of the help + * @param autoUsage whether to print an automatically generated + * usage statement + */ + public void printHelp(PrintWriter pw, int width, String cmdLineSyntax, + String header, Options options, int leftPad, + int descPad, String footer, boolean autoUsage) + { + if ((cmdLineSyntax == null) || (cmdLineSyntax.length() == 0)) + { + throw new IllegalArgumentException("cmdLineSyntax not provided"); + } + + if (autoUsage) + { + printUsage(pw, width, cmdLineSyntax, options); + } + else + { + printUsage(pw, width, cmdLineSyntax); + } + + if ((header != null) && (header.trim().length() > 0)) + { + printWrapped(pw, width, header); + } + + printOptions(pw, width, options, leftPad, descPad); + + if ((footer != null) && (footer.trim().length() > 0)) + { + printWrapped(pw, width, footer); + } + } + + /** + *

Prints the usage statement for the specified application.

+ * + * @param pw The PrintWriter to print the usage statement + * @param width The number of characters to display per line + * @param app The application name + * @param options The command line Options + * + */ + public void printUsage(PrintWriter pw, int width, String app, + Options options) + { + // initialise the string buffer + StringBuffer buff = new StringBuffer(defaultSyntaxPrefix).append(app) + .append(" "); + + // create a list for processed option groups + final Collection processedGroups = new ArrayList(); + + // temp variable + Option option; + + // iterate over the options + for (Iterator i = options.getOptions().iterator(); i.hasNext();) + { + // get the next Option + option = (Option) i.next(); + + // check if the option is part of an OptionGroup + OptionGroup group = options.getOptionGroup(option); + + // if the option is part of a group + if (group != null) + { + // and if the group has not already been processed + if (!processedGroups.contains(group)) + { + // add the group to the processed list + processedGroups.add(group); + + + // add the usage clause + appendOptionGroup(buff, group); + } + + // otherwise the option was displayed in the group + // previously so ignore it. + } + + // if the Option is not part of an OptionGroup + else + { + appendOption(buff, option, option.isRequired()); + } + + if (i.hasNext()) + { + buff.append(" "); + } + } + + + // call printWrapped + printWrapped(pw, width, buff.toString().indexOf(' ') + 1, + buff.toString()); + } + + /** + * Appends the usage clause for an OptionGroup to a StringBuffer. + * The clause is wrapped in square brackets if the group is required. + * The display of the options is handled by appendOption + * @param buff the StringBuffer to append to + * @param group the group to append + * @see #appendOption(StringBuffer,Option,boolean) + */ + private static void appendOptionGroup(final StringBuffer buff, + final OptionGroup group) + { + if (!group.isRequired()) + { + buff.append("["); + } + + // for each option in the OptionGroup + for (Iterator i = group.getOptions().iterator(); i.hasNext();) + { + // whether the option is required or not is handled at group level + appendOption(buff, (Option) i.next(), true); + + if (i.hasNext()) + { + buff.append(" | "); + } + } + + if (!group.isRequired()) + { + buff.append("]"); + } + } + + /** + * Appends the usage clause for an Option to a StringBuffer. + * + * @param buff the StringBuffer to append to + * @param option the Option to append + * @param required whether the Option is required or not + */ + private static void appendOption(final StringBuffer buff, + final Option option, + final boolean required) + { + if (!required) + { + buff.append("["); + } + + if (option.getOpt() != null) + { + buff.append("-").append(option.getOpt()); + } + else + { + buff.append("--").append(option.getLongOpt()); + } + + // if the Option has a value + if (option.hasArg() && (option.getArgName() != null)) + { + buff.append(" <").append(option.getArgName()).append(">"); + } + + // if the Option is not a required option + if (!required) + { + buff.append("]"); + } + } + + /** + *

Print the cmdLineSyntax to the specified writer, using the + * specified width.

+ * + * @param pw The printWriter to write the help to + * @param width The number of characters per line for the usage statement. + * @param cmdLineSyntax The usage statement. + */ + public void printUsage(PrintWriter pw, int width, String cmdLineSyntax) + { + int argPos = cmdLineSyntax.indexOf(' ') + 1; + + printWrapped(pw, width, defaultSyntaxPrefix.length() + argPos, + defaultSyntaxPrefix + cmdLineSyntax); + } + + /** + *

Print the help for the specified Options to the specified writer, + * using the specified width, left padding and description padding.

+ * + * @param pw The printWriter to write the help to + * @param width The number of characters to display per line + * @param options The command line Options + * @param leftPad the number of characters of padding to be prefixed + * to each line + * @param descPad the number of characters of padding to be prefixed + * to each description line + */ + public void printOptions(PrintWriter pw, int width, Options options, + int leftPad, int descPad) + { + StringBuffer sb = new StringBuffer(); + + renderOptions(sb, width, options, leftPad, descPad); + pw.println(sb.toString()); + } + + /** + *

Print the specified text to the specified PrintWriter.

+ * + * @param pw The printWriter to write the help to + * @param width The number of characters to display per line + * @param text The text to be written to the PrintWriter + */ + public void printWrapped(PrintWriter pw, int width, String text) + { + printWrapped(pw, width, 0, text); + } + + /** + *

Print the specified text to the specified PrintWriter.

+ * + * @param pw The printWriter to write the help to + * @param width The number of characters to display per line + * @param nextLineTabStop The position on the next line for the first tab. + * @param text The text to be written to the PrintWriter + */ + public void printWrapped(PrintWriter pw, int width, int nextLineTabStop, + String text) + { + StringBuffer sb = new StringBuffer(text.length()); + + renderWrappedText(sb, width, nextLineTabStop, text); + pw.println(sb.toString()); + } + + // --------------------------------------------------------------- Protected + + /** + *

Render the specified Options and return the rendered Options + * in a StringBuffer.

+ * + * @param sb The StringBuffer to place the rendered Options into. + * @param width The number of characters to display per line + * @param options The command line Options + * @param leftPad the number of characters of padding to be prefixed + * to each line + * @param descPad the number of characters of padding to be prefixed + * to each description line + * + * @return the StringBuffer with the rendered Options contents. + */ + protected StringBuffer renderOptions(StringBuffer sb, int width, + Options options, int leftPad, + int descPad) + { + final String lpad = createPadding(leftPad); + final String dpad = createPadding(descPad); + + // first create list containing only -a,--aaa where + // -a is opt and --aaa is long opt; in parallel look for + // the longest opt string this list will be then used to + // sort options ascending + int max = 0; + StringBuffer optBuf; + List prefixList = new ArrayList(); + Option option; + List optList = options.helpOptions(); + + Collections.sort(optList, new StringBufferComparator()); + + for (Iterator i = optList.iterator(); i.hasNext();) + { + option = (Option) i.next(); + optBuf = new StringBuffer(8); + + if (option.getOpt() == null) + { + optBuf.append(lpad).append(" " + defaultLongOptPrefix) + .append(option.getLongOpt()); + } + else + { + optBuf.append(lpad).append(defaultOptPrefix) + .append(option.getOpt()); + + if (option.hasLongOpt()) + { + optBuf.append(',').append(defaultLongOptPrefix) + .append(option.getLongOpt()); + } + } + + if (option.hasArg()) + { + if (option.hasArgName()) + { + optBuf.append(" <").append(option.getArgName()).append(">"); + } + else + { + optBuf.append(' '); + } + } + + prefixList.add(optBuf); + max = (optBuf.length() > max) ? optBuf.length() : max; + } + + int x = 0; + + for (Iterator i = optList.iterator(); i.hasNext();) + { + option = (Option) i.next(); + optBuf = new StringBuffer(prefixList.get(x++).toString()); + + if (optBuf.length() < max) + { + optBuf.append(createPadding(max - optBuf.length())); + } + + optBuf.append(dpad); + + int nextLineTabStop = max + descPad; + + if (option.getDescription() != null) + { + optBuf.append(option.getDescription()); + } + + renderWrappedText(sb, width, nextLineTabStop, optBuf.toString()); + + if (i.hasNext()) + { + sb.append(defaultNewLine); + } + } + + return sb; + } + + /** + *

Render the specified text and return the rendered Options + * in a StringBuffer.

+ * + * @param sb The StringBuffer to place the rendered text into. + * @param width The number of characters to display per line + * @param nextLineTabStop The position on the next line for the first tab. + * @param text The text to be rendered. + * + * @return the StringBuffer with the rendered Options contents. + */ + protected StringBuffer renderWrappedText(StringBuffer sb, int width, + int nextLineTabStop, String text) + { + int pos = findWrapPos(text, width, 0); + + if (pos == -1) + { + sb.append(rtrim(text)); + return sb; - } + } + else + { + sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine); + } + + // all following lines must be padded with nextLineTabStop space + // characters + final String padding = createPadding(nextLineTabStop); + + while (true) + { + text = padding + text.substring(pos).trim(); + pos = findWrapPos(text, width, nextLineTabStop); - sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine); - } + if (pos == -1) + { + sb.append(text); - } + return sb; + } - /** - * Finds the next text wrap position after startPos for the text - * in sb with the column width width. - * The wrap point is the last postion before startPos+width having a whitespace - * character (space, \n, \r). - * - * @param sb text to be analyzed - * @param width width of the wrapped text - * @param startPos position from which to start the lookup whitespace character - * @return postion on which the text must be wrapped or -1 if the wrap position is at the end - * of the text - */ - protected int findWrapPos( String text, int width, int startPos ) - { - int pos = -1; - // the line ends before the max wrap pos or a new line char found - if ( ((pos = text.indexOf('\n', startPos)) != -1 && pos <= width) || - ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width) ) - { - return pos; - } - else if ( (startPos + width) >= text.length() ) - { - return -1; - } - - //look for the last whitespace character before startPos+width - pos = startPos + width; - char c; - while ( pos >= startPos && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' ) - { - --pos; - } - //if we found it - just return - if ( pos > startPos ) - { - return pos; - } - else - { - //must look for the first whitespace chearacter after startPos + width - pos = startPos + width; - while ( pos <= text.length() && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' ) - { - ++pos; - } - return pos == text.length() ? -1 : pos; - } - } - - protected String createPadding(int len) - { - StringBuffer sb = new StringBuffer(len); - for ( int i = 0; i < len; ++i ) - { - sb.append(' '); - } - return sb.toString(); - } - - protected String rtrim( String s ) - { - if ( s == null || s.length() == 0 ) - { - return s; - } - - int pos = s.length(); - while ( pos >= 0 && Character.isWhitespace(s.charAt(pos-1)) ) - { - --pos; - } - return s.substring(0, pos); - } - - // ------------------------------------------------------- Package protected - - // ----------------------------------------------------------------- Private - - // ----------------------------------------------------------- Inner classes + sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine); + } + } - private static class StringBufferComparator - implements Comparator + /** + * Finds the next text wrap position after startPos for the + * text in text with the column width width. + * The wrap point is the last postion before startPos+width having a + * whitespace character (space, \n, \r). + * + * @param text The text being searched for the wrap position + * @param width width of the wrapped text + * @param startPos position from which to start the lookup whitespace + * character + * @return postion on which the text must be wrapped or -1 if the wrap + * position is at the end of the text + */ + protected int findWrapPos(String text, int width, int startPos) + { + int pos = -1; + + // the line ends before the max wrap pos or a new line char found + if (((pos = text.indexOf('\n', startPos)) != -1 && pos <= width) + || ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width)) + { + return pos; + } + else if ((startPos + width) >= text.length()) + { + return -1; + } + + + // look for the last whitespace character before startPos+width + pos = startPos + width; + + char c; + + while ((pos >= startPos) && ((c = text.charAt(pos)) != ' ') + && (c != '\n') && (c != '\r')) + { + --pos; + } + + // if we found it - just return + if (pos > startPos) + { + return pos; + } + else + { + // must look for the first whitespace chearacter after startPos + // + width + pos = startPos + width; + + while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ') + && (c != '\n') && (c != '\r')) + { + ++pos; + } + + return (pos == text.length()) ? (-1) : pos; + } + } + + /** + *

Return a String of padding of length len.

+ * + * @param len The length of the String of padding to create. + * + * @return The String of padding + */ + protected String createPadding(int len) + { + StringBuffer sb = new StringBuffer(len); + + for (int i = 0; i < len; ++i) + { + sb.append(' '); + } + + return sb.toString(); + } + + /** + *

Remove the trailing whitespace from the specified String.

+ * + * @param s The String to remove the trailing padding from. + * + * @return The String of without the trailing padding + */ + protected String rtrim(String s) { - public int compare( Object o1, Object o2 ) + if ((s == null) || (s.length() == 0)) + { + return s; + } + + int pos = s.length(); + + while ((pos >= 0) && Character.isWhitespace(s.charAt(pos - 1))) + { + --pos; + } + + return s.substring(0, pos); + } + + // ------------------------------------------------------ Package protected + // ---------------------------------------------------------------- Private + // ---------------------------------------------------------- Inner classes + /** + *

This class implements the Comparator interface + * for comparing StringBuffers.

+ */ + private static class StringBufferComparator + implements Comparator { + + /** + *

Compares its two arguments for order. Returns a negative + * integer, zero, or a positive integer as the first argument + * is less than, equal to, or greater than the second.

+ * + * @param o1 The first StringBuffer to be compared. + * @param o2 The second StringBuffer to be compared. + * + * @return a negative integer, zero, or a positive integer as + * the first argument is less than, equal to, or greater than the + * second. + */ + public int compare(Object o1, Object o2) { String str1 = stripPrefix(o1.toString()); String str2 = stripPrefix(o2.toString()); + return (str1.compareTo(str2)); } + /** + *

Removes any leading hyphens from strOption.

+ * + * @param strOption The String from which to strip the leading + * hyphens. + * + * @return The String without any leading hyphens. + */ private String stripPrefix(String strOption) { // Strip any leading '-' characters int iStartIndex = strOption.lastIndexOf('-'); + if (iStartIndex == -1) { - iStartIndex = 0; + iStartIndex = 0; } - return strOption.substring(iStartIndex); + return strOption.substring(iStartIndex); } } -} +} \ No newline at end of file 1.4 +7 -6 jakarta-commons/cli/src/java/org/apache/commons/cli/MissingArgumentException.java Index: MissingArgumentException.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/MissingArgumentException.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- MissingArgumentException.java 19 Sep 2002 22:59:43 -0000 1.3 +++ MissingArgumentException.java 9 Dec 2002 23:47:24 -0000 1.4 @@ -58,7 +58,6 @@ * . * */ - package org.apache.commons.cli; /** @@ -68,15 +67,17 @@ * @author John Keyes (john at integralsource.com) * @see ParseException */ -public class MissingArgumentException extends ParseException { - +public class MissingArgumentException + extends ParseException { + /** *

Construct a new MissingArgumentException * with the specified detail message.

* * @param message the detail message */ - public MissingArgumentException( String message ) { - super( message ); + public MissingArgumentException(String message) + { + super(message); } -} +} \ No newline at end of file 1.4 +7 -6 jakarta-commons/cli/src/java/org/apache/commons/cli/MissingOptionException.java Index: MissingOptionException.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/MissingOptionException.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- MissingOptionException.java 19 Sep 2002 22:59:43 -0000 1.3 +++ MissingOptionException.java 9 Dec 2002 23:47:24 -0000 1.4 @@ -58,7 +58,6 @@ * . * */ - package org.apache.commons.cli; /** @@ -67,15 +66,17 @@ * @author John Keyes ( john at integralsource.com ) * @see ParseException */ -public class MissingOptionException extends ParseException { - +public class MissingOptionException + extends ParseException { + /** *

Construct a new MissingSelectedException * with the specified detail message.

* * @param message the detail message */ - public MissingOptionException( String message ) { - super( message ); + public MissingOptionException(String message) + { + super(message); } -} +} \ No newline at end of file 1.20 +216 -122 jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java Index: Option.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- Option.java 27 Nov 2002 23:17:41 -0000 1.19 +++ Option.java 9 Dec 2002 23:47:24 -0000 1.20 @@ -79,12 +79,13 @@ */ public class Option implements Cloneable { - /** constant that specifies the number of argument values has not been specified */ - public final static int UNINITIALIZED = -1; - + /** constant that specifies the number of argument values has + not been specified */ + public static final int UNINITIALIZED = -1; + /** constant that specifies the number of argument values is infinite */ - public final static int UNLIMITED_VALUES = -2; - + public static final int UNLIMITED_VALUES = -2; + /** opt the name of the option */ private String opt; @@ -110,14 +111,14 @@ * numberOfArgs specifies the number of argument values this option * can have */ - private int numberOfArgs = UNINITIALIZED; + private int numberOfArgs = UNINITIALIZED; /** the type of this Option */ private Object type; /** the list of argument values **/ private ArrayList values = new ArrayList(); - + /** the character that is the value separator */ private char valuesep; @@ -125,13 +126,15 @@ * Creates an Option using the specified parameters. * * @param opt short representation of the option - * @param hasArg specifies whether the Option takes an argument or not * @param description describes the function of the option + * + * @throws IllegalArgumentException if there are any non valid + * Option characters in opt. */ - public Option( String opt, String description ) - throws IllegalArgumentException + public Option(String opt, String description) + throws IllegalArgumentException { - this( opt, null, false, description ); + this(opt, null, false, description); } /** @@ -140,13 +143,16 @@ * @param opt short representation of the option * @param hasArg specifies whether the Option takes an argument or not * @param description describes the function of the option + * + * @throws IllegalArgumentException if there are any non valid + * Option characters in opt. */ - public Option( String opt, boolean hasArg, String description ) - throws IllegalArgumentException + public Option(String opt, boolean hasArg, String description) + throws IllegalArgumentException { - this( opt, null, hasArg, description ); + this(opt, null, hasArg, description); } - + /** *

Creates an Option using the specified parameters.

* @@ -154,25 +160,30 @@ * @param longOpt the long representation of the option * @param hasArg specifies whether the Option takes an argument or not * @param description describes the function of the option + * + * @throws IllegalArgumentException if there are any non valid + * Option characters in opt. */ - public Option( String opt, String longOpt, boolean hasArg, String description ) - throws IllegalArgumentException + public Option(String opt, String longOpt, boolean hasArg, + String description) + throws IllegalArgumentException { // ensure that the option is valid - OptionValidator.validateOption( opt ); + OptionValidator.validateOption(opt); - this.opt = opt; - this.longOpt = longOpt; + this.opt = opt; + this.longOpt = longOpt; // if hasArg is set then the number of arguments is 1 - if( hasArg ) { + if (hasArg) + { this.numberOfArgs = 1; } - this.hasArg = hasArg; - this.description = description; + this.hasArg = hasArg; + this.description = description; } - + /** *

Returns the id of this Option. This is only set when the * Option shortOpt is a single character. This is used for switch @@ -180,8 +191,9 @@ * * @return the id of this Option */ - public int getId( ) { - return getKey().charAt( 0 ); + public int getId() + { + return getKey().charAt(0); } /** @@ -189,11 +201,14 @@ * * @return the 'unique' Option identifier */ - String getKey() { + String getKey() + { // if 'opt' is null, then it is a 'long' option - if( opt == null ) { + if (opt == null) + { return this.longOpt; } + return this.opt; } @@ -206,7 +221,8 @@ * * @return The name of this option */ - public String getOpt() { + public String getOpt() + { return this.opt; } @@ -215,7 +231,8 @@ * * @return The type of this option */ - public Object getType() { + public Object getType() + { return this.type; } @@ -224,16 +241,18 @@ * * @param type the type of this Option */ - public void setType( Object type ) { + public void setType(Object type) + { this.type = type; } - + /** *

Retrieve the long name of this Option.

* * @return Long name of this option, or null, if there is no long name */ - public String getLongOpt() { + public String getLongOpt() + { return this.longOpt; } @@ -242,7 +261,8 @@ * * @param longOpt the long name of this Option */ - public void setLongOpt( String longOpt ) { + public void setLongOpt(String longOpt) + { this.longOpt = longOpt; } @@ -252,38 +272,43 @@ * @param optionalArg specifies whether the Option can have * an optional argument. */ - public void setOptionalArg( boolean optionalArg ) { + public void setOptionalArg(boolean optionalArg) + { this.optionalArg = optionalArg; } /** * @return whether this Option can have an optional argument */ - public boolean hasOptionalArg( ) { + public boolean hasOptionalArg() + { return this.optionalArg; } - + /**

Query to see if this Option has a long name

* * @return boolean flag indicating existence of a long name */ - public boolean hasLongOpt() { - return ( this.longOpt != null ); + public boolean hasLongOpt() + { + return (this.longOpt != null); } - + /**

Query to see if this Option requires an argument

* * @return boolean flag indicating if an argument is required */ - public boolean hasArg() { - return this.numberOfArgs > 0 || numberOfArgs == UNLIMITED_VALUES; + public boolean hasArg() + { + return (this.numberOfArgs > 0) || (numberOfArgs == UNLIMITED_VALUES); } - + /**

Retrieve the self-documenting description of this Option

* * @return The string description of this option */ - public String getDescription() { + public String getDescription() + { return this.description; } @@ -292,7 +317,8 @@ * * @return boolean flag indicating if an argument is required */ - public boolean isRequired() { + public boolean isRequired() + { return this.required; } @@ -301,7 +327,8 @@ * * @param required specifies whether this Option is mandatory */ - public void setRequired( boolean required ) { + public void setRequired(boolean required) + { this.required = required; } @@ -310,7 +337,8 @@ * * @param argName the display name for the argument value. */ - public void setArgName( String argName ) { + public void setArgName(String argName) + { this.argName = argName; } @@ -319,7 +347,8 @@ * * @return the display name for the argument value. */ - public String getArgName() { + public String getArgName() + { return this.argName; } @@ -330,8 +359,9 @@ * @return if the display name for the argument value has been * set. */ - public boolean hasArgName() { - return (this.argName != null && this.argName.length() > 0 ); + public boolean hasArgName() + { + return (this.argName != null && this.argName.length() > 0); } /** @@ -339,8 +369,10 @@ * * @return boolean flag indicating if multiple values are allowed */ - public boolean hasArgs() { - return this.numberOfArgs > 1 || this.numberOfArgs == UNLIMITED_VALUES; + public boolean hasArgs() + { + return (this.numberOfArgs > 1) + || (this.numberOfArgs == UNLIMITED_VALUES); } /** @@ -348,7 +380,8 @@ * * @param num the number of argument values */ - public void setArgs( int num ) { + public void setArgs(int num) + { this.numberOfArgs = num; } @@ -358,7 +391,8 @@ * * @param sep The value separator. */ - public void setValueSeparator( char sep ) { + public void setValueSeparator(char sep) + { this.valuesep = sep; } @@ -367,15 +401,19 @@ * * @return the value separator character. */ - public char getValueSeparator() { + public char getValueSeparator() + { return this.valuesep; } /** - * ... + *

Return whether this Option has specified a value separator.

+ * + * @return whether this Option has specified a value separator.

*/ - public boolean hasValueSeparator() { - return ( this.valuesep > 0 ); + public boolean hasValueSeparator() + { + return (this.valuesep > 0); } /** @@ -383,7 +421,8 @@ * * @return num the number of argument values */ - public int getArgs( ) { + public int getArgs() + { return this.numberOfArgs; } @@ -392,13 +431,15 @@ * * @param value is a/the value of this Option */ - void addValue( String value ) + void addValue(String value) { - switch( numberOfArgs ) { - case UNINITIALIZED: - throw new RuntimeException( "Cannot add value, no arguments allowed." ); - default: - processValue( value ); + switch (numberOfArgs) + { + case UNINITIALIZED: + throw new RuntimeException("NO_ARGS_ALLOWED"); + + default: + processValue(value); } } @@ -409,40 +450,47 @@ * in the value, parsing is ceased and the remaining characters are * added as a single token.

* + * @param value The String to be processed. + * * @since 1.0.1 */ - private void processValue( String value ) { - + private void processValue(String value) + { // this Option has a separator character - if( hasValueSeparator() ) { - + if (hasValueSeparator()) + { // get the separator character char sep = getValueSeparator(); // store the index for the value separator - int index = value.indexOf( sep ); + int index = value.indexOf(sep); // while there are more value separators - while( index != -1 ) { - + while (index != -1) + { // next value to be added - if( values.size() == numberOfArgs-1 ) { + if (values.size() == (numberOfArgs - 1)) + { break; - } + } + // store - add( value.substring( 0, index ) ); + add(value.substring(0, index)); + // parse - value = value.substring( index+1 ); + value = value.substring(index + 1); + // get new index - index = value.indexOf( sep ); + index = value.indexOf(sep); } } + // store the actual value or the last value that has been parsed - add( value ); + add(value); } /** @@ -451,70 +499,105 @@ * add the value. Otherwise, throw a runtime exception. *

* + * @param value The value to be added to this Option + * * @since 1.0.1 */ - private void add( String value ) { - if( numberOfArgs > 0 && values.size() > numberOfArgs-1 ) { - throw new RuntimeException( "Cannot add value, list full." ); + private void add(String value) + { + if ((numberOfArgs > 0) && (values.size() > (numberOfArgs - 1))) + { + throw new RuntimeException("Cannot add value, list full."); } + + // store value - this.values.add( value ); + this.values.add(value); } /** + * Returns the specified value of this Option or + * null if there is no value. + * * @return the value/first value of this Option or - * null if there are no values. + * null if there is no value. */ - public String getValue() { - return this.values.size()==0 ? null : (String)this.values.get( 0 ); + public String getValue() + { + return hasNoValues() ? null : (String) this.values.get(0); } /** + * Returns the specified value of this Option or + * null if there is no value. + * + * @param index The index of the value to be returned. + * * @return the specified value of this Option or - * null if there are no values. + * null if there is no value. + * + * @throws IndexOutOfBoundsException if index is less than 1 + * or greater than the number of the values for this Option. */ - public String getValue( int index ) - throws IndexOutOfBoundsException + public String getValue(int index) + throws IndexOutOfBoundsException { - return ( this.values.size()==0 ) ? null : (String)this.values.get( index ); + return hasNoValues() ? null : (String) this.values.get(index); } /** + * Returns the value/first value of this Option or the + * defaultValue if there is no value. + * + * @param defaultValue The value to be returned if ther + * is no value. + * * @return the value/first value of this Option or the * defaultValue if there are no values. */ - public String getValue( String defaultValue ) { - String value = getValue( ); - return ( value != null ) ? value : defaultValue; + public String getValue(String defaultValue) + { + String value = getValue(); + + return (value != null) ? value : defaultValue; } /** + * Return the values of this Option as a String array + * or null if there are no values + * * @return the values of this Option as a String array * or null if there are no values */ - public String[] getValues() { - return this.values.size()==0 ? null : (String[])this.values.toArray(new String[]{}); + public String[] getValues() + { + return hasNoValues() + ? null : (String[]) this.values.toArray(new String[] { }); } /** * @return the values of this Option as a List * or null if there are no values */ - public java.util.List getValuesList() { + public java.util.List getValuesList() + { return this.values; } /** * @return a copy of this Option */ - public Object clone() { - Option option = new Option( getOpt(), getDescription() ); - option.setArgs( getArgs() ); - option.setOptionalArg( hasOptionalArg() ); - option.setRequired( isRequired() ); - option.setLongOpt( getLongOpt() ); - option.setType( getType() ); - option.setValueSeparator( getValueSeparator() ); + public Object clone() + { + Option option = new Option(getOpt(), getDescription()); + + option.setArgs(getArgs()); + option.setOptionalArg(hasOptionalArg()); + option.setRequired(isRequired()); + option.setLongOpt(getLongOpt()); + option.setType(getType()); + option.setValueSeparator(getValueSeparator()); + return option; } @@ -523,32 +606,43 @@ * * @return Stringified form of this object */ - public String toString() { + public String toString() + { StringBuffer buf = new StringBuffer().append("[ option: "); - - buf.append( this.opt ); - - if ( this.longOpt != null ) { - buf.append(" ") - .append(this.longOpt); + + buf.append(this.opt); + + if (this.longOpt != null) + { + buf.append(" ").append(this.longOpt); } - + buf.append(" "); - - if ( hasArg ) { - buf.append( "+ARG" ); + + if (hasArg) + { + buf.append("+ARG"); } - - buf.append(" :: ") - .append( this.description ); - - if ( this.type != null ) { - buf.append(" :: ") - .append( this.type ); + + buf.append(" :: ").append(this.description); + + if (this.type != null) + { + buf.append(" :: ").append(this.type); } buf.append(" ]"); + return buf.toString(); } -} + /** + * Returns whether this Option has any values. + * + * @return whether this Option has any values. + */ + private boolean hasNoValues() + { + return this.values.size() == 0; + } +} \ No newline at end of file 1.15 +101 -53 jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java Index: OptionBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- OptionBuilder.java 19 Nov 2002 22:54:48 -0000 1.14 +++ OptionBuilder.java 9 Dec 2002 23:47:24 -0000 1.15 @@ -58,48 +58,59 @@ * . * */ - package org.apache.commons.cli; /** *

OptionBuilder allows the user to create Options using descriptive * methods.

*

Details on the Builder pattern can be found at - * http://c2.com/cgi-bin/wiki?BuilderPattern.

+ * + * http://c2.com/cgi-bin/wiki?BuilderPattern.

* - * @author John Keyes ( john at integralsource.com ) + * @author John Keyes (john at integralsource.com) * @since 1.0 */ public class OptionBuilder { /** long option */ private static String longopt; + /** option description */ private static String description; + /** argument name */ private static String argName; + /** is required? */ private static boolean required; + /** the number of arguments */ private static int numberOfArgs = Option.UNINITIALIZED; + /** option type */ private static Object type; + /** option can have an optional argument value */ private static boolean optionalArg; + /** value separator for argument value */ private static char valuesep; /** option builder instance */ private static OptionBuilder instance = new OptionBuilder(); - // private constructor - private OptionBuilder() { + /** + * private constructor to prevent instances being created + */ + private OptionBuilder() + { } /** *

Resets the member variables to their default values.

*/ - private static void reset() { + private static void reset() + { description = null; argName = "arg"; longopt = null; @@ -107,6 +118,7 @@ required = false; numberOfArgs = Option.UNINITIALIZED; + // PMM 9/6/02 - these were missing optionalArg = false; valuesep = (char) 0; @@ -118,8 +130,10 @@ * @param longopt the long option value * @return the OptionBuilder instance */ - public static OptionBuilder withLongOpt( String longopt ) { + public static OptionBuilder withLongOpt(String longopt) + { instance.longopt = longopt; + return instance; } @@ -128,8 +142,10 @@ * * @return the OptionBuilder instance */ - public static OptionBuilder hasArg( ) { + public static OptionBuilder hasArg() + { instance.numberOfArgs = 1; + return instance; } @@ -140,8 +156,10 @@ * @param hasArg if true then the Option has an argument value * @return the OptionBuilder instance */ - public static OptionBuilder hasArg( boolean hasArg ) { - instance.numberOfArgs = ( hasArg == true ) ? 1 : Option.UNINITIALIZED; + public static OptionBuilder hasArg(boolean hasArg) + { + instance.numberOfArgs = (hasArg == true) ? 1 : Option.UNINITIALIZED; + return instance; } @@ -152,8 +170,10 @@ * @param name the name for the argument value * @return the OptionBuilder instance */ - public static OptionBuilder withArgName( String name ) { + public static OptionBuilder withArgName(String name) + { instance.argName = name; + return instance; } @@ -162,8 +182,10 @@ * * @return the OptionBuilder instance */ - public static OptionBuilder isRequired( ) { + public static OptionBuilder isRequired() + { instance.required = true; + return instance; } @@ -173,18 +195,22 @@ * * Example: *
  -     * Option opt = OptionBuilder.withValueSeparator( ':' )
  -     *                           .create( 'D' );
  +     * Option opt = OptionBuilder.withValueSeparator(':')
  +     *                           .create('D');
        *
  -     * CommandLine line = parser.parse( args );
  -     * String propertyName = opt.getValue( 0 );
  -     * String propertyValue = opt.getValue( 1 );
  +     * CommandLine line = parser.parse(args);
  +     * String propertyName = opt.getValue(0);
  +     * String propertyValue = opt.getValue(1);
        * 
* + * @param sep The value separator to be used for the argument values. + * * @return the OptionBuilder instance */ - public static OptionBuilder withValueSeparator( char sep ) { + public static OptionBuilder withValueSeparator(char sep) + { instance.valuesep = sep; + return instance; } @@ -194,18 +220,20 @@ * * Example: *
  -     * Option opt = OptionBuilder.withValueSeparator( )
  -     *                           .create( 'D' );
  +     * Option opt = OptionBuilder.withValueSeparator()
  +     *                           .create('D');
        *
  -     * CommandLine line = parser.parse( args );
  -     * String propertyName = opt.getValue( 0 );
  -     * String propertyValue = opt.getValue( 1 );
  +     * CommandLine line = parser.parse(args);
  +     * String propertyName = opt.getValue(0);
  +     * String propertyValue = opt.getValue(1);
        * 
* * @return the OptionBuilder instance */ - public static OptionBuilder withValueSeparator( ) { + public static OptionBuilder withValueSeparator() + { instance.valuesep = '='; + return instance; } @@ -216,8 +244,10 @@ * @param required if true then the Option is required * @return the OptionBuilder instance */ - public static OptionBuilder isRequired( boolean required ) { + public static OptionBuilder isRequired(boolean required) + { instance.required = required; + return instance; } @@ -226,8 +256,10 @@ * * @return the OptionBuilder instance */ - public static OptionBuilder hasArgs( ) { + public static OptionBuilder hasArgs() + { instance.numberOfArgs = Option.UNLIMITED_VALUES; + return instance; } @@ -238,8 +270,10 @@ * @param num the number of args that the option can have * @return the OptionBuilder instance */ - public static OptionBuilder hasArgs( int num ) { + public static OptionBuilder hasArgs(int num) + { instance.numberOfArgs = num; + return instance; } @@ -248,9 +282,11 @@ * * @return the OptionBuilder instance */ - public static OptionBuilder hasOptionalArg( ) { + public static OptionBuilder hasOptionalArg() + { instance.numberOfArgs = 1; instance.optionalArg = true; + return instance; } @@ -260,9 +296,11 @@ * * @return the OptionBuilder instance */ - public static OptionBuilder hasOptionalArgs( ) { + public static OptionBuilder hasOptionalArgs() + { instance.numberOfArgs = Option.UNLIMITED_VALUES; instance.optionalArg = true; + return instance; } @@ -274,9 +312,11 @@ * the next Option created can have. * @return the OptionBuilder instance */ - public static OptionBuilder hasOptionalArgs( int numArgs ) { + public static OptionBuilder hasOptionalArgs(int numArgs) + { instance.numberOfArgs = numArgs; instance.optionalArg = true; + return instance; } @@ -287,8 +327,10 @@ * @param type the type of the Options argument value * @return the OptionBuilder instance */ - public static OptionBuilder withType( Object type ) { + public static OptionBuilder withType(Object type) + { instance.type = type; + return instance; } @@ -298,8 +340,10 @@ * @param description a description of the Option's purpose * @return the OptionBuilder instance */ - public static OptionBuilder withDescription( String description ) { + public static OptionBuilder withDescription(String description) + { instance.description = description; + return instance; } @@ -312,10 +356,10 @@ * @throws IllegalArgumentException if opt is not * a valid character. See Option. */ - public static Option create( char opt ) - throws IllegalArgumentException + public static Option create(char opt) + throws IllegalArgumentException { - return create( String.valueOf( opt ) ); + return create(String.valueOf(opt)); } /** @@ -325,14 +369,15 @@ * @throws IllegalArgumentException if longOpt has * not been set. */ - public static Option create() - throws IllegalArgumentException + public static Option create() + throws IllegalArgumentException { - if( longopt == null ) { - throw new IllegalArgumentException( "must specify longopt" ); + if (longopt == null) + { + throw new IllegalArgumentException("must specify longopt"); } - return create( null ); + return create(null); } /** @@ -345,20 +390,23 @@ * @throws IllegalArgumentException if opt is not * a valid character. See Option. */ - public static Option create( String opt ) - throws IllegalArgumentException + public static Option create(String opt) + throws IllegalArgumentException { // create the option - Option option = new Option( opt, description ); + Option option = new Option(opt, description); + // set the option properties - option.setLongOpt( longopt ); - option.setRequired( required ); - option.setOptionalArg( optionalArg ); - option.setArgs( numberOfArgs ); - option.setType( type ); - option.setValueSeparator( valuesep ); - option.setArgName( argName ); + option.setLongOpt(longopt); + option.setRequired(required); + option.setOptionalArg(optionalArg); + option.setArgs(numberOfArgs); + option.setType(type); + option.setValueSeparator(valuesep); + option.setArgName(argName); + + // reset the OptionBuilder properties instance.reset(); 1.8 +50 -33 jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java Index: OptionGroup.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- OptionGroup.java 19 Nov 2002 00:16:18 -0000 1.7 +++ OptionGroup.java 9 Dec 2002 23:47:24 -0000 1.8 @@ -58,7 +58,6 @@ * . * */ - package org.apache.commons.cli; import java.util.Collection; @@ -87,10 +86,12 @@ * @param opt the option to add to this group * @return this option group with opt added */ - public OptionGroup addOption(Option opt) { + public OptionGroup addOption(Option opt) + { // key - option name // value - the option - optionMap.put( opt.getKey(), opt ); + optionMap.put(opt.getKey(), opt); + return this; } @@ -98,7 +99,8 @@ * @return the names of the options in this group as a * Collection */ - public Collection getNames() { + public Collection getNames() + { // the key set is the collection of names return optionMap.keySet(); } @@ -106,7 +108,8 @@ /** * @return the options in this group as a Collection */ - public Collection getOptions() { + public Collection getOptions() + { // the values are the collection of options return optionMap.values(); } @@ -117,41 +120,47 @@ * @throws AlreadySelectedException if an option from this group has * already been selected. */ - public void setSelected(Option opt) throws AlreadySelectedException { + public void setSelected(Option opt) + throws AlreadySelectedException + { // if no option has already been selected or the // same option is being reselected then set the // selected member variable - - if ( this.selected == null || this.selected.equals( opt.getOpt() ) ) { + if ((this.selected == null) || this.selected.equals(opt.getOpt())) + { this.selected = opt.getOpt(); } - else { - throw new AlreadySelectedException( "an option from this group has " + - "already been selected: '" + - selected + "'"); + else + { + throw new AlreadySelectedException("an option from this group has " + + "already been selected: '" + + selected + "'"); } } /** * @return the selected option name */ - public String getSelected() { + public String getSelected() + { return selected; } /** * @param required specifies if this group is required */ - public void setRequired( boolean required ) { + public void setRequired(boolean required) + { this.required = required; } /** * Returns whether this option group is required. * - * @returns whether this option group is required + * @return whether this option group is required */ - public boolean isRequired() { + public boolean isRequired() + { return this.required; } @@ -159,32 +168,40 @@ *

Returns the stringified version of this OptionGroup.

* @return the stringified representation of this group */ - public String toString() { + public String toString() + { StringBuffer buff = new StringBuffer(); Iterator iter = getOptions().iterator(); - buff.append( "[" ); - while( iter.hasNext() ) { - Option option = (Option)iter.next(); - - if( option.getOpt() != null ) { - buff.append( "-" ); - buff.append( option.getOpt() ); + buff.append("["); + + while (iter.hasNext()) + { + Option option = (Option) iter.next(); + + if (option.getOpt() != null) + { + buff.append("-"); + buff.append(option.getOpt()); } - else { - buff.append( "--" ); - buff.append( option.getLongOpt() ); + else + { + buff.append("--"); + buff.append(option.getLongOpt()); } - buff.append( " " ); - buff.append( option.getDescription( ) ); - if( iter.hasNext() ) { - buff.append( ", " ); + buff.append(" "); + buff.append(option.getDescription()); + + if (iter.hasNext()) + { + buff.append(", "); } } - buff.append( "]" ); + + buff.append("]"); return buff.toString(); } -} +} \ No newline at end of file 1.2 +35 -20 jakarta-commons/cli/src/java/org/apache/commons/cli/OptionValidator.java Index: OptionValidator.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionValidator.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- OptionValidator.java 18 Nov 2002 08:41:26 -0000 1.1 +++ OptionValidator.java 9 Dec 2002 23:47:25 -0000 1.2 @@ -82,27 +82,39 @@ * @param opt The option string to validate * @throws IllegalArgumentException if the Option is not valid. */ - static void validateOption( String opt ) - throws IllegalArgumentException + static void validateOption(String opt) + throws IllegalArgumentException { // check that opt is not NULL - if( opt == null ) { + if (opt == null) + { return; } + // handle the single character opt - else if( opt.length() == 1 ) { - char ch = opt.charAt( 0 ); - if ( !isValidOpt( ch ) ) { - throw new IllegalArgumentException( "illegal option value '" - + ch + "'" ); + else if (opt.length() == 1) + { + char ch = opt.charAt(0); + + if (!isValidOpt(ch)) + { + throw new IllegalArgumentException("illegal option value '" + ch + + "'"); } } + // handle the multi character opt - else { + else + { char[] chars = opt.toCharArray(); - for( int i = 0; i < chars.length; i++ ) { - if( !isValidChar( chars[i] ) ) { - throw new IllegalArgumentException( "opt contains illegal character value '" + chars[i] + "'" ); + + for (int i = 0; i < chars.length; i++) + { + if (!isValidChar(chars[i])) + { + throw new IllegalArgumentException( + "opt contains illegal character value '" + chars[i] + + "'"); } } } @@ -112,10 +124,12 @@ *

Returns whether the specified character is a valid Option.

* * @param c the option to validate - * @return true if c is a letter, ' ', '?' or '@', otherwise false. + * @return true if c is a letter, ' ', '?' or '@', + * otherwise false. */ - private static boolean isValidOpt( char c ) { - return ( isValidChar( c ) || c == ' ' || c == '?' || c == '@' ); + private static boolean isValidOpt(char c) + { + return (isValidChar(c) || (c == ' ') || (c == '?') || c == '@'); } /** @@ -124,7 +138,8 @@ * @param c the character to validate * @return true if c is a letter. */ - private static boolean isValidChar( char c ) { - return Character.isJavaIdentifierPart( c ); + private static boolean isValidChar(char c) + { + return Character.isJavaIdentifierPart(c); } } 1.18 +85 -60 jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java Index: Options.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- Options.java 25 Nov 2002 23:43:40 -0000 1.17 +++ Options.java 9 Dec 2002 23:47:25 -0000 1.18 @@ -58,7 +58,6 @@ * . * */ - package org.apache.commons.cli; import java.util.ArrayList; @@ -68,7 +67,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.*; /**

Main entry-point into the library.

* @@ -88,20 +86,21 @@ public class Options { /** a map of the options with the character key */ - private Map shortOpts = new HashMap(); + private Map shortOpts = new HashMap(); /** a map of the options with the long key */ - private Map longOpts = new HashMap(); + private Map longOpts = new HashMap(); /** a map of the required options */ private List requiredOpts = new ArrayList(); - + /** a map of the option groups */ - private Map optionGroups = new HashMap(); + private Map optionGroups = new HashMap(); /**

Construct a new Options descriptor

*/ - public Options() { + public Options() + { } /** @@ -110,22 +109,27 @@ * @param group the OptionGroup that is to be added * @return the resulting Options instance */ - public Options addOptionGroup( OptionGroup group ) { + public Options addOptionGroup(OptionGroup group) + { Iterator options = group.getOptions().iterator(); - if( group.isRequired() ) { - requiredOpts.add( group ); + if (group.isRequired()) + { + requiredOpts.add(group); } - while( options.hasNext() ) { - Option option = (Option)options.next(); + while (options.hasNext()) + { + Option option = (Option) options.next(); + + // an Option cannot be required if it is in an // OptionGroup, either the group is required or // nothing is required - option.setRequired( false ); - addOption( option ); + option.setRequired(false); + addOption(option); - optionGroups.put( option.getKey(), group ); + optionGroups.put(option.getKey(), group); } return this; @@ -139,11 +143,13 @@ * @param description Self-documenting description * @return the resulting Options instance */ - public Options addOption(String opt, boolean hasArg, String description) { - addOption( opt, null, hasArg, description ); + public Options addOption(String opt, boolean hasArg, String description) + { + addOption(opt, null, hasArg, description); + return this; } - + /**

Add an option that contains a short-name and a long-name

*

It may be specified as requiring an argument.

* @@ -153,8 +159,11 @@ * @param description Self-documenting description * @return the resulting Options instance */ - public Options addOption(String opt, String longOpt, boolean hasArg, String description) { - addOption( new Option( opt, longOpt, hasArg, description ) ); + public Options addOption(String opt, String longOpt, boolean hasArg, + String description) + { + addOption(new Option(opt, longOpt, hasArg, description)); + return this; } @@ -164,29 +173,34 @@ * @param opt the option that is to be added * @return the resulting Options instance */ - public Options addOption(Option opt) { + public Options addOption(Option opt) + { String key = opt.getKey(); - - // add it to the long option list - if ( opt.hasLongOpt() ) { - longOpts.put( opt.getLongOpt(), opt ); - } - - // if the option is required add it to the required list - if ( opt.isRequired() && !requiredOpts.contains( key ) ) { - requiredOpts.add( key ); - } - shortOpts.put( key, opt ); + + // add it to the long option list + if (opt.hasLongOpt()) + { + longOpts.put(opt.getLongOpt(), opt); + } + + // if the option is required add it to the required list + if (opt.isRequired() && !requiredOpts.contains(key)) + { + requiredOpts.add(key); + } + + shortOpts.put(key, opt); return this; } - + /**

Retrieve a read-only list of options in this set

* * @return read-only Collection of {@link Option} objects in this descriptor */ - public Collection getOptions() { - return Collections.unmodifiableCollection( helpOptions() ); + public Collection getOptions() + { + return Collections.unmodifiableCollection(helpOptions()); } /** @@ -194,21 +208,25 @@ * * @return the List of Options */ - List helpOptions() { - List opts = new ArrayList( shortOpts.values() ); + List helpOptions() + { + List opts = new ArrayList(shortOpts.values()); // now look through the long opts to see if there are any Long-opt // only options Iterator iter = longOpts.values().iterator(); + while (iter.hasNext()) { Object item = iter.next(); + if (!opts.contains(item)) { opts.add(item); } } - return new ArrayList( opts ); + + return new ArrayList(opts); } /**

Returns the required options as a @@ -216,23 +234,26 @@ * * @return Collection of required options */ - public List getRequiredOptions() { + public List getRequiredOptions() + { return requiredOpts; } - + /**

Retrieve the named {@link Option}

* * @param opt short or long name of the {@link Option} * @return the option represented by opt */ - public Option getOption( String opt ) { + public Option getOption(String opt) + { + opt = Util.stripLeadingHyphens(opt); - opt = Util.stripLeadingHyphens( opt ); - - if( shortOpts.containsKey( opt ) ) { - return (Option) shortOpts.get( opt ); + if (shortOpts.containsKey(opt)) + { + return (Option) shortOpts.get(opt); } - return (Option) longOpts.get( opt ); + + return (Option) longOpts.get(opt); } /** @@ -243,9 +264,11 @@ * @return true if the named {@link Option} is a member * of this {@link Options} */ - public boolean hasOption( String opt ) { - opt = Util.stripLeadingHyphens( opt ); - return shortOpts.containsKey( opt ) || longOpts.containsKey( opt ); + public boolean hasOption(String opt) + { + opt = Util.stripLeadingHyphens(opt); + + return shortOpts.containsKey(opt) || longOpts.containsKey(opt); } /**

Returns the OptionGroup the opt @@ -255,23 +278,25 @@ * @return the OptionGroup if opt is part * of an OptionGroup, otherwise return null */ - public OptionGroup getOptionGroup( Option opt ) { - return (OptionGroup)optionGroups.get( opt.getKey() ); + public OptionGroup getOptionGroup(Option opt) + { + return (OptionGroup) optionGroups.get(opt.getKey()); } - + /**

Dump state, suitable for debugging.

* * @return Stringified form of this object */ - public String toString() { + public String toString() + { StringBuffer buf = new StringBuffer(); - + buf.append("[ Options: [ short "); - buf.append( shortOpts.toString() ); - buf.append( " ] [ long " ); - buf.append( longOpts ); - buf.append( " ]"); - + buf.append(shortOpts.toString()); + buf.append(" ] [ long "); + buf.append(longOpts); + buf.append(" ]"); + return buf.toString(); } -} +} \ No newline at end of file 1.3 +6 -7 jakarta-commons/cli/src/java/org/apache/commons/cli/ParseException.java Index: ParseException.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/ParseException.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ParseException.java 26 Aug 2002 20:15:02 -0000 1.2 +++ ParseException.java 9 Dec 2002 23:47:25 -0000 1.3 @@ -58,7 +58,6 @@ * . * */ - package org.apache.commons.cli; /** @@ -67,16 +66,16 @@ * @author bob mcwhirter (bob @ werken.com) * @version $Revision$ */ -public class ParseException extends Exception -{ - +public class ParseException extends Exception { + /** *

Construct a new ParseException * with the specified detail message.

* * @param message the detail message */ - public ParseException( String message ) { - super( message ); + public ParseException(String message) + { + super(message); } -} +} \ No newline at end of file 1.11 +187 -104 jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java Index: Parser.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- Parser.java 27 Nov 2002 23:22:02 -0000 1.10 +++ Parser.java 9 Dec 2002 23:47:25 -0000 1.11 @@ -58,7 +58,6 @@ * . * */ - package org.apache.commons.cli; import java.util.Arrays; @@ -66,7 +65,6 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import java.util.Map; import java.util.Properties; /** @@ -80,8 +78,10 @@ /** commandline instance */ private CommandLine cmd; + /** current Options */ private Options options; + /** list of required options strings */ private List requiredOptions; @@ -91,14 +91,13 @@ * method.

* * @param opts The Options to parse the arguments by. - * @param args The arguments that have to be flattened. + * @param arguments The arguments that have to be flattened. * @param stopAtNonOption specifies whether to stop * flattening when a non option has been encountered * @return a String array of the flattened arguments */ - abstract protected String[] flatten( Options opts, - String[] arguments, - boolean stopAtNonOption ); + protected abstract String[] flatten(Options opts, String[] arguments, + boolean stopAtNonOption); /** *

Parses the specified arguments @@ -110,11 +109,10 @@ * @throws ParseException if an error occurs when parsing the * arguments. */ - public CommandLine parse( Options options, - String[] arguments ) - throws ParseException + public CommandLine parse(Options options, String[] arguments) + throws ParseException { - return parse( options, arguments, null, false ); + return parse(options, arguments, null, false); } /** @@ -129,12 +127,11 @@ * @throws ParseException if there are any problems encountered * while parsing the command line tokens. */ - public CommandLine parse( Options options, - String[] arguments, - Properties properties ) - throws ParseException + public CommandLine parse(Options options, String[] arguments, + Properties properties) + throws ParseException { - return parse( options, arguments, properties, false ); + return parse(options, arguments, properties, false); } /** @@ -152,12 +149,11 @@ * @throws ParseException if an error occurs when parsing the * arguments. */ - public CommandLine parse( Options options, - String[] arguments, - boolean stopAtNonOption ) - throws ParseException + public CommandLine parse(Options options, String[] arguments, + boolean stopAtNonOption) + throws ParseException { - return parse( options, arguments, null, stopAtNonOption ); + return parse(options, arguments, null, stopAtNonOption); } /** @@ -167,117 +163,156 @@ * @param options the specified Options * @param arguments the command line arguments * @param properties command line option name-value pairs + * @param stopAtNonOption stop parsing the arguments when the first + * non option is encountered. + * * @return the list of atomic option and value tokens * * @throws ParseException if there are any problems encountered * while parsing the command line tokens. */ - public CommandLine parse( Options opts, - String[] arguments, - Properties properties, - boolean stopAtNonOption ) - throws ParseException + public CommandLine parse(Options options, String[] arguments, + Properties properties, boolean stopAtNonOption) + throws ParseException { // initialise members - options = opts; + this.options = options; requiredOptions = options.getRequiredOptions(); cmd = new CommandLine(); boolean eatTheRest = false; - if( arguments == null ) { + if (arguments == null) + { arguments = new String[0]; } - List tokenList = Arrays.asList( flatten( opts, arguments, stopAtNonOption ) ); + List tokenList = Arrays.asList(flatten(this.options, + arguments, + stopAtNonOption)); + ListIterator iterator = tokenList.listIterator(); // process each flattened token - while( iterator.hasNext() ) { - String t = (String)iterator.next(); + while (iterator.hasNext()) + { + String t = (String) iterator.next(); // the value is the double-dash - if( "--".equals( t ) ) { + if ("--".equals(t)) + { eatTheRest = true; } + // the value is a single dash - else if( "-".equals( t ) ) { - if( stopAtNonOption ) { + else if ("-".equals(t)) + { + if (stopAtNonOption) + { eatTheRest = true; } - else { - cmd.addArg(t ); + else + { + cmd.addArg(t); } } + // the value is an option - else if( t.startsWith( "-" ) ) { - if ( stopAtNonOption && !options.hasOption( t ) ) { + else if (t.startsWith("-")) + { + if (stopAtNonOption && !options.hasOption(t)) + { eatTheRest = true; - cmd.addArg( t ); + cmd.addArg(t); } - else { - processOption( t, iterator ); + else + { + processOption(t, iterator); } } + // the value is an argument - else { - cmd.addArg( t ); - if( stopAtNonOption ) { + else + { + cmd.addArg(t); + + if (stopAtNonOption) + { eatTheRest = true; } } // eat the remaining tokens - if( eatTheRest ) { - while( iterator.hasNext() ) { - String str = (String)iterator.next(); + if (eatTheRest) + { + while (iterator.hasNext()) + { + String str = (String) iterator.next(); + // ensure only one double-dash is added - if( !"--".equals( str ) ) { - cmd.addArg( str ); + if (!"--".equals(str)) + { + cmd.addArg(str); } } } } - processProperties( properties ); + + processProperties(properties); checkRequiredOptions(); + return cmd; } /** - *

Sets the values of Options using the values in properties.

+ *

Sets the values of Options using the values in + * properties.

+ * + * @param properties The value properties to be processed. */ - private void processProperties( Properties properties ) { - if( properties == null ) { + private void processProperties(Properties properties) + { + if (properties == null) + { return; } - for( Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) { + for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) + { String option = e.nextElement().toString(); - if( !cmd.hasOption( option ) ) { - Option opt = options.getOption( option ); + + if (!cmd.hasOption(option)) + { + Option opt = options.getOption(option); // get the value from the properties instance - String value = properties.getProperty( option ); + String value = properties.getProperty(option); - if( opt.hasArgs() ) { - if( opt.getValues() == null || opt.getValues().length == 0 ) { - try { - opt.addValue( value ); + if (opt.hasArgs()) + { + if ((opt.getValues() == null) + || (opt.getValues().length == 0)) + { + try + { + opt.addValue(value); } - catch( RuntimeException exp ) { + catch (RuntimeException exp) + { // if we cannot add the value don't worry about it } } } - else if ( ! ( "yes".equalsIgnoreCase( value ) || - "true".equalsIgnoreCase( value ) || - "1".equalsIgnoreCase( value) ) ) { + else if (!("yes".equalsIgnoreCase(value) + || "true".equalsIgnoreCase(value) + || "1".equalsIgnoreCase(value))) + { // if the value is not yes, true or 1 then don't add the // option to the CommandLine break; } - cmd.addOption( opt ); + cmd.addOption(opt); } } } @@ -285,93 +320,141 @@ /** *

Throws a {@link MissingOptionException} if all of the * required options are no present.

+ * + * @throws MissingOptionException if any of the required Options + * are not present. */ private void checkRequiredOptions() - throws MissingOptionException + throws MissingOptionException { - // if there are required options that have not been // processsed - if( requiredOptions.size() > 0 ) { + if (requiredOptions.size() > 0) + { Iterator iter = requiredOptions.iterator(); StringBuffer buff = new StringBuffer(); // loop through the required options - while( iter.hasNext() ) { - buff.append( iter.next() ); + while (iter.hasNext()) + { + buff.append(iter.next()); } - throw new MissingOptionException( buff.toString() ); + throw new MissingOptionException(buff.toString()); } } - public void processArgs( Option opt, ListIterator iter ) - throws ParseException + /** + *

Process the argument values for the specified Option + * opt using the values retrieved from the + * specified iterator iter. + * + * @param opt The current Option + * @param iter The iterator over the flattened command line + * Options. + * + * @throws ParseException if an argument value is required + * and it is has not been found. + */ + public void processArgs(Option opt, ListIterator iter) + throws ParseException { // loop until an option is found - while( iter.hasNext() ) { - String str = (String)iter.next(); + while (iter.hasNext()) + { + String str = (String) iter.next(); // found an Option - if( options.hasOption( str ) ) { + if (options.hasOption(str)) + { iter.previous(); + break; } + // found a value - else { - try { - opt.addValue( str ) ; + else + { + try + { + opt.addValue(str); } - catch( RuntimeException exp ) { + catch (RuntimeException exp) + { iter.previous(); + break; } } } - if( opt.getValues() == null && !opt.hasOptionalArg() ) { - throw new MissingArgumentException( "no argument for:" + opt.getKey() ); + if ((opt.getValues() == null) && !opt.hasOptionalArg()) + { + throw new MissingArgumentException("no argument for:" + + opt.getKey()); } } - private void processOption( String arg, ListIterator iter ) - throws ParseException + /** + *

Process the Option specified by arg + * using the values retrieved from the specfied iterator + * iter. + * + * @param arg The String value representing an Option + * @param iter The iterator over the flattened command + * line arguments. + * + * @throws ParseException if arg does not + * represent an Option + */ + private void processOption(String arg, ListIterator iter) + throws ParseException { // get the option represented by arg Option opt = null; - boolean hasOption = options.hasOption( arg ); + boolean hasOption = options.hasOption(arg); // if there is no option throw an UnrecognisedOptionException - if( !hasOption ) { - throw new UnrecognizedOptionException("Unrecognized option: " + arg); - } - else { - opt = (Option) options.getOption( arg ); + if (!hasOption) + { + throw new UnrecognizedOptionException("Unrecognized option: " + + arg); + } + else + { + opt = (Option) options.getOption(arg); } // if the option is a required option remove the option from // the requiredOptions list - if ( opt.isRequired() ) { - requiredOptions.remove( opt.getKey() ); + if (opt.isRequired()) + { + requiredOptions.remove(opt.getKey()); } // if the option is in an OptionGroup make that option the selected // option of the group - if ( options.getOptionGroup( opt ) != null ) { - OptionGroup group = ( OptionGroup ) options.getOptionGroup( opt ); - if( group.isRequired() ) { - requiredOptions.remove( group ); + if (options.getOptionGroup(opt) != null) + { + OptionGroup group = (OptionGroup) options.getOptionGroup(opt); + + if (group.isRequired()) + { + requiredOptions.remove(group); } - group.setSelected( opt ); + + group.setSelected(opt); } // if the option takes an argument value - if ( opt.hasArg() ) { - processArgs( opt, iter ); + if (opt.hasArg()) + { + processArgs(opt, iter); } + // set the option on the command line - cmd.addOption( opt ); + cmd.addOption(opt); } } 1.5 +87 -58 jakarta-commons/cli/src/java/org/apache/commons/cli/PatternOptionBuilder.java Index: PatternOptionBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/PatternOptionBuilder.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- PatternOptionBuilder.java 30 Jul 2002 23:06:21 -0000 1.4 +++ PatternOptionBuilder.java 9 Dec 2002 23:47:25 -0000 1.5 @@ -58,42 +58,50 @@ * . * */ - package org.apache.commons.cli; /** * Allows Options to be created from a single String. * + * @todo These need to break out to OptionType and also + * to be pluggable. * * @author Henri Yandell (bayard @ generationjava.com) * @version $Revision$ */ public class PatternOptionBuilder { - /// TODO: These need to break out to OptionType and also to be pluggable. - /** String class */ - public static final Class STRING_VALUE = java.lang.String.class; + public static final Class STRING_VALUE = java.lang.String.class; + /** Object class */ - public static final Class OBJECT_VALUE = java.lang.Object.class; + public static final Class OBJECT_VALUE = java.lang.Object.class; + /** Number class */ - public static final Class NUMBER_VALUE = java.lang.Number.class; + public static final Class NUMBER_VALUE = java.lang.Number.class; + /** Date class */ - public static final Class DATE_VALUE = java.util.Date.class; + public static final Class DATE_VALUE = java.util.Date.class; + /** Class class */ - public static final Class CLASS_VALUE = java.lang.Class.class; + public static final Class CLASS_VALUE = java.lang.Class.class; + + /// can we do this one?? + // is meant to check that the file exists, else it errors. + // ie) it's for reading not writing. -/// can we do this one?? -// is meant to check that the file exists, else it errors. -// ie) it's for reading not writing. /** FileInputStream class */ - public static final Class EXISTING_FILE_VALUE = java.io.FileInputStream.class; + public static final Class EXISTING_FILE_VALUE = + java.io.FileInputStream.class; + /** File class */ - public static final Class FILE_VALUE = java.io.File.class; + public static final Class FILE_VALUE = java.io.File.class; + /** File array class */ - public static final Class FILES_VALUE = java.io.File[].class; + public static final Class FILES_VALUE = java.io.File[].class; + /** URL class */ - public static final Class URL_VALUE = java.net.URL.class; + public static final Class URL_VALUE = java.net.URL.class; /** *

Retrieve the class that ch represents.

@@ -101,29 +109,48 @@ * @param ch the specified character * @return The class that ch represents */ - public static Object getValueClass(char ch) { - if (ch == '@') { + public static Object getValueClass(char ch) + { + if (ch == '@') + { return PatternOptionBuilder.OBJECT_VALUE; - } else if (ch == ':') { + } + else if (ch == ':') + { return PatternOptionBuilder.STRING_VALUE; - } else if (ch == '%') { + } + else if (ch == '%') + { return PatternOptionBuilder.NUMBER_VALUE; - } else if (ch == '+') { + } + else if (ch == '+') + { return PatternOptionBuilder.CLASS_VALUE; - } else if (ch == '#') { + } + else if (ch == '#') + { return PatternOptionBuilder.DATE_VALUE; - } else if (ch == '<') { + } + else if (ch == '<') + { return PatternOptionBuilder.EXISTING_FILE_VALUE; - } else if (ch == '>') { + } + else if (ch == '>') + { return PatternOptionBuilder.FILE_VALUE; - } else if (ch == '*') { + } + else if (ch == '*') + { return PatternOptionBuilder.FILES_VALUE; - } else if (ch == '/') { + } + else if (ch == '/') + { return PatternOptionBuilder.URL_VALUE; } + return null; } - + /** *

Returns whether ch is a value code, i.e. * whether it represents a class in a pattern.

@@ -131,23 +158,18 @@ * @param ch the specified character * @return true if ch is a value code, otherwise false. */ - public static boolean isValueCode(char ch) { - if( (ch != '@') && - (ch != ':') && - (ch != '%') && - (ch != '+') && - (ch != '#') && - (ch != '<') && - (ch != '>') && - (ch != '*') && - (ch != '/') - ) + public static boolean isValueCode(char ch) + { + if ((ch != '@') && (ch != ':') && (ch != '%') && (ch != '+') + && (ch != '#') && (ch != '<') && (ch != '>') && (ch != '*') + && (ch != '/')) { return false; } + return true; - } - + } + /** *

Returns the {@link Options} instance represented by * pattern.

@@ -155,7 +177,8 @@ * @param pattern the pattern string * @return The {@link Options} instance */ - public static Options parsePattern(String pattern) { + public static Options parsePattern(String pattern) + { int sz = pattern.length(); char opt = ' '; @@ -164,41 +187,47 @@ Object type = null; Options options = new Options(); - - for(int i=0; itokens entries, set eatTheRest * to false and set currentOption to null.

*/ - private void init() { + private void init() + { eatTheRest = false; tokens.clear(); currentOption = null; @@ -121,7 +123,7 @@ * characters in length and the first character is "-" then * we need to burst the entry to determine its constituents. For more * information on the bursting algorithm see - * {@link PosixParser#burstToken( String, boolean) burstToken}. + * {@link PosixParser#burstToken(String, boolean) burstToken}. *
  • if the current arguments entry is not handled * by any of the previous rules, then the entry is added to the list * of processed tokens.
  • @@ -134,61 +136,73 @@ * when an non option is found. * @return The flattened arguments String array. */ - protected String[] flatten( Options options, - String[] arguments, - boolean stopAtNonOption ) + protected String[] flatten(Options options, String[] arguments, + boolean stopAtNonOption) { init(); this.options = options; // an iterator for the command line tokens - Iterator iter = Arrays.asList( arguments ).iterator(); + Iterator iter = Arrays.asList(arguments).iterator(); String token = null; - - // process each command line token - while ( iter.hasNext() ) { + // process each command line token + while (iter.hasNext()) + { // get the next command line token token = (String) iter.next(); // handle SPECIAL TOKEN - if( token.startsWith( "--" ) ) { - if( token.indexOf( '=' ) != -1 ) { - tokens.add( token.substring( 0, token.indexOf( '=' ) ) ); - tokens.add( token.substring( token.indexOf( '=' ) + 1, - token.length() ) ); - } - else { - tokens.add( token ); - } + if (token.startsWith("--")) + { + if (token.indexOf('=') != -1) + { + tokens.add(token.substring(0, token.indexOf('='))); + tokens.add(token.substring(token.indexOf('=') + 1, + token.length())); + } + else + { + tokens.add(token); + } } + // single hyphen - else if( "-".equals( token ) ) { - processSingleHyphen( token ); + else if ("-".equals(token)) + { + processSingleHyphen(token); } - else if( token.startsWith( "-" ) ) { + else if (token.startsWith("-")) + { int tokenLength = token.length(); - if( tokenLength == 2 ) { - processOptionToken( token, stopAtNonOption ); + + if (tokenLength == 2) + { + processOptionToken(token, stopAtNonOption); } + // requires bursting - else { - burstToken( token, stopAtNonOption ); + else + { + burstToken(token, stopAtNonOption); } } - else { - if( stopAtNonOption ) { - process( token ); + else + { + if (stopAtNonOption) + { + process(token); } - else { - tokens.add( token ); + else + { + tokens.add(token); } } - gobble( iter ); + gobble(iter); } - return (String[])tokens.toArray( new String[] {} ); + return (String[]) tokens.toArray(new String[] { }); } /** @@ -196,10 +210,13 @@ * * @param iter An iterator over the remaining tokens */ - private void gobble( Iterator iter ) { - if( eatTheRest ) { - while( iter.hasNext() ) { - tokens.add( iter.next() ); + private void gobble(Iterator iter) + { + if (eatTheRest) + { + while (iter.hasNext()) + { + tokens.add(iter.next()); } } } @@ -217,20 +234,25 @@ * * @param value The current token */ - private void process( String value ) { - if( currentOption != null && currentOption.hasArg() ) { - if( currentOption.hasArg() ) { - tokens.add( value ); + private void process(String value) + { + if ((currentOption != null) && currentOption.hasArg()) + { + if (currentOption.hasArg()) + { + tokens.add(value); currentOption = null; } - else if (currentOption.hasArgs() ) { - tokens.add( value ); + else if (currentOption.hasArgs()) + { + tokens.add(value); } } - else { + else + { eatTheRest = true; - tokens.add( "--" ); - tokens.add( value ); + tokens.add("--"); + tokens.add(value); } } @@ -240,8 +262,9 @@ * * @param hyphen The hyphen token */ - private void processSingleHyphen( String hyphen ) { - tokens.add( hyphen ); + private void processSingleHyphen(String hyphen) + { + tokens.add(hyphen); } /** @@ -256,12 +279,15 @@ * @param stopAtNonOption Specifies whether flattening should halt * at the first non option. */ - private void processOptionToken( String token, boolean stopAtNonOption ) { - if( this.options.hasOption( token ) ) { - currentOption = this.options.getOption( token ); - tokens.add( token ); + private void processOptionToken(String token, boolean stopAtNonOption) + { + if (this.options.hasOption(token)) + { + currentOption = this.options.getOption(token); + tokens.add(token); } - else if( stopAtNonOption ) { + else if (stopAtNonOption) + { eatTheRest = true; } } @@ -270,7 +296,7 @@ *

    Breaks token into its constituent parts * using the following algorithm. *

      - *
    • ignore the first character ("-" )
    • + *
    • ignore the first character ("-")
    • *
    • foreach remaining character check if an {@link Option} * exists with that id.
    • *
    • if an {@link Option} does exist then add that character @@ -287,27 +313,39 @@ * character prepended with "-".
    • *
    *

    + * + * @param token The current token to be burst + * @param stopAtNonOption Specifies whether to stop processing + * at the first non-Option encountered. */ - protected void burstToken( String token, boolean stopAtNonOption ) { + protected void burstToken(String token, boolean stopAtNonOption) + { int tokenLength = token.length(); - for( int i = 1; i < tokenLength; i++) { - String ch = String.valueOf( token.charAt( i ) ); - boolean hasOption = options.hasOption( ch ); - - if( hasOption ) { - tokens.add( "-" + ch ); - currentOption = options.getOption( ch ); - if( currentOption.hasArg() && token.length()!=i+1 ) { - tokens.add( token.substring( i+1 ) ); + for (int i = 1; i < tokenLength; i++) + { + String ch = String.valueOf(token.charAt(i)); + boolean hasOption = options.hasOption(ch); + + if (hasOption) + { + tokens.add("-" + ch); + currentOption = options.getOption(ch); + + if (currentOption.hasArg() && (token.length() != (i + 1))) + { + tokens.add(token.substring(i + 1)); + break; } } - else if( stopAtNonOption ) { - process( token.substring( i ) ); + else if (stopAtNonOption) + { + process(token.substring(i)); } - else { - tokens.add( "-" + ch ); + else + { + tokens.add("-" + ch); } } } 1.6 +103 -54 jakarta-commons/cli/src/java/org/apache/commons/cli/TypeHandler.java Index: TypeHandler.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/TypeHandler.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TypeHandler.java 26 Jul 2002 14:12:34 -0000 1.5 +++ TypeHandler.java 9 Dec 2002 23:47:25 -0000 1.6 @@ -58,12 +58,13 @@ * . * */ - package org.apache.commons.cli; import java.io.File; -import java.net.URL; + import java.net.MalformedURLException; +import java.net.URL; + import java.util.Date; import org.apache.commons.lang.NumberUtils; @@ -76,7 +77,7 @@ * * @author Henri Yandell (bayard @ generationjava.com) * @version $Revision$ - */ + */ public class TypeHandler { /** @@ -88,8 +89,9 @@ * @return The instance of obj initialised with * the value of str. */ - public static Object createValue(String str, Object obj) { - return createValue(str, (Class)obj); + public static Object createValue(String str, Object obj) + { + return createValue(str, (Class) obj); } /** @@ -101,34 +103,46 @@ * @return The instance of clazz initialised with * the value of str. */ - public static Object createValue(String str, Class clazz) { - if( PatternOptionBuilder.STRING_VALUE == clazz) { + public static Object createValue(String str, Class clazz) + { + if (PatternOptionBuilder.STRING_VALUE == clazz) + { return str; - } else - if( PatternOptionBuilder.OBJECT_VALUE == clazz) { + } + else if (PatternOptionBuilder.OBJECT_VALUE == clazz) + { return createObject(str); - } else - if( PatternOptionBuilder.NUMBER_VALUE == clazz) { + } + else if (PatternOptionBuilder.NUMBER_VALUE == clazz) + { return createNumber(str); - } else - if( PatternOptionBuilder.DATE_VALUE == clazz) { + } + else if (PatternOptionBuilder.DATE_VALUE == clazz) + { return createDate(str); - } else - if( PatternOptionBuilder.CLASS_VALUE == clazz) { + } + else if (PatternOptionBuilder.CLASS_VALUE == clazz) + { return createClass(str); - } else - if( PatternOptionBuilder.FILE_VALUE == clazz) { + } + else if (PatternOptionBuilder.FILE_VALUE == clazz) + { return createFile(str); - } else - if( PatternOptionBuilder.EXISTING_FILE_VALUE == clazz) { + } + else if (PatternOptionBuilder.EXISTING_FILE_VALUE == clazz) + { return createFile(str); - } else - if( PatternOptionBuilder.FILES_VALUE == clazz) { + } + else if (PatternOptionBuilder.FILES_VALUE == clazz) + { return createFiles(str); - } else - if( PatternOptionBuilder.URL_VALUE == clazz) { + } + else if (PatternOptionBuilder.URL_VALUE == clazz) + { return createURL(str); - } else { + } + else + { return null; } } @@ -137,27 +151,42 @@ *

    Create an Object from the classname and empty constructor.

    * * @param str the argument value - * @return the initialised object, or null if it couldn't create the Object. + * @return the initialised object, or null if it couldn't create + * the Object. */ - public static Object createObject(String str) { + public static Object createObject(String str) + { Class cl = null; - try { + + try + { cl = Class.forName(str); - } catch (ClassNotFoundException cnfe) { - System.err.println("Unable to find: "+str); + } + catch (ClassNotFoundException cnfe) + { + System.err.println("Unable to find: " + str); + return null; } Object instance = null; - try { + try + { instance = cl.newInstance(); - } catch (InstantiationException cnfe) { - System.err.println("InstantiationException; Unable to create: "+str); + } + catch (InstantiationException cnfe) + { + System.err.println("InstantiationException; Unable to create: " + + str); + return null; } - catch (IllegalAccessException cnfe) { - System.err.println("IllegalAccessException; Unable to create: "+str); + catch (IllegalAccessException cnfe) + { + System.err.println("IllegalAccessException; Unable to create: " + + str); + return null; } @@ -171,13 +200,18 @@ * @return the number represented by str, if str * is not a number, null is returned. */ - public static Number createNumber(String str) { + public static Number createNumber(String str) + { // Needs to be able to create - try { + try + { // do searching for decimal point etc, but atm just make an Integer return NumberUtils.createNumber(str); - } catch (NumberFormatException nfe) { + } + catch (NumberFormatException nfe) + { System.err.println(nfe.getMessage()); + return null; } } @@ -188,11 +222,16 @@ * @param str the class name * @return The class if it is found, otherwise return null */ - public static Class createClass(String str) { - try { + public static Class createClass(String str) + { + try + { return Class.forName(str); - } catch (ClassNotFoundException cnfe) { - System.err.println("Unable to find: "+str); + } + catch (ClassNotFoundException cnfe) + { + System.err.println("Unable to find: " + str); + return null; } } @@ -204,11 +243,15 @@ * @return The date if str is a valid date string, * otherwise return null. */ - public static Date createDate(String str) { + public static Date createDate(String str) + { Date date = null; - if(date == null) { - System.err.println("Unable to parse: "+str); + + if (date == null) + { + System.err.println("Unable to parse: " + str); } + return date; } @@ -219,11 +262,16 @@ * @return The URL is str is well-formed, otherwise * return null. */ - public static URL createURL(String str) { - try { + public static URL createURL(String str) + { + try + { return new URL(str); - } catch (MalformedURLException mue) { - System.err.println("Unable to parse: "+str); + } + catch (MalformedURLException mue) + { + System.err.println("Unable to parse: " + str); + return null; } } @@ -234,7 +282,8 @@ * @param str the File location * @return The file represented by str. */ - public static File createFile(String str) { + public static File createFile(String str) + { return new File(str); } @@ -244,10 +293,10 @@ * @param str the paths to the files * @return The File[] represented by str. */ - public static File[] createFiles(String str) { -// to implement/port: -// return FileW.findFiles(str); + public static File[] createFiles(String str) + { + // to implement/port: + // return FileW.findFiles(str); return null; } - -} +} \ No newline at end of file 1.3 +7 -6 jakarta-commons/cli/src/java/org/apache/commons/cli/UnrecognizedOptionException.java Index: UnrecognizedOptionException.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/UnrecognizedOptionException.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- UnrecognizedOptionException.java 26 Aug 2002 20:15:02 -0000 1.2 +++ UnrecognizedOptionException.java 9 Dec 2002 23:47:25 -0000 1.3 @@ -58,7 +58,6 @@ * . * */ - package org.apache.commons.cli; /** @@ -68,15 +67,17 @@ * @author bob mcwhiter (bob @ werken.com) * @version $Revision$ */ -public class UnrecognizedOptionException extends ParseException { - +public class UnrecognizedOptionException + extends ParseException { + /** *

    Construct a new UnrecognizedArgumentException * with the specified detail message.

    * * @param message the detail message */ - public UnrecognizedOptionException( String message ) { - super( message ); + public UnrecognizedOptionException(String message) + { + super(message); } -} +} \ No newline at end of file 1.2 +22 -9 jakarta-commons/cli/src/java/org/apache/commons/cli/Util.java Index: Util.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Util.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Util.java 18 Nov 2002 08:41:26 -0000 1.1 +++ Util.java 9 Dec 2002 23:47:25 -0000 1.2 @@ -67,13 +67,26 @@ */ class Util { - static String stripLeadingHyphens( String str ) { - if( str.startsWith( "--" ) ) { - return str.substring( 2, str.length() ); + /** + *

    Remove the hyphens from the begining of str and + * return the new String.

    + * + * @param str The string from which the hyphens should be removed. + * + * @return the hyphens from the begining of str and + * return the new String. + */ + static String stripLeadingHyphens(String str) + { + if (str.startsWith("--")) + { + return str.substring(2, str.length()); } - else if ( str.startsWith( "-" ) ) { - return str.substring( 1, str.length() ); + else if (str.startsWith("-")) + { + return str.substring(1, str.length()); } + return str; } - } \ No newline at end of file +} \ No newline at end of file 1.6 +11 -11 jakarta-commons/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java Index: TestHelpFormatter.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TestHelpFormatter.java 4 Oct 2002 11:06:58 -0000 1.5 +++ TestHelpFormatter.java 9 Dec 2002 23:47:26 -0000 1.6 @@ -64,18 +64,18 @@ String text = "This is a test."; String expected; - expected = "This is a" + hf.defaultNewLine + "test."; + expected = "This is a" + hf.getNewLine() + "test."; hf.renderWrappedText(sb, 12, 0, text); assertEquals("single line text", expected, sb.toString()); sb.setLength(0); - expected = "This is a" + hf.defaultNewLine + " test."; + expected = "This is a" + hf.getNewLine() + " test."; hf.renderWrappedText(sb, 12, 4, text); assertEquals("single line padded text", expected, sb.toString()); text = - "aaaa aaaa aaaa" + hf.defaultNewLine + - "aaaaaa" + hf.defaultNewLine + + "aaaa aaaa aaaa" + hf.getNewLine() + + "aaaaaa" + hf.getNewLine() + "aaaaa"; expected = text; @@ -84,8 +84,8 @@ assertEquals("multi line text", expected, sb.toString()); expected = - "aaaa aaaa aaaa" + hf.defaultNewLine + - " aaaaaa" + hf.defaultNewLine + + "aaaa aaaa aaaa" + hf.getNewLine() + + " aaaaaa" + hf.getNewLine() + " aaaaa"; sb.setLength(0); hf.renderWrappedText(sb, 16, 4, text); @@ -111,7 +111,7 @@ int nextLineTabStop = leftPad+descPad+"-a".length(); expected = - lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.defaultNewLine + + lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.getNewLine() + hf.createPadding(nextLineTabStop) + "aaaa aaaa"; sb.setLength(0); hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad); @@ -126,7 +126,7 @@ nextLineTabStop = leftPad+descPad+"-a,--aaa".length(); expected = - lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine + + lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + hf.createPadding(nextLineTabStop) + "dddd dddd"; sb.setLength(0); hf.renderOptions(sb, 25, options, leftPad, descPad); @@ -136,9 +136,9 @@ addOption("a", "aaa", false, "dddd dddd dddd dddd"). addOption("b", false, "feeee eeee eeee eeee"); expected = - lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine + - hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.defaultNewLine + - lpad + "-b " + dpad + "feeee eeee" + hf.defaultNewLine + + lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + + hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.getNewLine() + + lpad + "-b " + dpad + "feeee eeee" + hf.getNewLine() + hf.createPadding(nextLineTabStop) + "eeee eeee"; sb.setLength(0); hf.renderOptions(sb, 25, options, leftPad, descPad); -- To unsubscribe, e-mail: For additional commands, e-mail: