Skip to content

Commit 1f984e2

Browse files
author
Robert James Oxspring
committed
GroupImpl.canProcess() was returning true when the argument looked like an option but wasn't accepted by the children.
Option.canProcess() now takes a WriteableCommandLine to provide the looksLikeOption() method. Bug discovered by Steve Alberty git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@179892 13f79535-47bb-0310-9956-ffa450edef68
1 parent 083189d commit 1f984e2

19 files changed

Lines changed: 127 additions & 56 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ void process(
6565
* The argument to be tested
6666
* @return true if the argument can be processed by this Option
6767
*/
68-
boolean canProcess(final String argument);
68+
boolean canProcess(final WriteableCommandLine commandLine, final String argument);
6969

7070
/**
7171
* Indicates whether this Option will be able to process the particular
@@ -77,7 +77,7 @@ void process(
7777
* the ListIterator over String arguments
7878
* @return true if the argument can be processed by this Option
7979
*/
80-
boolean canProcess(final ListIterator arguments);
80+
boolean canProcess(final WriteableCommandLine commandLine, final ListIterator arguments);
8181

8282
/**
8383
* Identifies the argument prefixes that should trigger this option. This

src/java/org/apache/commons/cli2/commandline/Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ public CommandLine parse(final String[] arguments) throws OptionException {
6363
group.defaults(commandLine);
6464

6565
final ListIterator iterator = argumentList.listIterator();
66-
while (group.canProcess(iterator)) {
66+
while (group.canProcess(commandLine, iterator)) {
6767
group.process(commandLine, iterator);
6868
}
6969

src/java/org/apache/commons/cli2/option/ArgumentImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -195,7 +195,7 @@ else if (subsequentSplit) {
195195
}
196196
}
197197

198-
public boolean canProcess(String arg) {
198+
public boolean canProcess(final WriteableCommandLine commandLine, final String arg) {
199199
return true;
200200
}
201201

src/java/org/apache/commons/cli2/option/Command.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,7 +109,7 @@ public void processParent(
109109
final String arg = (String)arguments.next();
110110

111111
// if we can process it
112-
if (canProcess(arg)) {
112+
if (canProcess(commandLine, arg)) {
113113

114114
// then note the option
115115
commandLine.addOption(this);

src/java/org/apache/commons/cli2/option/DefaultOption.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -127,9 +127,9 @@ public DefaultOption(
127127
checkPrefixes(newPrefixes);
128128
}
129129

130-
public boolean canProcess(final String argument) {
130+
public boolean canProcess(final WriteableCommandLine commandLine, final String argument) {
131131
return argument != null
132-
&& (super.canProcess(argument)
132+
&& (super.canProcess(commandLine, argument)
133133
|| (argument.length() >= burstLength
134134
&& burstAliases.contains(argument.substring(0, burstLength))));
135135
}

src/java/org/apache/commons/cli2/option/GroupImpl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,7 +113,7 @@ public GroupImpl(
113113
this.prefixes = Collections.unmodifiableSet(newPrefixes);
114114
}
115115

116-
public boolean canProcess(String arg) {
116+
public boolean canProcess(final WriteableCommandLine commandLine, String arg) {
117117
if (arg == null) {
118118
return false;
119119
}
@@ -131,11 +131,15 @@ public boolean canProcess(String arg) {
131131
iter.hasNext();) {
132132

133133
final Option option = (Option) iter.next();
134-
if (option.canProcess(arg)) {
134+
if (option.canProcess(commandLine, arg)) {
135135
return true;
136136
}
137137
}
138138

139+
if(commandLine.looksLikeOption(arg)) {
140+
return false;
141+
}
142+
139143
// anonymous argument(s) means we can process it
140144
if (anonymous.size() > 0) {
141145
return true;
@@ -182,7 +186,7 @@ public void process(
182186
for (Iterator i = values.iterator(); i.hasNext() && !foundMemberOption;) {
183187
final Option option = (Option) i.next();
184188

185-
if (option.canProcess(arg)) {
189+
if (option.canProcess(commandLine, arg)) {
186190
foundMemberOption = true;
187191
arguments.previous();
188192
option.process(commandLine, arguments);
@@ -210,7 +214,7 @@ public void process(
210214
// canProcess will always return true?
211215
for (final Iterator i = anonymous.iterator(); i.hasNext();) {
212216
final Argument argument = (Argument)i.next();
213-
if (argument.canProcess(arguments)) {
217+
if (argument.canProcess(commandLine, arguments)) {
214218
argument.process(commandLine, arguments);
215219
}
216220
}

src/java/org/apache/commons/cli2/option/OptionImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,11 +43,11 @@ public OptionImpl(final int id, final boolean required) {
4343
this.required = required;
4444
}
4545

46-
public boolean canProcess(final ListIterator arguments) {
46+
public boolean canProcess(final WriteableCommandLine commandLine, final ListIterator arguments) {
4747
if (arguments.hasNext()) {
4848
final String argument = (String)arguments.next();
4949
arguments.previous();
50-
return canProcess(argument);
50+
return canProcess(commandLine, argument);
5151
}
5252
else {
5353
return false;

src/java/org/apache/commons/cli2/option/ParentImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,7 +77,7 @@ public void process(
7777
argument.processValues(commandLine, arguments, this);
7878
}
7979

80-
if (children != null && children.canProcess(arguments)) {
80+
if (children != null && children.canProcess(commandLine, arguments)) {
8181
children.process(commandLine, arguments);
8282
}
8383
}
@@ -87,7 +87,7 @@ public void process(
8787
*
8888
* @see org.apache.commons.cli2.Option#canProcess(java.lang.String)
8989
*/
90-
public boolean canProcess(final String arg) {
90+
public boolean canProcess(final WriteableCommandLine commandLine, final String arg) {
9191

9292
final Set triggers = getTriggers();
9393

src/java/org/apache/commons/cli2/option/PropertyOption.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,7 +68,7 @@ public PropertyOption(
6868
*/
6969
public static final PropertyOption INSTANCE = new PropertyOption();
7070

71-
public boolean canProcess(final String argument) {
71+
public boolean canProcess(final WriteableCommandLine commandLine, final String argument) {
7272
return argument != null
7373
&& argument.startsWith(optionString)
7474
&& argument.length() > optionString.length();
@@ -85,7 +85,7 @@ public void process(
8585

8686
final String arg = (String)arguments.next();
8787

88-
if (!canProcess(arg)) {
88+
if (!canProcess(commandLine, arg)) {
8989
throw new OptionException(this, "cli.error.unexpected", arg);
9090
}
9191

src/java/org/apache/commons/cli2/option/SourceDestArgument.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,7 +138,7 @@ public void validate(WriteableCommandLine commandLine, Option option)
138138
dest.validate(commandLine, dest);
139139
}
140140

141-
public boolean canProcess(final String arg) {
142-
return source.canProcess(arg) || dest.canProcess(arg);
141+
public boolean canProcess(final WriteableCommandLine commandLine, final String arg) {
142+
return source.canProcess(commandLine, arg) || dest.canProcess(commandLine, arg);
143143
}
144144
}

0 commit comments

Comments
 (0)