Skip to content

Commit b0e1b80

Browse files
committed
Applying Brian Egge's fix and unit test from CLI-13.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/branches/cli-1.0.x@538031 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8f46f46 commit b0e1b80

3 files changed

Lines changed: 123 additions & 38 deletions

File tree

src/java/org/apache/commons/cli/CommandLine.java

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
package org.apache.commons.cli;
1717

1818
import java.util.Collection;
19-
import java.util.HashMap;
2019
import java.util.Iterator;
2120
import java.util.LinkedList;
2221
import java.util.List;
23-
import java.util.Map;
22+
import java.util.Set;
23+
import java.util.HashSet;
2424

2525
/**
2626
* <p>Represents list of arguments parsed against
@@ -43,16 +43,10 @@ public class CommandLine {
4343
private List args = new LinkedList();
4444

4545
/** the processed options */
46-
private Map options = new HashMap();
47-
48-
/** the option name map */
49-
private Map names = new HashMap();
46+
private Set options = new HashSet();
5047

5148
/** Map of unique options for ease to get complete list of options */
52-
private Map hashcodeMap = new HashMap();
53-
54-
/** the processed options */
55-
private Option[] optionsArray;
49+
// private Set allOptions = new HashSet();
5650

5751
/**
5852
* Creates a command line.
@@ -70,7 +64,7 @@ public class CommandLine {
7064
*/
7165
public boolean hasOption(String opt)
7266
{
73-
return options.containsKey(opt);
67+
return options.contains( resolveOption(opt));
7468
}
7569

7670
/**
@@ -94,12 +88,13 @@ public Object getOptionObject(String opt)
9488
{
9589
String res = getOptionValue(opt);
9690

97-
if (!options.containsKey(opt))
91+
Option option = resolveOption(opt);
92+
if (option == null)
9893
{
9994
return null;
10095
}
10196

102-
Object type = ((Option) options.get(opt)).getType();
97+
Object type = option.getType();
10398

10499
return (res == null) ? null : TypeHandler.createValue(res, type);
105100
}
@@ -150,20 +145,37 @@ public String getOptionValue(char opt)
150145
*/
151146
public String[] getOptionValues(String opt)
152147
{
153-
opt = Util.stripLeadingHyphens(opt);
154-
155-
String key = opt;
148+
Option key = resolveOption( opt );
156149

157-
if (names.containsKey(opt))
150+
if (options.contains(key))
158151
{
159-
key = (String) names.get(opt);
152+
return key.getValues();
160153
}
161154

162-
if (options.containsKey(key))
155+
return null;
156+
}
157+
158+
/**
159+
* <p>Retrieves the option object given the long or short option as a String</p>
160+
* @param opt short or long name of the option
161+
* @return Canonicalized option
162+
*/
163+
private Option resolveOption( String opt )
164+
{
165+
opt = Util.stripLeadingHyphens(opt);
166+
for ( Iterator it = options.iterator(); it.hasNext(); )
163167
{
164-
return ((Option) options.get(key)).getValues();
168+
Option option = (Option) it.next();
169+
if (opt.equals(option.getOpt()))
170+
{
171+
return option;
172+
}
173+
if (opt.equals( option.getLongOpt()))
174+
{
175+
return option;
165176
}
166177

178+
}
167179
return null;
168180
}
169181

@@ -273,20 +285,7 @@ void addArg(String arg)
273285
*/
274286
void addOption(Option opt)
275287
{
276-
hashcodeMap.put(new Integer(opt.hashCode()), opt);
277-
278-
String key = opt.getKey();
279-
280-
if (key == null)
281-
{
282-
key = opt.getLongOpt();
283-
}
284-
else
285-
{
286-
names.put(opt.getLongOpt(), key);
287-
}
288-
289-
options.put(key, opt);
288+
options.add(opt);
290289
}
291290

292291
/**
@@ -297,7 +296,7 @@ void addOption(Option opt)
297296
*/
298297
public Iterator iterator()
299298
{
300-
return hashcodeMap.values().iterator();
299+
return options.iterator();
301300
}
302301

303302
/**
@@ -307,11 +306,10 @@ public Iterator iterator()
307306
*/
308307
public Option[] getOptions()
309308
{
310-
Collection processed = options.values();
311-
309+
Collection processed = options;
312310

313311
// reinitialise array
314-
optionsArray = new Option[processed.size()];
312+
Option[] optionsArray = new Option[processed.size()];
315313

316314
// return the array
317315
return (Option[]) processed.toArray(optionsArray);

src/java/org/apache/commons/cli/Option.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,39 @@ private boolean hasNoValues()
596596
{
597597
return this.values.size() == 0;
598598
}
599+
600+
public boolean equals( Object o )
601+
{
602+
if ( this == o )
603+
{
604+
return true;
605+
}
606+
if ( o == null || getClass() != o.getClass() )
607+
{
608+
return false;
609+
}
610+
611+
Option option = (Option) o;
612+
613+
614+
if ( opt != null ? !opt.equals( option.opt ) : option.opt != null )
615+
{
616+
return false;
617+
}
618+
if ( longOpt != null ? !longOpt.equals( option.longOpt ) : option.longOpt != null )
619+
{
620+
return false;
621+
}
622+
623+
return true;
624+
}
625+
626+
public int hashCode()
627+
{
628+
int result;
629+
result = ( opt != null ? opt.hashCode() : 0 );
630+
result = 31 * result + ( longOpt != null ? longOpt.hashCode() : 0 );
631+
return result;
632+
}
633+
599634
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.cli.bug;
18+
19+
import junit.framework.TestCase;
20+
import org.apache.commons.cli.CommandLine;
21+
import org.apache.commons.cli.Option;
22+
import org.apache.commons.cli.OptionBuilder;
23+
import org.apache.commons.cli.Options;
24+
import org.apache.commons.cli.ParseException;
25+
import org.apache.commons.cli.PosixParser;
26+
27+
/**
28+
* @author brianegge
29+
*/
30+
public class BugCLI13Test
31+
extends TestCase
32+
{
33+
public void testCLI13()
34+
throws ParseException
35+
{
36+
final String debugOpt = "debug";
37+
Option debug = OptionBuilder
38+
.withArgName( debugOpt )
39+
.withDescription( "turn on debugging" )
40+
.withLongOpt( debugOpt )
41+
.hasArg()
42+
.create( 'd' );
43+
Options options = new Options();
44+
options.addOption( debug );
45+
CommandLine commandLine = new PosixParser().parse( options, new String[]{"-d", "true"} );
46+
47+
assertEquals("true", commandLine.getOptionValue( debugOpt ));
48+
assertEquals("true", commandLine.getOptionValue( 'd' ));
49+
assertTrue(commandLine.hasOption( 'd'));
50+
assertTrue(commandLine.hasOption( debugOpt));
51+
}
52+
}

0 commit comments

Comments
 (0)