Return-Path:
Construct a new Creates a command line. Query to see if an option has been set. Retrieve the argument, if any, of an option. Retrieve the argument, if any, of an option. Retrieve any left-over non-recognized options and arguments Add an option to the command line. The values of
* the option are stored. Resets the members to their original state i.e. remove
* all of Prints the usage statement for the specified application. Print the help for Print the help for Print the help for Print the help for Print the help for Print the help for Print the help for Print the help for Prints the usage statement for the specified application. Print the cmdLineSyntax to the specified writer, using the
+ * specified width. Print the help for the specified Options to the specified writer,
+ * using the specified width, left padding and description padding. Print the specified text to the specified PrintWriter. Print the specified text to the specified PrintWriter. Render the specified Options and return the rendered Options
+ * in a StringBuffer. Render the specified text and return the rendered Options
+ * in a StringBuffer. Return a String of padding of length Remove the trailing whitespace from the specified String. This class implements the 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. Removes any leading hyphens from Construct a new Construct a new Creates an Option using the specified parameters. 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. Query to see if this Option has a long name Query to see if this Option requires an argument Retrieve the self-documenting description of this Option Return whether this Option has specified a value separator.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 @@
/**
* 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));
}
-
+
/**
* 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;
}
-
+
/**
* 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;
}
-
+
/**
* 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);
}
-
+
/**
* 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 @@
* tokens
entries.
*/
- private void init() {
+ private void init()
+ {
tokens.clear();
}
@@ -98,90 +96,123 @@
*
*
* options
with the specified
+ * command line syntax.options
with the specified
+ * command line syntax.options
with the specified
+ * command line syntax.options
with the specified
+ * command line syntax.options
with the specified
+ * command line syntax.options
with the specified
+ * command line syntax.options
with the specified
+ * command line syntax.options
with the specified
+ * command line syntax.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;
+ }
+ }
+
+ /**
+ * len
.Comparator
interface
+ * for comparing StringBuffers.strOption
.MissingArgumentException
* with the specified detail message.MissingSelectedException
* with the specified detail message.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);
}
-
+
/**
* 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;
}
-
+
/**
*
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 @@
* 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 @@
* 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 ifc
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 @@
* 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 @@ *Construct a new ParseException
* with the specified detail message.
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
.
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 @@
*
Retrieve the class that ch
represents.
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.
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
.
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}.
* arguments
entry is not handled
* by any of the previous rules, then the entry is added to the list
* of processed tokens.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.
*
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 bystr
, 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 @@
* Construct a new UnrecognizedArgumentException
* with the specified detail message.
Remove the hyphens from the begining of str
and
+ * return the new String.
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: