You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jk...@apache.org on 2002/06/17 01:00:59 UTC
cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli BuildTest.java HelpFormatterExamples.java OptionGroupTest.java ParseRequiredTest.java ParseTest.java PatternOptionBuilderTest.java TestHelpFormatter.java ValueTest.java ValuesTest.java
jkeyes 2002/06/16 16:00:59
Modified: cli/src/java/org/apache/commons/cli CommandLine.java
HelpFormatter.java Option.java Options.java
PatternOptionBuilder.java
cli/src/test/org/apache/commons/cli BuildTest.java
HelpFormatterExamples.java OptionGroupTest.java
ParseRequiredTest.java ParseTest.java
PatternOptionBuilderTest.java
TestHelpFormatter.java ValueTest.java
ValuesTest.java
Added: cli/src/java/org/apache/commons/cli CommandLineParser.java
PosixParser.java
Log:
configurable parser support added, PosixParser added
Revision Changes Path
1.2 +20 -19 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CommandLine.java 10 Jun 2002 18:01:15 -0000 1.1
+++ CommandLine.java 16 Jun 2002 23:00:58 -0000 1.2
@@ -69,8 +69,8 @@
/** <p>Represents list of arguments parsed against
* a {@link Options} descriptor.<p>
*
- * <p>It allows querying of a boolean {@link #hasOption(char opt)},
- * in addition to retrieving the {@link #getOptionValue(char opt)}
+ * <p>It allows querying of a boolean {@link #hasOption(String opt)},
+ * in addition to retrieving the {@link #getOptionValue(String opt)}
* for options requiring arguments.</p>
*
* <p>Additionally, any left-over or unrecognized arguments,
@@ -78,6 +78,8 @@
*
* @author bob mcwhirter (bob @ werken.com)
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
+ * @author John Keyes (jbjk at mac.com)
+ *
* @version $Revision$
*/
public class CommandLine {
@@ -102,13 +104,13 @@
* @param opt Short single-character name of the option
* @return true if set, false if not
*/
- public boolean hasOption(char opt) {
- return options.containsKey( new Character(opt) );
+ public boolean hasOption(String opt) {
+ return options.containsKey( opt );
}
- public Object getOptionObject(char opt) {
- String[] result = (String[])options.get( new Character(opt) );
- Object type = types.get( new Character(opt) );
+ public Object getOptionObject(String opt) {
+ String[] result = (String[])options.get( opt );
+ Object type = types.get( opt );
String res = result == null ? null : result[0];
if(res == null) {
return null;
@@ -121,8 +123,8 @@
* @param opt Short single-character name of the option
* @return Value of the argument if option is set, and has an argument, else null.
*/
- public String getOptionValue(char opt) {
- String[] result = (String[])options.get( new Character(opt) );
+ public String getOptionValue(String opt) {
+ String[] result = (String[])options.get( opt );
return result == null ? null : result[0];
}
@@ -131,8 +133,8 @@
* @param opt Single-character name of the option
* @return An array of values if the option is set, and has an argument, else null.
*/
- public String[] getOptionValues(char opt) {
- String[] result = (String[])options.get( new Character(opt) );
+ public String[] getOptionValues(String opt) {
+ String[] result = (String[])options.get( opt );
return result == null ? null : result;
}
@@ -142,7 +144,7 @@
* @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, else null.
*/
- public String getOptionValue(char opt, String defaultValue) {
+ public String getOptionValue(String opt, String defaultValue) {
String answer = getOptionValue(opt);
return (answer != null) ? answer : defaultValue;
}
@@ -196,8 +198,8 @@
*
* @param opt the processed option
*/
- void setOpt(char opt) {
- options.put( new Character(opt), null );
+ void setOpt(String opt) {
+ options.put( opt, null );
}
/**
@@ -207,8 +209,8 @@
* @param opt the processed option
* @param value the value of the option
*/
- void setOpt(char opt, String value) {
- options.put( new Character(opt), value );
+ void setOpt(String opt, String value) {
+ options.put( opt, value );
}
/**
@@ -218,8 +220,7 @@
* @param opt the processed option
*/
void setOpt(Option opt) {
- Character chr = new Character( opt.getOpt() );
- options.put( chr, opt.getValues() );
- types.put( chr, opt.getType() );
+ options.put( opt.getOpt(), opt.getValues() );
+ types.put( opt.getOpt(), opt.getType() );
}
}
1.2 +1 -1 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HelpFormatter.java 10 Jun 2002 18:01:15 -0000 1.1
+++ HelpFormatter.java 16 Jun 2002 23:00:58 -0000 1.2
@@ -199,7 +199,7 @@
{
optBuf = (StringBuffer) i.next();
opt = optBuf.charAt(optOffset);
- option = options.getOption(opt);
+ option = options.getOption( "" + opt);
renderWrappedText(sb, width, nextLineTabStop,
optBuf.append(option.getDescription()).toString());
if ( i.hasNext() )
1.2 +28 -15 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Option.java 10 Jun 2002 18:01:15 -0000 1.1
+++ Option.java 16 Jun 2002 23:00:58 -0000 1.2
@@ -92,7 +92,7 @@
public class Option {
/** opt the single character representation of the option */
- private Character opt = null;
+ private String opt = null;
/** longOpt is the long representation of the option */
private String longOpt = null;
@@ -119,36 +119,36 @@
/**
* Creates an Option using the specified parameters.
*
- * @param opt character representation of the option
+ * @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
*/
- public Option(char opt, boolean hasArg, String description) {
+ public Option(String opt, boolean hasArg, String description) {
this(opt, null, hasArg, description, false, false);
}
/**
* Creates an Option using the specified parameters.
*
- * @param opt character representation of the option
+ * @param opt short representation of the option
* @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
*/
- public Option(char opt, String longOpt, boolean hasArg, String description) {
+ public Option(String opt, String longOpt, boolean hasArg, String description) {
this(opt, longOpt, hasArg, description, false, false );
}
/**
* Creates an Option using the specified parameters.
*
- * @param opt character representation of the option
+ * @param opt short representation of the option
* @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
* @param required specifies whether the option is required or not
*/
- public Option(char opt, String longOpt, boolean hasArg, String description,
+ public Option(String opt, String longOpt, boolean hasArg, String description,
boolean required ) {
this(opt, longOpt, hasArg, description, required, false );
}
@@ -156,7 +156,7 @@
/**
* Creates an Option using the specified parameters.
*
- * @param opt character representation of the option
+ * @param opt short representation of the option
* @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
@@ -164,13 +164,26 @@
* @param multipleArgs specifies whether the option has multiple argument
* values
*/
- public Option(char opt, String longOpt, boolean hasArg, String description,
+ public Option(String opt, String longOpt, boolean hasArg, String description,
boolean required, boolean multipleArgs ) {
this(opt, longOpt, hasArg, description, required, multipleArgs, null );
}
- public Option(char opt, String longOpt, boolean hasArg, String description,
+
+ /**
+ * Creates an Option using the specified parameters.
+ *
+ * @param opt short representation of the option
+ * @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
+ * @param required specifies whether the option is required or not
+ * @param multipleArgs specifies whether the option has multiple argument
+ * values
+ * @param type specifies the type of the option
+ */
+ public Option(String opt, String longOpt, boolean hasArg, String description,
boolean required, boolean multipleArgs, Object type ) {
- this.opt = new Character( opt );
+ this.opt = opt;
this.longOpt = longOpt;
this.hasArg = hasArg;
this.description = description;
@@ -182,14 +195,14 @@
/** <p>Retrieve the single-character name of this Option</p>
*
* <p>It is this character which can be used with
- * {@link CommandLine#hasOption(char opt)} and
- * {@link CommandLine#getOptionValue(char opt)} to check
+ * {@link CommandLine#hasOption(String opt)} and
+ * {@link CommandLine#getOptionValue(String opt)} to check
* for existence and argument.<p>
*
* @return Single character name of this option
*/
- public char getOpt() {
- return this.opt.charValue();
+ public String getOpt() {
+ return this.opt;
}
public Object getType() {
1.2 +45 -120 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Options.java 10 Jun 2002 18:01:15 -0000 1.1
+++ Options.java 16 Jun 2002 23:00:58 -0000 1.2
@@ -67,7 +67,6 @@
import java.util.List;
import java.util.ListIterator;
import java.util.ArrayList;
-import java.util.LinkedList;
import java.util.Iterator;
import java.util.Collections;
@@ -87,7 +86,12 @@
* @version $Revision$
*/
public class Options {
-
+
+ private String defaultParserImpl = "org.apache.commons.cli.PosixParser";
+ private String parserImpl = defaultParserImpl;
+
+ private CommandLineParser parser;
+
/** the list of options */
private List options = new ArrayList();
@@ -106,6 +110,21 @@
/** <p>Construct a new Options descriptor</p>
*/
public Options() {
+ parserImpl = System.getProperty( "org.apache.commons.cli.parser" );
+ try {
+ parser = (CommandLineParser)Class.forName( parserImpl ).newInstance();
+ }
+ catch( Exception exp ) {
+ // could not create according to parserImpl so default to
+ // PosixParser
+ try {
+ parser = (CommandLineParser)Class.forName( defaultParserImpl ).newInstance();
+ }
+ catch( Exception exp2 ) {
+ // this will not happen ?
+ }
+ }
+ System.out.println( parser.getClass().getName() );
}
/**
@@ -134,7 +153,7 @@
* @param description Self-documenting description
* @return the resulting Options instance
*/
- public Options addOption(char opt, boolean hasArg, String description) {
+ public Options addOption(String opt, boolean hasArg, String description) {
addOption( opt, null, hasArg, description, false );
return this;
}
@@ -148,7 +167,7 @@
* @param description Self-documenting description
* @return the resulting Options instance
*/
- public Options addOption(char opt, String longOpt, boolean hasArg, String description) {
+ public Options addOption(String opt, String longOpt, boolean hasArg, String description) {
addOption( opt, longOpt, hasArg, description, false );
return this;
}
@@ -163,7 +182,7 @@
* @param required specifies if this option is required
* @return the resulting Options instance
*/
- public Options addOption(char opt, String longOpt, boolean hasArg, String description,
+ public Options addOption(String opt, String longOpt, boolean hasArg, String description,
boolean required) {
addOption( new Option(opt, longOpt, hasArg, description, required) );
return this;
@@ -180,13 +199,25 @@
* @param multipleArgs specifies if this option can accept multiple argument values
* @return the resulting Options instance
*/
- public Options addOption(char opt, String longOpt, boolean hasArg, String description,
+ public Options addOption(String opt, String longOpt, boolean hasArg, String description,
boolean required, boolean multipleArgs) {
addOption( new Option(opt, longOpt, hasArg, description, required, multipleArgs) );
return this;
}
- public Options addOption(char opt, String longOpt, boolean hasArg, String description,
+ /** <p>Add an option that contains a short-name and a long-name</p>
+ * <p>It may be specified as requiring an argument.</p>
+ *
+ * @param opt Short single-character name of the option.
+ * @param longOpt Long multi-character name of the option.
+ * @param hasArg flag signally if an argument is required after this option
+ * @param description Self-documenting description
+ * @param required specifies if this option is required
+ * @param multipleArgs specifies if this option can accept multiple argument values
+ * @param type specifies the type for the value of the option
+ * @return the resulting Options instance
+ */
+ public Options addOption(String opt, String longOpt, boolean hasArg, String description,
boolean required, boolean multipleArgs, Object type) {
addOption( new Option(opt, longOpt, hasArg, description, required, multipleArgs, type) );
return this;
@@ -362,7 +393,7 @@
MissingOptionException, AlreadySelectedException {
CommandLine cl = new CommandLine();
- List args = burst( inArgs, stopAtNonOption );
+ List args = parser.parse( this, inArgs, stopAtNonOption );
ListIterator argIter = args.listIterator();
String eachArg = null;
@@ -506,121 +537,12 @@
}
else {
- //option.addValue( null );
cl.setOpt( option );
}
}
}
/**
- * <p>Processes the argument list according to POSIX command line
- * processing rules.</p>
- *
- * @param inArgs the argument list
- * @param stopAtNonOption stop processing when the first non option
- * is encountered.
- * @return the processed list of arguments.
- */
- private List burst(List inArgs, boolean stopAtNonOption) {
- List args = new LinkedList();
-
- Iterator argIter = inArgs.iterator();
- String eachArg = null;
-
- boolean eatTheRest = false;
-
- while ( argIter.hasNext() ) {
- eachArg = (String) argIter.next();
-
- if ( eachArg.equals("--") ) {
- // Look for -- to indicate end-of-options, and
- // just stuff it, along with everything past it
- // into the returned list.
-
- args.add( eachArg );
- eatTheRest = true;
- }
- else if ( eachArg.startsWith("--") ) {
- // It's a long-option, so doesn't need any
- // bursting applied to it.
-
- args.add( eachArg );
- }
- else if ( eachArg.startsWith("-") ) {
- // It might be a short arg needing
- // some bursting
-
- if ( eachArg.length() == 1) {
- // It's not really an option, so
- // just drop it on the list
-
- if ( stopAtNonOption ) {
- eatTheRest = true;
- }
- else {
- args.add( eachArg );
- }
- }
- else if ( eachArg.length() == 2 ) {
- // No bursting required
-
- args.add( eachArg );
- }
- else {
- // Needs bursting. Figure out
- // if we have multiple options,
- // or maybe an option plus an arg,
- // or some combination thereof.
-
- for ( int i = 1 ; i < eachArg.length() ; ++i ) {
- String optStr = "-" + eachArg.charAt(i);
- Option opt = (Option) shortOpts.get( optStr );
-
- if ( (opt != null) && (opt.hasArg()) ) {
- // If the current option has an argument,
- // then consider the rest of the eachArg
- // to be that argument.
-
- args.add( optStr );
-
- if ( (i+1) < eachArg.length() ) {
- String optArg = eachArg.substring(i+1);
- args.add( optArg );
- }
- break;
- }
- else {
- // No argument, so prepend the single dash,
- // and then drop it into the arglist.
-
- args.add( optStr );
- }
- }
- }
- }
- else {
- // It's just a normal non-option arg,
- // so dump it into the list of returned
- // values.
-
- args.add( eachArg );
-
- if ( stopAtNonOption ) {
- eatTheRest = true;
- }
- }
-
- if ( eatTheRest ) {
- while ( argIter.hasNext() ) {
- args.add( argIter.next() );
- }
- }
- }
-
- return args;
- }
-
- /**
* <p>Adds the option to the necessary member lists</p>
*
* @param opt the option that is to be added
@@ -654,7 +576,10 @@
* @param opt short single-character name of the {@link Option}
* @return the option represented by opt
*/
- public Option getOption(char opt) {
+ public Option getOption(String opt) {
+ if( opt.startsWith( "--" ) ) {
+ return (Option) longOpts.get( opt );
+ }
return (Option) shortOpts.get( "-" + opt );
}
@@ -663,9 +588,9 @@
* @param longOpt long name of the {@link Option}
* @return the option represented by longOpt
*/
- public Option getOption(String longOpt) {
+ /*public Option getOption(String longOpt) {
return (Option) longOpts.get( longOpt );
- }
+ } */
/** <p>Dump state, suitable for debugging.</p>
*
1.2 +2 -2 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PatternOptionBuilder.java 10 Jun 2002 18:01:15 -0000 1.1
+++ PatternOptionBuilder.java 16 Jun 2002 23:00:58 -0000 1.2
@@ -144,7 +144,7 @@
if(!isValueCode(ch)) {
if(opt != ' ') {
// we have a previous one to deal with
- options.addOption(opt, null, (type != null), "", required, false, type);
+ options.addOption("" + opt, null, (type != null), "", required, false, type);
required = false;
type = null;
opt = ' ';
@@ -160,7 +160,7 @@
if(opt != ' ') {
// we have a final one to deal with
- options.addOption(opt, null, (type != null), "", required, false, type);
+ options.addOption( "" + opt, null, (type != null), "", required, false, type);
}
return options;
1.1 jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLineParser.java
Index: CommandLineParser.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLineParser.java,v 1.1 2002/06/16 23:00:57 jkeyes Exp $
* $Revision: 1.1 $
* $Date: 2002/06/16 23:00:57 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.cli;
import java.util.List;
/**
* @author John Keyes (jbjk at mac.com)
*/
public interface CommandLineParser {
/**
* Parse the arguments according to the specified options.
* @param options the specified Options
* @param arguments the command line arguments
* @return the list of atomic option and value tokens
*/
public List parse( Options options, List arguments );
/**
* Parse the arguments according to the specified options.
* @param options the specified Options
* @param arguments the command line arguments
* @param stopAtNonOption specifies whether to continue parsing the
* arguments if a non option is encountered.
* @return the list of atomic option and value tokens
*/
public List parse( Options options, List arguments, boolean stopAtNonOption );
}
1.1 jakarta-commons/cli/src/java/org/apache/commons/cli/PosixParser.java
Index: PosixParser.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/PosixParser.java,v 1.1 2002/06/16 23:00:58 jkeyes Exp $
* $Revision: 1.1 $
* $Date: 2002/06/16 23:00:58 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.cli;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* PosixParser parses the command line arguments using the Posix style.
* For example, -buildfile can only be interpreted as the option
* 'b' with value 'uildfile' or it could be interpreted as the options
* 'b','u','i','l','d','f','i','l','e'.
*
* @author John Keyes (jbjk at mac.com)
*/
public class PosixParser implements CommandLineParser {
/**
* Parse the arguments according to the specified options.
* @param options the specified Options
* @param arguments the command line arguments
* @return the list of atomic option and value tokens
*/
public List parse( Options options, List arguments ) {
return parse( options, arguments, false );
}
/**
* Parse the arguments according to the specified options.
* @param options the specified Options
* @param arguments the command line arguments
* @param stopAtNonOption specifies whether to continue parsing the
* arguments if a non option is encountered.
* @return the list of atomic option and value tokens
*/
public List parse( Options options, List arguments, boolean stopAtNonOption ) {
List args = new LinkedList();
Iterator argIter = arguments.iterator();
String eachArg = null;
boolean eatTheRest = false;
while ( argIter.hasNext() ) {
eachArg = (String) argIter.next();
if ( eachArg.equals("--") ) {
// Look for -- to indicate end-of-options, and
// just stuff it, along with everything past it
// into the returned list.
args.add( eachArg );
eatTheRest = true;
}
else if ( eachArg.startsWith("--") ) {
// It's a long-option, so doesn't need any
// bursting applied to it.
args.add( eachArg );
}
else if ( eachArg.startsWith("-") ) {
// It might be a short arg needing
// some bursting
if ( eachArg.length() == 1) {
// It's not really an option, so
// just drop it on the list
if ( stopAtNonOption ) {
eatTheRest = true;
}
else {
args.add( eachArg );
}
}
else if ( eachArg.length() == 2 ) {
// No bursting required
args.add( eachArg );
}
else {
// Needs bursting. Figure out
// if we have multiple options,
// or maybe an option plus an arg,
// or some combination thereof.
for ( int i = 1 ; i < eachArg.length() ; ++i ) {
String optStr = "-" + eachArg.charAt(i);
Option opt = (Option) options.getOption( "" + eachArg.charAt(i) );
if ( (opt != null) && (opt.hasArg()) ) {
// If the current option has an argument,
// then consider the rest of the eachArg
// to be that argument.
args.add( optStr );
if ( (i+1) < eachArg.length() ) {
String optArg = eachArg.substring(i+1);
args.add( optArg );
}
break;
}
else {
// No argument, so prepend the single dash,
// and then drop it into the arglist.
args.add( optStr );
}
}
}
}
else {
// It's just a normal non-option arg,
// so dump it into the list of returned
// values.
args.add( eachArg );
if ( stopAtNonOption ) {
eatTheRest = true;
}
}
if ( eatTheRest ) {
while ( argIter.hasNext() ) {
args.add( argIter.next() );
}
}
}
return args;
}
}
1.2 +10 -10 jakarta-commons/cli/src/test/org/apache/commons/cli/BuildTest.java
Index: BuildTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/BuildTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BuildTest.java 10 Jun 2002 18:01:15 -0000 1.1
+++ BuildTest.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -40,11 +40,11 @@
{
Options opts = new Options();
- opts.addOption('a',
+ opts.addOption("a",
false,
"toggle -a");
- opts.addOption('b',
+ opts.addOption("b",
true,
"toggle -b");
/*
@@ -68,15 +68,15 @@
public void testDuplicateSimple()
{
Options opts = new Options();
- opts.addOption('a',
+ opts.addOption("a",
false,
"toggle -a");
- opts.addOption('a',
+ opts.addOption("a",
true,
"toggle -a*");
- assertEquals( "last one in wins", "toggle -a*", opts.getOption('a').getDescription() );
+ assertEquals( "last one in wins", "toggle -a*", opts.getOption("a").getDescription() );
/*
try
{
@@ -100,12 +100,12 @@
{
Options opts = new Options();
- opts.addOption('a',
+ opts.addOption("a",
"--a",
false,
"toggle -a");
- opts.addOption('b',
+ opts.addOption("b",
"--b",
true,
"set -b");
@@ -133,16 +133,16 @@
public void testDuplicateLong()
{
Options opts = new Options();
- opts.addOption('a',
+ opts.addOption("a",
"--a",
false,
"toggle -a");
- opts.addOption('a',
+ opts.addOption("a",
"--a",
false,
"toggle -a*");
- assertEquals( "last one in wins", "toggle -a*", opts.getOption('a').getDescription() );
+ assertEquals( "last one in wins", "toggle -a*", opts.getOption("a").getDescription() );
/*
try
{
1.2 +23 -23 jakarta-commons/cli/src/test/org/apache/commons/cli/HelpFormatterExamples.java
Index: HelpFormatterExamples.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/HelpFormatterExamples.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HelpFormatterExamples.java 10 Jun 2002 18:01:15 -0000 1.1
+++ HelpFormatterExamples.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -39,39 +39,39 @@
"[-msystem] [-pstring] [-Llocale] [-eextension] [section] page ...";
Options opts =
new Options().
- addOption('a', "all", false, "find all matching manual pages.").
- addOption('d', "debug", false, "emit debugging messages.").
- addOption('e', "extension", false, "limit search to extension type 'extension'.").
- addOption('f', "whatis", false, "equivalent to whatis.").
- addOption('k', "apropos", false, "equivalent to apropos.").
- addOption('w', "location", false, "print physical location of man page(s).").
- addOption('l', "local-file", false, "interpret 'page' argument(s) as local filename(s)").
- addOption('u', "update", false, "force a cache consistency check.").
+ addOption("a", "all", false, "find all matching manual pages.").
+ addOption("d", "debug", false, "emit debugging messages.").
+ addOption("e", "extension", false, "limit search to extension type 'extension'.").
+ addOption("f", "whatis", false, "equivalent to whatis.").
+ addOption("k", "apropos", false, "equivalent to apropos.").
+ addOption("w", "location", false, "print physical location of man page(s).").
+ addOption("l", "local-file", false, "interpret 'page' argument(s) as local filename(s)").
+ addOption("u", "update", false, "force a cache consistency check.").
//FIXME - should generate -r,--prompt string
- addOption('r', "prompt", true, "provide 'less' pager with prompt.").
- addOption('c', "catman", false, "used by catman to reformat out of date cat pages.").
- addOption('7', "ascii", false, "display ASCII translation or certain latin1 chars.").
- addOption('t', "troff", false, "use troff format pages.").
+ addOption("r", "prompt", true, "provide 'less' pager with prompt.").
+ addOption("c", "catman", false, "used by catman to reformat out of date cat pages.").
+ addOption("7", "ascii", false, "display ASCII translation or certain latin1 chars.").
+ addOption("t", "troff", false, "use troff format pages.").
//FIXME - should generate -T,--troff-device device
- addOption('T', "troff-device", true, "use groff with selected device.").
- addOption('Z', "ditroff", false, "use groff with selected device.").
- addOption('D', "default", false, "reset all options to their default values.").
+ addOption("T", "troff-device", true, "use groff with selected device.").
+ addOption("Z", "ditroff", false, "use groff with selected device.").
+ addOption("D", "default", false, "reset all options to their default values.").
//FIXME - should generate -M,--manpath path
- addOption('M', "manpath", true, "set search path for manual pages to 'path'.").
+ addOption("M", "manpath", true, "set search path for manual pages to 'path'.").
//FIXME - should generate -P,--pager pager
- addOption('P', "pager", true, "use program 'pager' to display output.").
+ addOption("P", "pager", true, "use program 'pager' to display output.").
//FIXME - should generate -S,--sections list
- addOption('S', "sections", true, "use colon separated section list.").
+ addOption("S", "sections", true, "use colon separated section list.").
//FIXME - should generate -m,--systems system
- addOption('m', "systems", true, "search for man pages from other unix system(s).").
+ addOption("m", "systems", true, "search for man pages from other unix system(s).").
//FIXME - should generate -L,--locale locale
- addOption('L', "locale", true, "defaine the locale for this particular man search.").
+ addOption("L", "locale", true, "defaine the locale for this particular man search.").
//FIXME - should generate -p,--preprocessor string
- addOption('p', "preprocessor", true, "string indicates which preprocessor to run.\n" +
+ addOption("p", "preprocessor", true, "string indicates which preprocessor to run.\n" +
" e - [n]eqn p - pic t - tbl\n" +
" g - grap r - refer v - vgrind").
- addOption('V', "version", false, "show version.").
- addOption('h', "help", false, "show this usage message.");
+ addOption("V", "version", false, "show version.").
+ addOption("h", "help", false, "show this usage message.");
HelpFormatter hf = new HelpFormatter();
//hf.printHelp(cmdLine, opts);
1.2 +40 -40 jakarta-commons/cli/src/test/org/apache/commons/cli/OptionGroupTest.java
Index: OptionGroupTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/OptionGroupTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- OptionGroupTest.java 10 Jun 2002 18:01:15 -0000 1.1
+++ OptionGroupTest.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -35,21 +35,21 @@
public void setUp()
{
- Option file = new Option( 'f', "file", false, "file to process" );
- Option dir = new Option( 'd', "directory", false, "directory to process" );
+ Option file = new Option( "f", "file", false, "file to process" );
+ Option dir = new Option( "d", "directory", false, "directory to process" );
OptionGroup group = new OptionGroup();
group.addOption( file );
group.addOption( dir );
_options = new Options().addOptionGroup( group );
- Option section = new Option( 's', "section", false, "section to process" );
- Option chapter = new Option( 'c', "chapter", false, "chapter to process" );
+ Option section = new Option( "s", "section", false, "section to process" );
+ Option chapter = new Option( "c", "chapter", false, "chapter to process" );
OptionGroup group2 = new OptionGroup();
group2.addOption( section );
group2.addOption( chapter );
_options.addOptionGroup( group2 );
- _options.addOption( 'r', "revision", false, "revision number" );
+ _options.addOption( "r", "revision", false, "revision number" );
}
public void tearDown()
@@ -64,11 +64,11 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
- assertTrue( "Confirm -f is set", cl.hasOption('f') );
- assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
- assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
- assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
+ assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
+ assertTrue( "Confirm -f is set", cl.hasOption("f") );
+ assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
+ assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
+ assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
@@ -85,11 +85,11 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -r is set", cl.hasOption('r') );
- assertTrue( "Confirm -f is NOT set", !cl.hasOption('f') );
- assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
- assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
- assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
+ assertTrue( "Confirm -r is set", cl.hasOption("r") );
+ assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") );
+ assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
+ assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
+ assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
@@ -106,11 +106,11 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -r is set", cl.hasOption('r') );
- assertTrue( "Confirm -f is set", cl.hasOption('f') );
- assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
- assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
- assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
+ assertTrue( "Confirm -r is set", cl.hasOption("r") );
+ assertTrue( "Confirm -f is set", cl.hasOption("f") );
+ assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
+ assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
+ assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
@@ -127,11 +127,11 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
- assertTrue( "Confirm -f is set", cl.hasOption('f') );
- assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
- assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
- assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
+ assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
+ assertTrue( "Confirm -f is set", cl.hasOption("f") );
+ assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
+ assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
+ assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
@@ -148,11 +148,11 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -r is set", cl.hasOption('r') );
- assertTrue( "Confirm -f is set", cl.hasOption('f') );
- assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
- assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
- assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
+ assertTrue( "Confirm -r is set", cl.hasOption("r") );
+ assertTrue( "Confirm -f is set", cl.hasOption("f") );
+ assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
+ assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
+ assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
@@ -169,11 +169,11 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
- assertTrue( "Confirm -f is NOT set", !cl.hasOption('f') );
- assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
- assertTrue( "Confirm -s is NOT set", !cl.hasOption('s') );
- assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
+ assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
+ assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") );
+ assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
+ assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
+ assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2);
}
catch (ParseException e)
@@ -225,11 +225,11 @@
try
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -r is NOT set", !cl.hasOption('r') );
- assertTrue( "Confirm -f is set", cl.hasOption('f') );
- assertTrue( "Confirm -d is NOT set", !cl.hasOption('d') );
- assertTrue( "Confirm -s is set", cl.hasOption('s') );
- assertTrue( "Confirm -c is NOT set", !cl.hasOption('c') );
+ assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
+ assertTrue( "Confirm -f is set", cl.hasOption("f") );
+ assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
+ assertTrue( "Confirm -s is set", cl.hasOption("s") );
+ assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm NO extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
1.2 +8 -8 jakarta-commons/cli/src/test/org/apache/commons/cli/ParseRequiredTest.java
Index: ParseRequiredTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/ParseRequiredTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ParseRequiredTest.java 10 Jun 2002 18:01:15 -0000 1.1
+++ ParseRequiredTest.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -35,11 +35,11 @@
public void setUp()
{
_options = new Options()
- .addOption('a',
+ .addOption("a",
"enable-a",
false,
"turn [a] on or off")
- .addOption('b',
+ .addOption("b",
"bfile",
true,
"set the value of [b]",
@@ -59,9 +59,9 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -a is NOT set", !cl.hasOption('a') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("file") );
+ assertTrue( "Confirm -a is NOT set", !cl.hasOption("a") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("file") );
assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
@@ -78,9 +78,9 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -a is set", cl.hasOption('a') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("file") );
+ assertTrue( "Confirm -a is set", cl.hasOption("a") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("file") );
assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0);
}
catch (ParseException e)
1.2 +30 -30 jakarta-commons/cli/src/test/org/apache/commons/cli/ParseTest.java
Index: ParseTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/ParseTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ParseTest.java 10 Jun 2002 18:01:15 -0000 1.1
+++ ParseTest.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -31,15 +31,15 @@
public void setUp()
{
_options = new Options()
- .addOption('a',
+ .addOption("a",
"enable-a",
false,
"turn [a] on or off")
- .addOption('b',
+ .addOption("b",
"bfile",
true,
"set the value of [b]")
- .addOption('c',
+ .addOption("c",
"copt",
false,
"turn [c] on or off");
@@ -83,9 +83,9 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -a is set", cl.hasOption('a') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("toast") );
+ assertTrue( "Confirm -a is set", cl.hasOption("a") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
}
catch (ParseException e)
@@ -104,9 +104,9 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -a is set", cl.hasOption('a') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("toast") );
+ assertTrue( "Confirm -a is set", cl.hasOption("a") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
}
catch (ParseException e)
@@ -124,10 +124,10 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -a is set", cl.hasOption('a') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm -c is set", cl.hasOption('c') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("toast") );
+ assertTrue( "Confirm -a is set", cl.hasOption("a") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm -c is set", cl.hasOption("c") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
}
catch (ParseException e)
@@ -147,9 +147,9 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -a is set", cl.hasOption('a') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "confirm arg of -b", cl.getOptionValue('b').equals("toast") );
+ assertTrue( "Confirm -a is set", cl.hasOption("a") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "confirm arg of -b", cl.getOptionValue("b").equals("toast") );
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3);
}
catch (UnrecognizedOptionException e)
@@ -196,7 +196,7 @@
{
CommandLine cl = _options.parse(args,
true);
- assertTrue( "Confirm -c is set", cl.hasOption('c') );
+ assertTrue( "Confirm -c is set", cl.hasOption("c") );
assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
}
catch (ParseException e)
@@ -215,14 +215,14 @@
{
CommandLine cl = _options.parse(args,
true);
- assertTrue( "Confirm -c is set", cl.hasOption('c') );
+ assertTrue( "Confirm -c is set", cl.hasOption("c") );
assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
cl = _options.parse( cl.getArgList() );
- assertTrue( "Confirm -c is not set", ! cl.hasOption('c') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("toast") );
+ assertTrue( "Confirm -c is not set", ! cl.hasOption("c") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") );
}
@@ -242,14 +242,14 @@
{
CommandLine cl = _options.parse(args,
true);
- assertTrue( "Confirm -c is set", cl.hasOption('c') );
+ assertTrue( "Confirm -c is set", cl.hasOption("c") );
assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
cl = _options.parse( cl.getArgList() );
- assertTrue( "Confirm -c is not set", ! cl.hasOption('c') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("toast") );
+ assertTrue( "Confirm -c is not set", ! cl.hasOption("c") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") );
}
@@ -269,8 +269,8 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -c is set", cl.hasOption('c') );
- assertTrue( "Confirm -b is not set", ! cl.hasOption('b') );
+ assertTrue( "Confirm -c is set", cl.hasOption("c") );
+ assertTrue( "Confirm -b is not set", ! cl.hasOption("b") );
assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
}
@@ -291,9 +291,9 @@
{
CommandLine cl = _options.parse(args);
- assertTrue( "Confirm -a is set", cl.hasOption('a') );
- assertTrue( "Confirm -b is set", cl.hasOption('b') );
- assertTrue( "Confirm arg of -b", cl.getOptionValue('b').equals("-") );
+ assertTrue( "Confirm -a is set", cl.hasOption("a") );
+ assertTrue( "Confirm -b is set", cl.hasOption("b") );
+ assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("-") );
assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-") );
}
1.2 +9 -9 jakarta-commons/cli/src/test/org/apache/commons/cli/PatternOptionBuilderTest.java
Index: PatternOptionBuilderTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/PatternOptionBuilderTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PatternOptionBuilderTest.java 10 Jun 2002 18:01:15 -0000 1.1
+++ PatternOptionBuilderTest.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -46,15 +46,15 @@
String[] args = new String[] { "-c", "-a", "foo", "-b", "java.util.Vector", "-e", "build.xml", "-f", "java.util.Calendar", "-n", "4.5", "-t", "http://jakarta.apache.org/" };
CommandLine line = options.parse(args);
- assertEquals("flag a", "foo", line.getOptionValue('a'));
- assertEquals("string flag a", "foo", line.getOptionObject('a'));
- assertEquals("object flag b", new java.util.Vector(), line.getOptionObject('b'));
- assertEquals("boolean true flag c", true, line.hasOption('c'));
- assertEquals("boolean false flag d", false, line.hasOption('d'));
- assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject('e'));
- assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject('f'));
- assertEquals("number flag n", new Float(4.5), line.getOptionObject('n'));
- assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject('t'));
+ assertEquals("flag a", "foo", line.getOptionValue("a"));
+ assertEquals("string flag a", "foo", line.getOptionObject("a"));
+ assertEquals("object flag b", new java.util.Vector(), line.getOptionObject("b"));
+ assertEquals("boolean true flag c", true, line.hasOption("c"));
+ assertEquals("boolean false flag d", false, line.hasOption("d"));
+ assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject("e"));
+ assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject("f"));
+ assertEquals("number flag n", new Float(4.5), line.getOptionObject("n"));
+ assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject("t"));
/// DATES NOT SUPPORTED YET.
// assertEquals("number flag t", new java.util.Date(1023400137276L), line.getOptionObject('z'));
// input is: "Thu Jun 06 17:48:57 EDT 2002"
1.2 +4 -4 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestHelpFormatter.java 10 Jun 2002 18:01:15 -0000 1.1
+++ TestHelpFormatter.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -111,7 +111,7 @@
Options options = null;
String expected = null;
- options = new Options().addOption('a', false, "aaaa aaaa aaaa aaaa aaaa");
+ options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
hf.renderOptions(sb, 60, options, leftPad, descPad);
assertEquals("simple non-wrapped option", expected, sb.toString());
@@ -125,7 +125,7 @@
assertEquals("simple wrapped option", expected, sb.toString());
- options = new Options().addOption('a', "aaa", false, "dddd dddd dddd dddd");
+ options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd");
expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
sb.setLength(0);
hf.renderOptions(sb, 60, options, leftPad, descPad);
@@ -140,8 +140,8 @@
assertEquals("long wrapped option", expected, sb.toString());
options = new Options().
- addOption('a', "aaa", false, "dddd dddd dddd dddd").
- addOption('b', false, "feeee eeee eeee eeee");
+ 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 +
1.2 +14 -14 jakarta-commons/cli/src/test/org/apache/commons/cli/ValueTest.java
Index: ValueTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/ValueTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValueTest.java 10 Jun 2002 18:01:15 -0000 1.1
+++ ValueTest.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -39,20 +39,20 @@
public void setUp()
{
Options opts = new Options();
- opts.addOption('a',
+ opts.addOption("a",
false,
"toggle -a");
- opts.addOption('b',
+ opts.addOption("b",
true,
"set -b");
- opts.addOption('c',
+ opts.addOption("c",
"c",
false,
"toggle -c");
- opts.addOption('d',
+ opts.addOption("d",
"d",
true,
"set -d");
@@ -106,27 +106,27 @@
public void testShortNoArg()
{
- assertTrue( _cl.hasOption('a') );
- assertNull( _cl.getOptionValue('a') );
+ assertTrue( _cl.hasOption("a") );
+ assertNull( _cl.getOptionValue("a") );
}
public void testShortWithArg()
{
- assertTrue( _cl.hasOption('b') );
- assertNotNull( _cl.getOptionValue('b') );
- assertEquals( _cl.getOptionValue('b'), "foo");
+ assertTrue( _cl.hasOption("b") );
+ assertNotNull( _cl.getOptionValue("b") );
+ assertEquals( _cl.getOptionValue("b"), "foo");
}
public void testLongNoArg()
{
- assertTrue( _cl.hasOption('c') );
- assertNull( _cl.getOptionValue('c') );
+ assertTrue( _cl.hasOption("c") );
+ assertNull( _cl.getOptionValue("c") );
}
public void testLongWithArg()
{
- assertTrue( _cl.hasOption('d') );
- assertNotNull( _cl.getOptionValue('d') );
- assertEquals( _cl.getOptionValue('d'), "bar");
+ assertTrue( _cl.hasOption("d") );
+ assertNotNull( _cl.getOptionValue("d") );
+ assertEquals( _cl.getOptionValue("d"), "bar");
}
}
1.2 +21 -21 jakarta-commons/cli/src/test/org/apache/commons/cli/ValuesTest.java
Index: ValuesTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/ValuesTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValuesTest.java 10 Jun 2002 18:01:15 -0000 1.1
+++ ValuesTest.java 16 Jun 2002 23:00:59 -0000 1.2
@@ -39,32 +39,32 @@
public void setUp()
{
Options opts = new Options();
- opts.addOption('a',
+ opts.addOption("a",
false,
"toggle -a");
- opts.addOption('b',
+ opts.addOption("b",
true,
"set -b");
- opts.addOption('c',
+ opts.addOption("c",
"c",
false,
"toggle -c");
- opts.addOption('d',
+ opts.addOption("d",
"d",
true,
"set -d");
- opts.addOption('e',
+ opts.addOption("e",
"e",
true,
"set -e",
false,
true);
- opts.addOption('f',
+ opts.addOption("f",
"f",
false,
"jk");
@@ -94,31 +94,31 @@
public void testShortArgs()
{
- assertTrue( _cl.hasOption('a') );
- assertTrue( _cl.hasOption('c') );
+ assertTrue( _cl.hasOption("a") );
+ assertTrue( _cl.hasOption("c") );
- assertNull( _cl.getOptionValues('a') );
- assertNull( _cl.getOptionValues('c') );
+ assertNull( _cl.getOptionValues("a") );
+ assertNull( _cl.getOptionValues("c") );
}
public void testShortArgsWithValue()
{
- assertTrue( _cl.hasOption('b') );
- assertTrue( _cl.getOptionValue('b').equals("foo"));
- assertTrue( _cl.getOptionValues('b').length == 1);
-
- assertTrue( _cl.hasOption('d') );
- assertTrue( _cl.getOptionValue('d').equals("bar"));
- assertTrue( _cl.getOptionValues('d').length == 1);
+ assertTrue( _cl.hasOption("b") );
+ assertTrue( _cl.getOptionValue("b").equals("foo"));
+ assertTrue( _cl.getOptionValues("b").length == 1);
+
+ assertTrue( _cl.hasOption("d") );
+ assertTrue( _cl.getOptionValue("d").equals("bar"));
+ assertTrue( _cl.getOptionValues("d").length == 1);
}
public void testMultipleArgValues()
{
- String[] result = _cl.getOptionValues('e');
+ String[] result = _cl.getOptionValues("e");
String[] values = new String[] { "one", "two" };
- assertTrue( _cl.hasOption('e') );
- assertTrue( _cl.getOptionValues('e').length == 2);
- assertTrue( java.util.Arrays.equals( values, _cl.getOptionValues('e') ) );
+ assertTrue( _cl.hasOption("e") );
+ assertTrue( _cl.getOptionValues("e").length == 2);
+ assertTrue( java.util.Arrays.equals( values, _cl.getOptionValues("e") ) );
}
public void testExtraArgs()
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>