Skip to content

Commit 0c281fb

Browse files
author
John Keyes
committed
added fix for Rob's problem (2), so it is now possible to query using either the opt or longOpt
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129843 13f79535-47bb-0310-9956-ffa450edef68
1 parent e9a3217 commit 0c281fb

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public class CommandLine {
9191
/** the processed options */
9292
private Map options = new HashMap();
9393

94+
/** the option name map */
95+
private Map names = new HashMap();
96+
9497
/** Map of unique options for ease to get complete list of options */
9598
private Map hashcodeMap = new HashMap();
9699

@@ -179,8 +182,13 @@ public String getOptionValue( char opt ) {
179182
public String[] getOptionValues( String opt ) {
180183
List values = new java.util.ArrayList();
181184

182-
if( options.containsKey( opt ) ) {
183-
List opts = (List)options.get( opt );
185+
String key = opt;
186+
if( names.containsKey( opt ) ) {
187+
key = (String)names.get( opt );
188+
}
189+
190+
if( options.containsKey( key ) ) {
191+
List opts = (List)options.get( key );
184192
Iterator iter = opts.iterator();
185193

186194
while( iter.hasNext() ) {
@@ -290,6 +298,9 @@ void addOption( Option opt ) {
290298
if( " ".equals(key) ) {
291299
key = opt.getLongOpt();
292300
}
301+
else {
302+
names.put( opt.getLongOpt(), key );
303+
}
293304

294305
if( options.get( key ) != null ) {
295306
((java.util.List)options.get( key )).add( opt );

src/test/org/apache/commons/cli/ParseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void testSimpleLong()
8787
assertTrue( "Confirm -a is set", cl.hasOption("a") );
8888
assertTrue( "Confirm -b is set", cl.hasOption("b") );
8989
assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
90+
assertTrue( "Confirm arg of --bfile", cl.getOptionValue( "bfile" ).equals( "toast" ) );
9091
assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
9192
}
9293
catch (ParseException e)

0 commit comments

Comments
 (0)