Skip to content

Commit 20dfc60

Browse files
committed
Restored the CLI 1.0 behavior regarding repeated options and the number of arguments per option (CLI-137)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@667565 13f79535-47bb-0310-9956-ffa450edef68
1 parent 459f2d0 commit 20dfc60

3 files changed

Lines changed: 62 additions & 57 deletions

File tree

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
package org.apache.commons.cli;
1818

1919
import java.io.Serializable;
20+
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.Iterator;
2223
import java.util.LinkedList;
2324
import java.util.List;
24-
import java.util.Set;
25-
import java.util.HashSet;
2625

2726
/**
2827
* <p>Represents list of arguments parsed against
@@ -47,7 +46,7 @@ public class CommandLine implements Serializable {
4746
private List args = new LinkedList();
4847

4948
/** the processed options */
50-
private Set options = new HashSet();
49+
private List options = new ArrayList();
5150

5251
/** Map of unique options for ease to get complete list of options */
5352
// private Set allOptions = new HashSet();
@@ -149,15 +148,19 @@ public String getOptionValue(char opt)
149148
*/
150149
public String[] getOptionValues(String opt)
151150
{
152-
Option key = resolveOption( opt );
151+
List values = new ArrayList();
153152

154-
if (options.contains(key))
153+
for ( Iterator it = options.iterator(); it.hasNext(); )
155154
{
156-
return key.getValues();
155+
Option option = (Option) it.next();
156+
if (opt.equals(option.getOpt()) || opt.equals( option.getLongOpt()))
157+
{
158+
values.addAll(option.getValuesList());
159+
}
157160
}
158161

159-
return null;
160-
}
162+
return values.isEmpty() ? null : (String[]) values.toArray(new String[values.size()]);
163+
}
161164

