Skip to content

Commit f04a1fb

Browse files
committed
Applying Brian Egge's fix and test from CLI-122
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@589237 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6b97c7a commit f04a1fb

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,18 @@ public void processValues(final WriteableCommandLine commandLine,
143143
int argumentCount = commandLine.getValues(option, Collections.EMPTY_LIST).size();
144144

145145
while (arguments.hasNext() && (argumentCount < maximum)) {
146-
final String allValues = stripBoundaryQuotes((String) arguments.next());
146+
final String allValuesQuoted = (String) arguments.next();
147+
final String allValues = stripBoundaryQuotes(allValuesQuoted);
147148

148149
// should we ignore things that look like options?
149-
if (allValues.equals(consumeRemaining)) {
150+
if (allValuesQuoted.equals(consumeRemaining)) {
150151
while (arguments.hasNext() && (argumentCount < maximum)) {
151152
++argumentCount;
152153
commandLine.addValue(option, arguments.next());
153154
}
154155
}
155156
// does it look like an option?
156-
else if (commandLine.looksLikeOption(allValues)) {
157+
else if (commandLine.looksLikeOption(allValuesQuoted)) {
157158
arguments.previous();
158159

159160
break;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,13 @@ private void handleInitialSeparator(final ListIterator arguments,
217217
if (initialIndex > 0) {
218218
arguments.remove();
219219
arguments.add(newArgument.substring(0, initialIndex));
220-
arguments.add(newArgument.substring(initialIndex + 1));
220+
String value = newArgument.substring(initialIndex + 1);
221+
// The value obviously isn't an option, so we need to quote it if looks like an option.
222+
// The quotes will be removed later
223+
if (value.startsWith("-")) {
224+
value = '"' + value + '"';
225+
}
226+
arguments.add(value);
221227
arguments.previous();
222228
}
223229

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.apache.commons.cli2.bug;
2+
3+
import junit.framework.TestCase;
4+
import org.apache.commons.cli2.*;
5+
import org.apache.commons.cli2.builder.ArgumentBuilder;
6+
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
7+
import org.apache.commons.cli2.builder.GroupBuilder;
8+
import org.apache.commons.cli2.commandline.Parser;
9+
import org.apache.commons.cli2.validation.FileValidator;
10+
11+
/**
12+
* @author brianegge
13+
*/
14+
public class BugCLI122Test extends TestCase {
15+
public void testArgumentWhichStartsWithDash() throws OptionException {
16+
Argument wdArg = new ArgumentBuilder()
17+
.withName("anything")
18+
.withMaximum(1)
19+
.withMinimum(1)
20+
.withInitialSeparator('=')
21+
.create();
22+
23+
Option wdOpt = new DefaultOptionBuilder().withArgument(wdArg)
24+
.withDescription("anything, foo or -foo")
25+
.withLongName("argument")
26+
.withShortName("a")
27+
.create();
28+
29+
Group group = new GroupBuilder().withOption(wdOpt).create();
30+
31+
Parser p = new Parser();
32+
p.setGroup(group);
33+
CommandLine normal = p.parse (new String[]{"-a", "foo"});
34+
assertNotNull(normal);
35+
assertEquals(normal.getValue(wdOpt), "foo");
36+
37+
CommandLine withDash = p.parse (new String[]{"--argument", "\"-foo\""});
38+
assertNotNull(withDash);
39+
assertEquals("-foo", withDash.getValue(wdOpt));
40+
41+
CommandLine withDashAndEquals = p.parse (new String[]{"--argument=-foo"});
42+
assertNotNull(withDashAndEquals);
43+
assertEquals("-foo", withDashAndEquals.getValue(wdOpt));
44+
}
45+
}

0 commit comments

Comments
 (0)