jkeyes 2002/11/18 00:41:26
Modified: cli/src/java/org/apache/commons/cli CommandLine.java
CommandLineParser.java Option.java
OptionBuilder.java Options.java Parser.java
cli/src/test/org/apache/commons/cli BugsTest.java
OptionBuilderTest.java ValueTest.java
Added: cli/src/java/org/apache/commons/cli OptionValidator.java
Util.java
Log:
refactored the option string handling, added property support for options with an
argument value
Revision Changes Path
1.14 +11 -17
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.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- CommandLine.java 14 Nov 2002 23:30:02 -0000 1.13
+++ CommandLine.java 18 Nov 2002 08:41:26 -0000 1.14
@@ -135,7 +135,7 @@
public Object getOptionObject( String opt ) {
String res = getOptionValue( opt );
- Object type =
((Option)((List)options.get(opt)).iterator().next()).getType();
+ Object type = ((Option)options.get(opt)).getType();
return res == null ? null : TypeHandler.createValue(res, type);
}
@@ -182,21 +182,17 @@
public String[] getOptionValues( String opt ) {
List values = new java.util.ArrayList();
+ opt = Util.stripLeadingHyphens( opt );
+
String key = opt;
if( names.containsKey( opt ) ) {
key = (String)names.get( opt );
}
if( options.containsKey( key ) ) {
- List opts = (List)options.get( key );
- Iterator iter = opts.iterator();
-
- while( iter.hasNext() ) {
- Option optt = (Option)iter.next();
- values.addAll( optt.getValuesList() );
- }
+ return ((Option)options.get(key)).getValues();
}
- return (values.size() == 0) ? null : (String[])values.toArray(new
String[]{});
+ return null;
}
/**
@@ -294,21 +290,19 @@
void addOption( Option opt ) {
hashcodeMap.put( new Integer( opt.hashCode() ), opt );
- String key = opt.getOpt();
- if( " ".equals(key) ) {
+ String key = opt.getKey();
+ if( key == null ) {
key = opt.getLongOpt();
}
else {
names.put( opt.getLongOpt(), key );
}
- if( options.get( key ) != null ) {
- ((java.util.List)options.get( key )).add( opt );
- }
- else {
- options.put( key, new java.util.ArrayList() );
- ((java.util.List)options.get( key ) ).add( opt );
+ if( opt.getValues() != null ) {
+ System.out.println( opt.getKey() + "=" + opt.getValues().length );
}
+
+ options.put( key, opt );
}
/**
1.5 +38 -3
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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CommandLineParser.java 19 Sep 2002 22:59:43 -0000 1.4
+++ CommandLineParser.java 18 Nov 2002 08:41:26 -0000 1.5
@@ -60,6 +60,8 @@
*/
package org.apache.commons.cli;
+import java.util.Properties;
+
/**
* A class that implements the <code>CommandLineParser</code> interface
* can parse a String array according to the {@link Options} specified
@@ -75,6 +77,7 @@
* @param options the specified Options
* @param arguments the command line arguments
* @return the list of atomic option and value tokens
+ *
* @throws ParseException if there are any problems encountered
* while parsing the command line tokens.
*/
@@ -82,16 +85,48 @@
throws ParseException;
/**
+ * Parse the arguments according to the specified options and
+ * properties.
+ *
+ * @param options the specified Options
+ * @param arguments the command line arguments
+ * @param properties command line option name-value pairs
+ * @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 options, String[] arguments, Properties props
)
+ throws ParseException;
+
+ /**
* 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
* @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;
+
+ /**
+ * Parse the arguments according to the specified options and
+ * properties.
+ *
+ * @param options the specified Options
+ * @param arguments the command line arguments
+ * @param properties command line option name-value pairs
+ * @param stopAtNonOption specifies whether to continue parsing the
+ *
+ * @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 options, String[] arguments, Properties
properties, boolean stopAtNonOption)
throws ParseException;
}
1.16 +62 -121 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.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- Option.java 19 Oct 2002 21:15:48 -0000 1.15
+++ Option.java 18 Nov 2002 08:41:26 -0000 1.16
@@ -58,17 +58,6 @@
* <http://www.apache.org/>.
*
*/
-
-/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
- *
- * This software is published under the terms of the Apache Software License
- * version 1.1, a copy of which has been included with this distribution in
- * the LICENSE file.
- *
- * $Id$
- */
-
package org.apache.commons.cli;
import java.util.ArrayList;
@@ -88,7 +77,6 @@
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
* @version $Revision$
*/
-
public class Option implements Cloneable {
/** constant that specifies the number of argument values has not been
specified */
@@ -97,7 +85,7 @@
/** constant that specifies the number of argument values is infinite */
public final static int UNLIMITED_VALUES = -2;
- /** opt the single character representation of the option */
+ /** opt the name of the option */
private String opt;
/** longOpt is the long representation of the option */
@@ -130,86 +118,10 @@
/** the list of argument values **/
private ArrayList values = new ArrayList();
- /** option char (only valid for single character options) */
- private char id;
-
/** the character that is the value separator */
private char valuesep;
/**
- * <p>Validates whether <code>opt</code> is a permissable Option
- * shortOpt. The rules that specify if the <code>opt</code>
- * is valid are:</p>
- * <ul>
- * <li><code>opt</code> is not NULL</li>
- * <li>a single character <code>opt</code> that is either
- * ' '(special case), '?', '@' or a letter</li>
- * <li>a multi character <code>opt</code> that only contains
- * letters.</li>
- * </ul>
- *
- * @param opt The option string to validate
- * @throws IllegalArgumentException if the Option is not valid.
- */
- private void validateOption( String opt )
- throws IllegalArgumentException
- {
- // check that opt is not NULL
- if( opt == null ) {
- throw new IllegalArgumentException( "opt is null" );
- }
- // 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 + "'" );
- }
- id = ch;
- }
- // handle the multi character opt
- 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] + "'" );
- }
- }
- }
- }
-
- /**
- * <p>Returns whether the specified character is a valid Option.</p>
- *
- * @param c the option to validate
- * @return true if <code>c</code> is a letter, ' ', '?' or '@', otherwise false.
- */
- private boolean isValidOpt( char c ) {
- return ( isValidChar( c ) || c == ' ' || c == '?' || c == '@' );
- }
-
- /**
- * <p>Returns whether the specified character is a valid character.</p>
- *
- * @param c the character to validate
- * @return true if <code>c</code> is a letter.
- */
- private boolean isValidChar( char c ) {
- return Character.isJavaIdentifierPart( c );
- }
-
- /**
- * <p>Returns the id of this Option. This is only set when the
- * Option shortOpt is a single character. This is used for switch
- * statements.</p>
- *
- * @return the id of this Option
- */
- public int getId( ) {
- return id;
- }
-
- /**
* Creates an Option using the specified parameters.
*
* @param opt short representation of the option
@@ -247,7 +159,7 @@
throws IllegalArgumentException
{
// ensure that the option is valid
- validateOption( opt );
+ OptionValidator.validateOption( opt );
this.opt = opt;
this.longOpt = longOpt;
@@ -261,6 +173,30 @@
this.description = description;
}
+ /**
+ * <p>Returns the id of this Option. This is only set when the
+ * Option shortOpt is a single character. This is used for switch
+ * statements.</p>
+ *
+ * @return the id of this Option
+ */
+ public int getId( ) {
+ return getKey().charAt( 0 );
+ }
+
+ /**
+ * <p>Returns the 'unique' Option identifier.</p>
+ *
+ * @return the 'unique' Option identifier
+ */
+ String getKey() {
+ // if 'opt' is null, then it is a 'long' option
+ if( opt == null ) {
+ return this.longOpt;
+ }
+ return this.opt;
+ }
+
/** <p>Retrieve the name of this Option.</p>
*
* <p>It is this String which can be used with
@@ -444,37 +380,8 @@
return this.numberOfArgs;
}
- /**
- * <p>Dump state, suitable for debugging.</p>
- *
- * @return Stringified form of this object
- */
- public String toString() {
- StringBuffer buf = new StringBuffer().append("[ option: ");
-
- buf.append( this.opt );
-
- if ( this.longOpt != null ) {
- buf.append(" ")
- .append(this.longOpt);
- }
-
- buf.append(" ");
-
- if ( hasArg ) {
- buf.append( "+ARG" );
- }
-
- buf.append(" :: ")
- .append( this.description );
-
- if ( this.type != null ) {
- buf.append(" :: ")
- .append( this.type );
- }
-
- buf.append(" ]");
- return buf.toString();
+ public void clearValues() {
+ this.values.clear();
}
/**
@@ -572,4 +479,38 @@
option.setValueSeparator( getValueSeparator() );
return option;
}
+
+ /**
+ * <p>Dump state, suitable for debugging.</p>
+ *
+ * @return Stringified form of this object
+ */
+ public String toString() {
+ StringBuffer buf = new StringBuffer().append("[ option: ");
+
+ buf.append( this.opt );
+
+ if ( this.longOpt != null ) {
+ buf.append(" ")
+ .append(this.longOpt);
+ }
+
+ buf.append(" ");
+
+ if ( hasArg ) {
+ buf.append( "+ARG" );
+ }
+
+ buf.append(" :: ")
+ .append( this.description );
+
+ if ( this.type != null ) {
+ buf.append(" :: ")
+ .append( this.type );
+ }
+
+ buf.append(" ]");
+ return buf.toString();
+ }
+
}
1.13 +4 -4
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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- OptionBuilder.java 15 Oct 2002 22:50:45 -0000 1.12
+++ OptionBuilder.java 18 Nov 2002 08:41:26 -0000 1.13
@@ -332,7 +332,7 @@
throw new IllegalArgumentException( "must specify longopt" );
}
- return create( " " );
+ return create( null );
}
/**
1.16 +11 -32 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.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- Options.java 15 Nov 2002 22:25:58 -0000 1.15
+++ Options.java 18 Nov 2002 08:41:26 -0000 1.16
@@ -125,7 +125,7 @@
option.setRequired( false );
addOption( option );
- optionGroups.put( option.getOpt(), group );
+ optionGroups.put( option.getKey(), group );
}
return this;
@@ -165,16 +165,16 @@
* @return the resulting Options instance
*/
public Options addOption(Option opt) {
- String shortOpt = "-" + opt.getOpt();
+ String shortOpt = opt.getOpt();
// add it to the long option list
if ( opt.hasLongOpt() ) {
- longOpts.put( "--" + opt.getLongOpt(), opt );
+ longOpts.put( opt.getLongOpt(), opt );
}
// if the option is required add it to the required list
if ( opt.isRequired() ) {
- requiredOpts.add( shortOpt );
+ requiredOpts.add( opt.getKey() );
}
shortOpts.put( shortOpt, opt );
@@ -228,22 +228,12 @@
*/
public Option getOption( String opt ) {
- Option option = null;
+ opt = Util.stripLeadingHyphens( opt );
- // short option
- if( opt.length() == 1 ) {
- option = (Option)shortOpts.get( "-" + opt );
+ if( shortOpts.containsKey( opt ) ) {
+ return (Option) shortOpts.get( opt );
}
- // long option
- else if( opt.startsWith( "--" ) ) {
- option = (Option)longOpts.get( opt );
- }
- // a just-in-case
- else {
- option = (Option)shortOpts.get( opt );
- }
-
- return (option == null) ? null : (Option)option.clone();
+ return (Option) longOpts.get( opt );
}
/**
@@ -255,19 +245,8 @@
* of this {@link Options}
*/
public boolean hasOption( String opt ) {
-
- // short option
- if( opt.length() == 1 ) {
- return shortOpts.containsKey( "-" + opt );
- }
- // long option
- else if( opt.startsWith( "--" ) ) {
- return longOpts.containsKey( opt );
- }
- // a just-in-case
- else {
- return shortOpts.containsKey( opt );
- }
+ opt = Util.stripLeadingHyphens( opt );
+ return shortOpts.containsKey( opt ) || longOpts.containsKey( opt );
}
/** <p>Returns the OptionGroup the <code>opt</code>
@@ -278,7 +257,7 @@
* of an OptionGroup, otherwise return null
*/
public OptionGroup getOptionGroup( Option opt ) {
- return (OptionGroup)optionGroups.get( opt.getOpt() );
+ return (OptionGroup)optionGroups.get( opt.getKey() );
}
/** <p>Dump state, suitable for debugging.</p>
1.8 +81 -11 jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java
Index: Parser.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Parser.java 24 Oct 2002 23:17:49 -0000 1.7
+++ Parser.java 18 Nov 2002 08:41:26 -0000 1.8
@@ -62,10 +62,12 @@
package org.apache.commons.cli;
import java.util.Arrays;
+import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
+import java.util.Properties;
/**
* <p><code>Parser</code> creates {@link CommandLine}s.</p>
@@ -108,10 +110,31 @@
* @throws ParseException if an error occurs when parsing the
* arguments.
*/
- public CommandLine parse( Options options, String[] arguments )
+ public CommandLine parse( Options options,
+ String[] arguments )
throws ParseException
{
- return parse( options, arguments, false );
+ return parse( options, arguments, null, false );
+ }
+
+ /**
+ * Parse the arguments according to the specified options and
+ * properties.
+ *
+ * @param options the specified Options
+ * @param arguments the command line arguments
+ * @param properties command line option name-value pairs
+ * @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 options,
+ String[] arguments,
+ Properties properties )
+ throws ParseException
+ {
+ return parse( options, arguments, properties, false );
}
/**
@@ -124,12 +147,34 @@
* interpreting the arguments when a non option has
* been encountered and to add them to the CommandLines
* args list.
+ *
* @return the <code>CommandLine</code>
* @throws ParseException if an error occurs when parsing the
* arguments.
*/
+ public CommandLine parse( Options options,
+ String[] arguments,
+ boolean stopAtNonOption )
+ throws ParseException
+ {
+ return parse( options, arguments, null, stopAtNonOption );
+ }
+
+ /**
+ * Parse the arguments according to the specified options and
+ * properties.
+ *
+ * @param options the specified Options
+ * @param arguments the command line arguments
+ * @param properties command line option name-value pairs
+ * @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
{
@@ -140,6 +185,10 @@
boolean eatTheRest = false;
+ if( arguments == null ) {
+ arguments = new String[0];
+ }
+
List tokenList = Arrays.asList( flatten( opts, arguments, stopAtNonOption )
);
ListIterator iterator = tokenList.listIterator();
@@ -189,11 +238,32 @@
}
}
}
+ processProperties( properties );
checkRequiredOptions();
return cmd;
}
/**
+ * <p>Sets the values of Options using the values in
<code>properties</code>.</p>
+ */
+ private void processProperties( Properties properties ) {
+ if( properties == null ) {
+ return;
+ }
+
+ for( Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) {
+ String option = e.nextElement().toString();
+ if( !cmd.hasOption( option ) ) {
+ Option opt = options.getOption( option );
+ if( opt.getValues() == null || opt.getValues().length == 0 ) {
+ opt.addValue( properties.getProperty( option ) );
+ }
+ cmd.addOption( opt );
+ }
+ }
+ }
+
+ /**
* <p>Throws a {@link MissingOptionException} if all of the
* required options are no present.</p>
*/
@@ -221,22 +291,22 @@
{
// loop until an option is found
while( iter.hasNext() ) {
- String var = (String)iter.next();
+ String str = (String)iter.next();
// found an Option
- if( options.hasOption( var ) ) {
+ if( options.hasOption( str ) ) {
iter.previous();
break;
}
// found a value
- else if( !opt.addValue( var ) ) {
+ else if( !opt.addValue( str ) ) {
iter.previous();
break;
}
}
if( opt.getValues() == null && !opt.hasOptionalArg() ) {
- throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
+ throw new MissingArgumentException( "no argument for:" + opt.getKey() );
}
}
@@ -259,7 +329,7 @@
// if the option is a required option remove the option from
// the requiredOptions list
if ( opt.isRequired() ) {
- requiredOptions.remove( "-" + opt.getOpt() );
+ requiredOptions.remove( opt.getKey() );
}
// if the option is in an OptionGroup make that option the selected
1.1
jakarta-commons/cli/src/java/org/apache/commons/cli/OptionValidator.java
Index: OptionValidator.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionValidator.java,v
1.1 2002/11/18 08:41:26 jkeyes Exp $
* $Revision: 1.1 $
* $Date: 2002/11/18 08:41:26 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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;
/**
* Validates an Option string.
*
* @author John Keyes ( john at integralsource.com )
*/
public class OptionValidator {
/**
* <p>Validates whether <code>opt</code> is a permissable Option
* shortOpt. The rules that specify if the <code>opt</code>
* is valid are:</p>
* <ul>
* <li><code>opt</code> is not NULL</li>
* <li>a single character <code>opt</code> that is either
* ' '(special case), '?', '@' or a letter</li>
* <li>a multi character <code>opt</code> that only contains
* letters.</li>
* </ul>
*
* @param opt The option string to validate
* @throws IllegalArgumentException if the Option is not valid.
*/
static void validateOption( String opt )
throws IllegalArgumentException
{
// check that opt is not 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 + "'" );
}
}
// handle the multi character opt
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] + "'" );
}
}
}
}
/**
* <p>Returns whether the specified character is a valid Option.</p>
*
* @param c the option to validate
* @return true if <code>c</code> is a letter, ' ', '?' or '@', otherwise false.
*/
private static boolean isValidOpt( char c ) {
return ( isValidChar( c ) || c == ' ' || c == '?' || c == '@' );
}
/**
* <p>Returns whether the specified character is a valid character.</p>
*
* @param c the character to validate
* @return true if <code>c</code> is a letter.
*/
private static boolean isValidChar( char c ) {
return Character.isJavaIdentifierPart( c );
}
}
1.1 jakarta-commons/cli/src/java/org/apache/commons/cli/Util.java
Index: Util.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Util.java,v 1.1
2002/11/18 08:41:26 jkeyes Exp $
* $Revision: 1.1 $
* $Date: 2002/11/18 08:41:26 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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;
/**
* Contains useful helper methods for classes within this package.
*
* @author John Keyes (john at integralsource.com)
*/
class Util {
static String stripLeadingHyphens( String str ) {
if( str.startsWith( "--" ) ) {
return str.substring( 2, str.length() );
}
else if ( str.startsWith( "-" ) ) {
return str.substring( 1, str.length() );
}
return str;
}
}
1.11 +3 -3
jakarta-commons/cli/src/test/org/apache/commons/cli/BugsTest.java
Index: BugsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/BugsTest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- BugsTest.java 24 Oct 2002 23:17:49 -0000 1.10
+++ BugsTest.java 18 Nov 2002 08:41:26 -0000 1.11
@@ -331,7 +331,7 @@
CommandLine line = parser.parse( opts, args );
}
catch( ParseException exp ) {
- fail( "Unexpected exception: " + exp.getMessage() );
+ fail( "Unexpected exception: " + exp.getClass().getName() + ":" +
exp.getMessage() );
}
opts.addOption( forward );
@@ -340,7 +340,7 @@
CommandLine line = parser.parse( opts, args );
}
catch( ParseException exp ) {
- fail( "Unexpected exception: " + exp.getMessage() );
+ fail( "Unexpected exception: " + exp.getClass().getName() + ":" +
exp.getMessage() );
}
}
}
1.3 +0 -9
jakarta-commons/cli/src/test/org/apache/commons/cli/OptionBuilderTest.java
Index: OptionBuilderTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/OptionBuilderTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- OptionBuilderTest.java 3 Aug 2002 23:45:09 -0000 1.2
+++ OptionBuilderTest.java 18 Nov 2002 08:41:26 -0000 1.3
@@ -137,15 +137,6 @@
// success
}
- // null option
- try {
- Option opt = OptionBuilder.create( null );
- fail( "IllegalArgumentException not caught" );
- }
- catch( IllegalArgumentException exp ) {
- // success
- }
-
// valid option
try {
Option opt = OptionBuilder.create( "opt" );
1.6 +48 -0
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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ValueTest.java 19 Sep 2002 22:59:44 -0000 1.5
+++ ValueTest.java 18 Nov 2002 08:41:26 -0000 1.6
@@ -14,6 +14,8 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import java.util.Properties;
+
public class ValueTest extends TestCase
{
@@ -269,4 +271,50 @@
fail("Cannot setUp() CommandLine: " + e.toString());
}
}
+
+ public void testPropertyValues()
+ {
+ Properties properties = new Properties();
+ properties.setProperty( "hide", "seek" );
+ try
+ {
+ CommandLineParser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts, null, properties);
+ assertTrue( cmd.hasOption("hide") );
+ assertEquals( "seek", cmd.getOptionValue("hide") );
+ assertTrue( !cmd.hasOption("fake") );
+ }
+ catch (ParseException e)
+ {
+ fail("Cannot setUp() CommandLine: " + e.toString());
+ }
+ }
+
+ public void testPropertyOverrideValues()
+ {
+ String[] args = new String[] {
+ "-j",
+ "found",
+ "-i",
+ "ink"
+ };
+
+ Properties properties = new Properties();
+ properties.setProperty( "j", "seek" );
+ try
+ {
+ CommandLineParser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts, args, properties);
+ assertTrue( cmd.hasOption("j") );
+ assertEquals( "found", cmd.getOptionValue("j") );
+ assertTrue( cmd.hasOption("i") );
+ assertEquals( "ink", cmd.getOptionValue("i") );
+ assertTrue( !cmd.hasOption("fake") );
+ }
+ catch (ParseException e)
+ {
+ fail("Cannot setUp() CommandLine: " + e.toString());
+ }
+ }
+
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>