162165
/**
163166
* <p>Retrieves the option object given the long or short option as a String</p>
@@ -318,4 +321,4 @@ public Option[] getOptions()
318321
// return the array
319322
return (Option[]) processed.toArray(optionsArray);
320323
}
321-
}
324+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ protected void processOption(String arg, ListIterator iter)
396396
}
397397

398398
// get the option represented by arg
399-
final Option opt = getOptions().getOption(arg);
399+
Option opt = (Option) getOptions().getOption(arg).clone();
400400

401401
// if the option is a required option remove the option from
402402
// the requiredOptions list

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

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.PrintWriter;
2323
import java.io.StringWriter;
2424
import java.util.Iterator;
25+
import java.util.List;
2526
import java.util.Properties;
2627

2728
import junit.framework.TestCase;
@@ -31,8 +32,7 @@ public class BugsTest extends TestCase
3132
public void test11457() throws Exception
3233
{
3334
Options options = new Options();
34-
options.addOption(OptionBuilder.withLongOpt("verbose")
35-
.create());
35+
options.addOption(OptionBuilder.withLongOpt("verbose").create());
3636
String[] args = new String[]{"--verbose"};
3737

3838
CommandLineParser parser = new PosixParser();
@@ -44,12 +44,8 @@ public void test11457() throws Exception
4444
public void test11458() throws Exception
4545
{
4646
Options options = new Options();
47-
options.addOption( OptionBuilder.withValueSeparator( '=' )
48-
.hasArgs()
49-
.create( 'D' ) );
50-
options.addOption( OptionBuilder.withValueSeparator( ':' )
51-
.hasArgs()
52-
.create( 'p' ) );
47+
options.addOption( OptionBuilder.withValueSeparator( '=' ).hasArgs().create( 'D' ) );
48+
options.addOption( OptionBuilder.withValueSeparator( ':' ).hasArgs().create( 'p' ) );
5349
String[] args = new String[] { "-DJAVA_HOME=/opt/java" , "-pfile1:file2:file3" };
5450

5551
CommandLineParser parser = new PosixParser();
@@ -107,10 +103,8 @@ public void test11456() throws Exception
107103
{
108104
// Posix
109105
Options options = new Options();
110-
options.addOption( OptionBuilder.hasOptionalArg()
111-
.create( 'a' ) );
112-
options.addOption( OptionBuilder.hasArg()
113-
.create( 'b' ) );
106+
options.addOption( OptionBuilder.hasOptionalArg().create( 'a' ) );
107+
options.addOption( OptionBuilder.hasArg().create( 'b' ) );
114108
String[] args = new String[] { "-a", "-bvalue" };
115109

116110
CommandLineParser parser = new PosixParser();
@@ -120,10 +114,8 @@ public void test11456() throws Exception
120114

121115
// GNU
122116
options = new Options();
123-
options.addOption( OptionBuilder.hasOptionalArg()
124-
.create( 'a' ) );
125-
options.addOption( OptionBuilder.hasArg()
126-
.create( 'b' ) );
117+
options.addOption( OptionBuilder.hasOptionalArg().create( 'a' ) );
118+
options.addOption( OptionBuilder.hasArg().create( 'b' ) );
127119
args = new String[] { "-a", "-b", "value" };
128120

129121
parser = new GnuParser();
@@ -227,12 +219,9 @@ public void test13425() throws Exception
227219
public void test13666() throws Exception
228220
{
229221
Options options = new Options();
230-
Option dir = OptionBuilder.withDescription( "dir" )
231-
.hasArg()
232-
.create( 'd' );
222+
Option dir = OptionBuilder.withDescription( "dir" ).hasArg().create( 'd' );
233223
options.addOption( dir );
234224

235-
236225
final PrintStream oldSystemOut = System.out;
237226
try
238227
{
@@ -279,29 +268,35 @@ public void test13935() throws Exception
279268
boolean exception = false;
280269

281270
String[] args = new String[] { };
282-
try {
283-
CommandLine line = parser.parse( opts, args );
271+
try
272+
{
273+
CommandLine line = parser.parse(opts, args);
284274
}
285-
catch( ParseException exp ) {
275+
catch (ParseException exp)
276+
{
286277
exception = true;
287278
}
288279

289-
if( !exception ) {
290-
fail( "Expected exception not caught.");
280+
if (!exception)
281+
{
282+
fail("Expected exception not caught.");
291283
}
292284

293285
exception = false;
294286

295287
args = new String[] { "-s" };
296-
try {
297-
CommandLine line = parser.parse( opts, args );
288+
try
289+
{
290+
CommandLine line = parser.parse(opts, args);
298291
}
299-
catch( ParseException exp ) {
292+
catch (ParseException exp)
293+
{
300294
exception = true;
301295
}
302296

303-
if( !exception ) {
304-
fail( "Expected exception not caught.");
297+
if (!exception)
298+
{
299+
fail("Expected exception not caught.");
305300
}
306301

307302
exception = false;
@@ -328,7 +323,8 @@ public void test13935() throws Exception
328323
}
329324
}
330325

331-
public void test14786() throws Exception {
326+
public void test14786() throws Exception
327+
{
332328
Option o = OptionBuilder.isRequired().withDescription("test").create("test");
333329
Options opts = new Options();
334330
opts.addOption(o);
@@ -342,23 +338,25 @@ public void test14786() throws Exception {
342338
assertTrue( line.hasOption( "test" ) );
343339
}
344340

345-
public void test15046() throws Exception {
341+
public void test15046() throws Exception
342+
{
346343
CommandLineParser parser = new PosixParser();
347-
final String[] CLI_ARGS = new String[] {"-z", "c"};
348-
Option option = new Option("z", "timezone", true,
349-
"affected option");
350-
Options cliOptions = new Options();
351-
cliOptions.addOption(option);
352-
parser.parse(cliOptions, CLI_ARGS);
353-
344+
String[] CLI_ARGS = new String[] {"-z", "c"};
345+
346+
Options options = new Options();
347+
options.addOption(new Option("z", "timezone", true, "affected option"));
348+
349+
parser.parse(options, CLI_ARGS);
350+
354351
//now add conflicting option
355-
cliOptions.addOption("c", "conflict", true, "conflict option");
356-
CommandLine line = parser.parse(cliOptions, CLI_ARGS);
357-
assertEquals( option.getValue(), "c" );
352+
options.addOption("c", "conflict", true, "conflict option");
353+
CommandLine line = parser.parse(options, CLI_ARGS);
354+
assertEquals( line.getOptionValue('z'), "c" );
358355
assertTrue( !line.hasOption("c") );
359356
}
360357

361-
public void test15648() throws Exception {
358+
public void test15648() throws Exception
359+
{
362360
CommandLineParser parser = new PosixParser();
363361
final String[] args = new String[] { "-m", "\"Two Words\"" };
364362
Option m = OptionBuilder.hasArgs().create("m");
@@ -368,7 +366,8 @@ public void test15648() throws Exception {
368366
assertEquals( "Two Words", line.getOptionValue( "m" ) );
369367
}
370368

371-
public void test27635() {
369+
public void test27635()
370+
{
372371
Option help = new Option("h", "help", false, "print this message");
373372
Option version = new Option("v", "version", false, "print version information");
374373
Option newRun = new Option("n", "new", false, "Create NLT cache entries only for new items");
@@ -436,7 +435,8 @@ public void test27635() {
436435
,out.toString());
437436
}
438437

439-
public void test31148() throws ParseException {
438+
public void test31148() throws ParseException
439+
{
440440
Option multiArgOption = new Option("o","option with multiple args");
441441
multiArgOption.setArgs(1);
442442

@@ -453,7 +453,8 @@ public void test31148() throws ParseException {
453453
assertEquals("ovalue",cl.getOptionValue('o'));
454454
}
455455

456-
public void test21215() {
456+
public void test21215()
457+
{
457458
Options options = new Options();
458459
HelpFormatter formatter = new HelpFormatter();
459460
String SEP = System.getProperty("line.separator");
@@ -470,7 +471,8 @@ public void test21215() {
470471
,out.toString());
471472
}
472473

473-
public void test19383() {
474+
public void test19383()
475+
{
474476
Options options = new Options();
475477
options.addOption(new Option("a","aaa",false,"aaaaaaa"));
476478
options.addOption(new Option(null,"bbb",false,"bbbbbbb"));

0 commit comments

Comments
 (0)