Skip to content

Commit c860625

Browse files
committed
Fixed the handling of unrecognized options starting with '-' by PosixParser (CLI-164)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@680287 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9c5ce35 commit c860625

2 files changed

Lines changed: 21 additions & 25 deletions

File tree

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

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,11 @@ protected String[] flatten(Options options, String[] arguments, boolean stopAtNo
125125
// single hyphen
126126
else if ("-".equals(token))
127127
{
128-
processSingleHyphen(token);
128+
tokens.add(token);
129129
}
130130
else if (token.startsWith("-"))
131131
{
132-
int tokenLength = token.length();
133-
134-
if (tokenLength == 2)
132+
if (token.length() == 2)
135133
{
136134
processOptionToken(token, stopAtNonOption);
137135
}
@@ -145,16 +143,13 @@ else if (options.hasOption(token))
145143
burstToken(token, stopAtNonOption);
146144
}
147145
}
146+
else if (stopAtNonOption)
147+
{
148+
process(token);
149+
}
148150
else
149151
{
150-
if (stopAtNonOption)
151-
{
152-
process(token);
153-
}
154-
else
155-
{
156-
tokens.add(token);
157-
}
152+
tokens.add(token);
158153
}
159154

160155
gobble(iter);
@@ -216,17 +211,6 @@ else if (currentOption.hasArgs())
216211
}
217212
}
218213

219-
/**
220-
* If it is a hyphen then add the hyphen directly to
221-
* the processed tokens list.
222-
*
223-
* @param hyphen The hyphen token
224-
*/
225-
private void processSingleHyphen(String hyphen)
226-
{
227-
tokens.add(hyphen);
228-
}
229-
230214
/**
231215
* <p>If an {@link Option} exists for <code>token</code> then
232216
* set the current option and add the token to the processed
@@ -242,14 +226,15 @@ private void processSingleHyphen(String hyphen)
242226
*/
243227
private void processOptionToken(String token, boolean stopAtNonOption)
244228
{
245-
if (this.options.hasOption(token))
229+
if (options.hasOption(token))
246230
{
247-
currentOption = this.options.getOption(token);
231+
currentOption = options.getOption(token);
248232
tokens.add(token);
249233
}
250234
else if (stopAtNonOption)
251235
{
252236
eatTheRest = true;
237+
tokens.add(token);
253238
}
254239
}
255240

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ public void testStop() throws Exception
130130
assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
131131
}
132132

133+
public void testStop2() throws Exception
134+
{
135+
String[] args = new String[]{"-z",
136+
"-a",
137+
"-btoast"};
138+
139+
CommandLine cl = parser.parse(options, args, true);
140+
assertFalse("Confirm -a is not set", cl.hasOption("a"));
141+
assertTrue("Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
142+
}
143+
133144
public void testStopBursting() throws Exception
134145
{
135146
String[] args = new String[] { "-azc" };

0 commit comments

Comments
 (0)