Author: bayard Date: Mon May 14 17:47:38 2007 New Revision: 538031 URL: http://svn.apache.org/viewvc?view=rev&rev=538031 Log: Applying Brian Egge's fix and unit test from CLI-13.
Added: jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI13Test.java Modified: jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/CommandLine.java jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java Modified: jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/CommandLine.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/CommandLine.java?view=diff&rev=538031&r1=538030&r2=538031 ============================================================================== --- jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/CommandLine.java (original) +++ jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/CommandLine.java Mon May 14 17:47:38 2007 @@ -16,11 +16,11 @@ package org.apache.commons.cli; import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.Map; +import java.util.Set; +import java.util.HashSet; /** * <p>Represents list of arguments parsed against @@ -43,16 +43,10 @@ private List args = new LinkedList(); /** the processed options */ - private Map options = new HashMap(); - - /** the option name map */ - private Map names = new HashMap(); + private Set options = new HashSet(); /** Map of unique options for ease to get complete list of options */ - private Map hashcodeMap = new HashMap(); - - /** the processed options */ - private Option[] optionsArray; +// private Set allOptions = new HashSet(); /** * Creates a command line. @@ -70,7 +64,7 @@ */ public boolean hasOption(String opt) { - return options.containsKey(opt); + return options.contains( resolveOption(opt)); } /** @@ -94,12 +88,13 @@ { String res = getOptionValue(opt); - if (!options.containsKey(opt)) + Option option = resolveOption(opt); + if (option == null) { return null; } - Object type = ((Option) options.get(opt)).getType(); + Object type = option.getType(); return (res == null) ? null : TypeHandler.createValue(res, type); } @@ -150,20 +145,37 @@ */ public String[] getOptionValues(String opt) { - opt = Util.stripLeadingHyphens(opt); - - String key = opt; + Option key = resolveOption( opt ); - if (names.containsKey(opt)) + if (options.contains(key)) { - key = (String) names.get(opt); + return key.getValues(); } - if (options.containsKey(key)) + return null; + } + + /** + * <p>Retrieves the option object given the long or short option as a String</p> + * @param opt short or long name of the option + * @return Canonicalized option + */ + private Option resolveOption( String opt ) + { + opt = Util.stripLeadingHyphens(opt); + for ( Iterator it = options.iterator(); it.hasNext(); ) { - return ((Option) options.get(key)).getValues(); + Option option = (Option) it.next(); + if (opt.equals(option.getOpt())) + { + return option; + } + if (opt.equals( option.getLongOpt())) + { + return option; } + } return null; } @@ -273,20 +285,7 @@ */ void addOption(Option opt) { - hashcodeMap.put(new Integer(opt.hashCode()), opt); - - String key = opt.getKey(); - - if (key == null) - { - key = opt.getLongOpt(); - } - else - { - names.put(opt.getLongOpt(), key); - } - - options.put(key, opt); + options.add(opt); } /** @@ -297,7 +296,7 @@ */ public Iterator iterator() { - return hashcodeMap.values().iterator(); + return options.iterator(); } /** @@ -307,11 +306,10 @@ */ public Option[] getOptions() { - Collection processed = options.values(); - + Collection processed = options; // reinitialise array - optionsArray = new Option[processed.size()]; + Option[] optionsArray = new Option[processed.size()]; // return the array return (Option[]) processed.toArray(optionsArray); Modified: jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java?view=diff&rev=538031&r1=538030&r2=538031 ============================================================================== --- jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java (original) +++ jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java Mon May 14 17:47:38 2007 @@ -596,4 +596,39 @@ { return this.values.size() == 0; } + + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + Option option = (Option) o; + + + if ( opt != null ? !opt.equals( option.opt ) : option.opt != null ) + { + return false; + } + if ( longOpt != null ? !longOpt.equals( option.longOpt ) : option.longOpt != null ) + { + return false; + } + + return true; + } + + public int hashCode() + { + int result; + result = ( opt != null ? opt.hashCode() : 0 ); + result = 31 * result + ( longOpt != null ? longOpt.hashCode() : 0 ); + return result; + } + } Added: jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI13Test.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI13Test.java?view=auto&rev=538031 ============================================================================== --- jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI13Test.java (added) +++ jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI13Test.java Mon May 14 17:47:38 2007 @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.cli.bug; + +import junit.framework.TestCase; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; + +/** + * @author brianegge + */ +public class BugCLI13Test + extends TestCase +{ + public void testCLI13() + throws ParseException + { + final String debugOpt = "debug"; + Option debug = OptionBuilder + .withArgName( debugOpt ) + .withDescription( "turn on debugging" ) + .withLongOpt( debugOpt ) + .hasArg() + .create( 'd' ); + Options options = new Options(); + options.addOption( debug ); + CommandLine commandLine = new PosixParser().parse( options, new String[]{"-d", "true"} ); + + assertEquals("true", commandLine.getOptionValue( debugOpt )); + assertEquals("true", commandLine.getOptionValue( 'd' )); + assertTrue(commandLine.hasOption( 'd')); + assertTrue(commandLine.hasOption( debugOpt)); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]