You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ro...@apache.org on 2005/07/06 22:03:56 UTC
svn commit: r209508 - in /jakarta/commons/proper/cli/branches/CLI_1_BRANCH:
src/java/org/apache/commons/cli/ src/test/org/apache/commons/cli/ xdocs/
Author: roxspring
Date: Wed Jul 6 13:03:54 2005
New Revision: 209508
URL: http://svn.apache.org/viewcvs?rev=209508&view=rev
Log:
Merged 129840:129847 to the branch
Added:
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionValidator.java
- copied unchanged from r129847, jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli/OptionValidator.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Util.java
- copied unchanged from r129847, jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli/Util.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionsTest.java
- copied unchanged from r129847, jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli/OptionsTest.java
Modified:
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLine.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLineParser.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionBuilder.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionGroup.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Options.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Parser.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/BugsTest.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionBuilderTest.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionGroupTest.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ParseTest.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ValueTest.java
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/introduction.xml
jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/usage.xml
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLine.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLine.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLine.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLine.java Wed Jul 6 13:03:54 2005
@@ -91,6 +91,9 @@
/** the processed options */
private Map options = new HashMap();
+ /** the option name map */
+ private Map names = new HashMap();
+
/** Map of unique options for ease to get complete list of options */
private Map hashcodeMap = new HashMap();
@@ -132,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);
}
@@ -179,16 +182,17 @@
public String[] getOptionValues( String opt ) {
List values = new java.util.ArrayList();
- if( options.containsKey( opt ) ) {
- List opts = (List)options.get( opt );
- Iterator iter = opts.iterator();
-
- while( iter.hasNext() ) {
- Option optt = (Option)iter.next();
- values.addAll( optt.getValuesList() );
- }
+ opt = Util.stripLeadingHyphens( opt );
+
+ String key = opt;
+ if( names.containsKey( opt ) ) {
+ key = (String)names.get( opt );
+ }
+
+ if( options.containsKey( key ) ) {
+ return ((Option)options.get(key)).getValues();
}
- return (values.size() == 0) ? null : (String[])values.toArray(new String[]{});
+ return null;
}
/**
@@ -286,18 +290,15 @@
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();
}
-
- 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 );
+ names.put( opt.getLongOpt(), key );
}
+
+ options.put( key, opt );
}
/**
@@ -316,7 +317,7 @@
* @return an array of the processed {@link Option}s.
*/
public Option[] getOptions( ) {
- Collection processed = hashcodeMap.values();
+ Collection processed = options.values();
// reinitialise array
optionsArray = new Option[ processed.size() ];
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLineParser.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLineParser.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLineParser.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/CommandLineParser.java Wed Jul 6 13:03:54 2005
@@ -1,7 +1,7 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/CommandLineParser.java,v 1.4 2002/09/19 22:59:43 jkeyes Exp $
- * $Revision: 1.4 $
- * $Date: 2002/09/19 22:59:43 $
+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/CommandLineParser.java,v 1.5 2002/11/18 08:41:26 jkeyes Exp $
+ * $Revision: 1.5 $
+ * $Date$
*
* ====================================================================
*
@@ -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;
}
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/HelpFormatter.java Wed Jul 6 13:03:54 2005
@@ -277,13 +277,9 @@
buff.append( "--" ).append( option.getLongOpt() );
}
- if( option.hasArg() ){
- buff.append( " " );
- }
-
// if the Option has a value
- if( option.hasArg() ) {
- buff.append( option.getArgName() );
+ if( option.hasArg() && option.getArgName() != null ) {
+ buff.append( " " ).append( option.getArgName() );
}
// if the Option is not a required option
@@ -389,8 +385,12 @@
optBuf.append( dpad );
int nextLineTabStop = max + descPad;
+
+ if( option.getDescription() != null ) {
+ optBuf.append( option.getDescription() );
+ }
renderWrappedText(sb, width, nextLineTabStop,
- optBuf.append(option.getDescription()).toString());
+ optBuf.toString());
if ( i.hasNext() )
{
sb.append(defaultNewLine);
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Option.java Wed Jul 6 13:03:54 2005
@@ -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: Option.java,v 1.6 2002/06/06 22:50:14 bayard Exp $
- */
-
package org.apache.commons.cli;
import java.util.ArrayList;
@@ -88,7 +77,6 @@
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.6 $
*/
-
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();
+ }
+
}
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionBuilder.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionBuilder.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionBuilder.java Wed Jul 6 13:03:54 2005
@@ -1,7 +1,7 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.12 2002/10/15 22:50:45 jkeyes Exp $
- * $Revision: 1.12 $
- * $Date: 2002/10/15 22:50:45 $
+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.13 2002/11/18 08:41:26 jkeyes Exp $
+ * $Revision: 1.13 $
+ * $Date$
*
* ====================================================================
*
@@ -332,7 +332,7 @@
throw new IllegalArgumentException( "must specify longopt" );
}
- return create( " " );
+ return create( null );
}
/**
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionGroup.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionGroup.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionGroup.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/OptionGroup.java Wed Jul 6 13:03:54 2005
@@ -90,7 +90,7 @@
public OptionGroup addOption(Option opt) {
// key - option name
// value - the option
- optionMap.put( "-" + opt.getOpt(), opt );
+ optionMap.put( opt.getKey(), opt );
return this;
}
@@ -168,8 +168,14 @@
while( iter.hasNext() ) {
Option option = (Option)iter.next();
- buff.append( "-" );
- buff.append( option.getOpt() );
+ if( option.getOpt() != null ) {
+ buff.append( "-" );
+ buff.append( option.getOpt() );
+ }
+ else {
+ buff.append( "--" );
+ buff.append( option.getLongOpt() );
+ }
buff.append( " " );
buff.append( option.getDescription( ) );
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Options.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Options.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Options.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Options.java Wed Jul 6 13:03:54 2005
@@ -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 );
@@ -187,6 +187,15 @@
* @return read-only Collection of {@link Option} objects in this descriptor
*/
public Collection getOptions() {
+ return Collections.unmodifiableCollection( helpOptions() );
+ }
+
+ /**
+ * <p>Returns the Options for use by the HelpFormatter.</p>
+ *
+ * @return the List of Options
+ */
+ List helpOptions() {
List opts = new ArrayList( shortOpts.values() );
// now look through the long opts to see if there are any Long-opt
@@ -200,16 +209,7 @@
opts.add(item);
}
}
- return Collections.unmodifiableCollection( opts );
- }
-
- /**
- * <p>Returns the Options for use by the HelpFormatter.</p>
- *
- * @return the List of Options
- */
- List helpOptions() {
- return new ArrayList( shortOpts.values() );
+ return new ArrayList( opts );
}
/** <p>Returns the required options as a
@@ -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 );
- }
- // long option
- else if( opt.startsWith( "--" ) ) {
- option = (Option)longOpts.get( opt );
- }
- // a just-in-case
- else {
- option = (Option)shortOpts.get( opt );
+ if( shortOpts.containsKey( opt ) ) {
+ return (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>
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Parser.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Parser.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Parser.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/java/org/apache/commons/cli/Parser.java Wed Jul 6 13:03:54 2005
@@ -1,7 +1,7 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.7 2002/10/24 23:17:49 jkeyes Exp $
- * $Revision: 1.7 $
- * $Date: 2002/10/24 23:17:49 $
+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.8 2002/11/18 08:41:26 jkeyes Exp $
+ * $Revision: 1.8 $
+ * $Date$
*
* ====================================================================
*
@@ -62,17 +62,19 @@
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>
*
* @author John Keyes (john at integralsource.com)
* @see Parser
- * @version $Revision: 1.7 $
+ * @version $Revision: 1.8 $
*/
public abstract class Parser implements CommandLineParser {
@@ -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
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/BugsTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/BugsTest.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/BugsTest.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/BugsTest.java Wed Jul 6 13:03:54 2005
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: BugsTest.java,v 1.10 2002/10/24 23:17:49 jkeyes Exp $
+ * $Id$
*/
package org.apache.commons.cli;
@@ -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() );
}
}
}
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionBuilderTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionBuilderTest.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionBuilderTest.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionBuilderTest.java Wed Jul 6 13:03:54 2005
@@ -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" );
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionGroupTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionGroupTest.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionGroupTest.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/OptionGroupTest.java Wed Jul 6 13:03:54 2005
@@ -51,6 +51,14 @@
group2.addOption( chapter );
_options.addOptionGroup( group2 );
+
+ Option importOpt = new Option( null, "import", false, "section to process" );
+ Option exportOpt = new Option( null, "export", false, "chapter to process" );
+ OptionGroup group3 = new OptionGroup();
+ group3.addOption( importOpt );
+ group3.addOption( exportOpt );
+ _options.addOptionGroup( group3 );
+
_options.addOption( "r", "revision", false, "revision number" );
}
@@ -233,6 +241,29 @@
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)
+ {
+ fail( e.toString() );
+ }
+ }
+
+ public void testValidLongOnlyOptions()
+ {
+ try
+ {
+ CommandLine cl = parser.parse( _options, new String[]{"--export"});
+ assertTrue( "Confirm --export is set", cl.hasOption("export") );
+ }
+ catch (ParseException e)
+ {
+ fail( e.toString() );
+ }
+
+ try
+ {
+ CommandLine cl = parser.parse( _options, new String[]{"--import"});
+ assertTrue( "Confirm --import is set", cl.hasOption("import") );
}
catch (ParseException e)
{
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ParseTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ParseTest.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ParseTest.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ParseTest.java Wed Jul 6 13:03:54 2005
@@ -87,6 +87,7 @@
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 arg of --bfile", cl.getOptionValue( "bfile" ).equals( "toast" ) );
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
}
catch (ParseException e)
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ValueTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ValueTest.java?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ValueTest.java (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/src/test/org/apache/commons/cli/ValueTest.java Wed Jul 6 13:03:54 2005
@@ -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());
+ }
+ }
+
}
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/introduction.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/introduction.xml?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/introduction.xml (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/introduction.xml Wed Jul 6 13:03:54 2005
@@ -50,7 +50,7 @@
The <code>parse</code> method defined on
<a href="apidocs/org/apache/commons/cli/CommandLineParser.html">
CommandLineParser</a> takes an <code>Options</code>
- instance and a <code>java.util.List</code> of arguments and
+ instance and a <code>String[]</code> of arguments and
returns a
<a href="apidocs/org/apache/commons/cli/CommandLine.html">
CommandLine</a>.
Modified: jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/usage.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/usage.xml?rev=209508&r1=209507&r2=209508&view=diff
==============================================================================
--- jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/usage.xml (original)
+++ jakarta/commons/proper/cli/branches/CLI_1_BRANCH/xdocs/usage.xml Wed Jul 6 13:03:54 2005
@@ -50,10 +50,11 @@
</subsection>
<subsection name="Parsing the command line arguments">
<p>
- The <code>parse</code> methods of <code>Options</code> are used to
- parse the command line arguments.
+ The <code>parse</code> methods of <code>CommandLineParser</code> are used
+ to parse the command line arguments.
</p>
- <source>CommandLine cmd = options.parse(args);</source>
+ <source>CommandLineParser parser = new PosixParser();
+CommandLine cmd = parser.parse( options, args);</source>
<p>
Now we need to check if the <code>t</code> option is present. To do
this we will interrogate the
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org