Skip to content

Commit 99aa05a

Browse files
committed
Fixed PosixParser when an unrecognized long option is found (CLI-165)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@680299 13f79535-47bb-0310-9956-ffa450edef68
1 parent b133180 commit 99aa05a

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,22 @@ protected String[] flatten(Options options, String[] arguments, boolean stopAtNo
108108
// get the next command line token
109109
String token = (String) iter.next();
110110

111-
// handle SPECIAL TOKEN
111+
// handle long option --foo or --foo=bar
112112
if (token.startsWith("--"))
113113
{
114-
if (token.indexOf('=') != -1)
114+
int pos = token.indexOf('=');
115+
String opt = pos == -1 ? token : token.substring(0, pos); // --foo
116+
117+
if (!options.hasOption(opt) && stopAtNonOption)
115118
{
116-
tokens.add(token.substring(0, token.indexOf('=')));
117-
tokens.add(token.substring(token.indexOf('=') + 1, token.length()));
119+
process(token);
118120
}
119121
else
120122
{
121-
tokens.add(token);
123+
tokens.add(opt);
124+
if (pos != -1) {
125+
tokens.add(token.substring(pos + 1));
126+
}
122127
}
123128
}
124129

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ public void testStop2() throws Exception
156156
assertTrue("Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
157157
}
158158

159+
public void testStop3() throws Exception
160+
{
161+
String[] args = new String[]{"--zop==1",
162+
"-abtoast",
163+
"--b=bar"};
164+
165+
CommandLine cl = parser.parse(options, args, true);
166+
167+
assertFalse("Confirm -a is not set", cl.hasOption("a"));
168+
assertFalse("Confirm -b is not set", cl.hasOption("b"));
169+
assertTrue("Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
170+
}
171+
159172
public void testStopBursting() throws Exception
160173
{
161174
String[] args = new String[] { "-azc" };

0 commit comments

Comments
 (0)