Skip to content

Commit 9c5ce35

Browse files
committed
Fixed PosixParser to stop bursting tokens when a non option character is found (CLI-163)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@680208 13f79535-47bb-0310-9956-ffa450edef68
1 parent cfbb46a commit 9c5ce35

2 files changed

Lines changed: 34 additions & 22 deletions

File tree

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
package org.apache.commons.cli;
1819

1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Iterator;
23+
import java.util.List;
2224

2325
/**
2426
* The class PosixParser provides an implementation of the
@@ -31,7 +33,7 @@
3133
public class PosixParser extends Parser {
3234

3335
/** holder for flattened tokens */
34-
private ArrayList tokens = new ArrayList();
36+
private List tokens = new ArrayList();
3537

3638
/** specifies if bursting should continue */
3739
private boolean eatTheRest;
@@ -43,9 +45,9 @@ public class PosixParser extends Parser {
4345
private Options options;
4446

4547
/**
46-
* <p>Resets the members to their original state i.e. remove
48+
* Resets the members to their original state i.e. remove
4749
* all of <code>tokens</code> entries, set <code>eatTheRest</code>
48-
* to false and set <code>currentOption</code> to null.</p>
50+
* to false and set <code>currentOption</code> to null.
4951
*/
5052
private void init()
5153
{
@@ -92,30 +94,27 @@ private void init()
9294
* when an non option is found.
9395
* @return The flattened <code>arguments</code> String array.
9496
*/
95-
protected String[] flatten(Options options, String[] arguments,
96-
boolean stopAtNonOption)
97+
protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption)
9798
{
9899
init();
99100
this.options = options;
100101

101102
// an iterator for the command line tokens
102103
Iterator iter = Arrays.asList(arguments).iterator();
103-
String token;
104104

105105
// process each command line token
106106
while (iter.hasNext())
107107
{
108108
// get the next command line token
109-
token = (String) iter.next();
109+
String token = (String) iter.next();
110110

111111
// handle SPECIAL TOKEN
112112
if (token.startsWith("--"))
113113
{
114114
if (token.indexOf('=') != -1)
115115
{
116116
tokens.add(token.substring(0, token.indexOf('=')));
117-
tokens.add(token.substring(token.indexOf('=') + 1,
118-
token.length()));
117+
tokens.add(token.substring(token.indexOf('=') + 1, token.length()));
119118
}
120119
else
121120
{
@@ -136,8 +135,9 @@ else if (token.startsWith("-"))
136135
{
137136
processOptionToken(token, stopAtNonOption);
138137
}
139-
else if (options.hasOption(token)) {
140-
tokens.add(token);
138+
else if (options.hasOption(token))
139+
{
140+
tokens.add(token);
141141
}
142142
// requires bursting
143143
else
@@ -164,7 +164,7 @@ else if (options.hasOption(token)) {
164164
}
165165

166166
/**
167-
* <p>Adds the remaining tokens to the processed tokens list.</p>
167+
* Adds the remaining tokens to the processed tokens list.
168168
*
169169
* @param iter An iterator over the remaining tokens
170170
*/
@@ -183,8 +183,10 @@ private void gobble(Iterator iter)
183183
* <p>If there is a current option and it can have an argument
184184
* value then add the token to the processed tokens list and
185185
* set the current option to null.</p>
186+
*
186187
* <p>If there is a current option and it can have argument
187188
* values then add the token to the processed tokens list.</p>
189+
*
188190
* <p>If there is not a current option add the special token
189191
* "<b>--</b>" and the current <code>value</code> to the processed
190192
* tokens list. The add all the remaining <code>argument</code>
@@ -194,7 +196,7 @@ private void gobble(Iterator iter)
194196
*/
195197
private void process(String value)
196198
{
197-
if ((currentOption != null) && currentOption.hasArg())
199+
if (currentOption != null && currentOption.hasArg())
198200
{
199201
if (currentOption.hasArg())
200202
{
@@ -215,8 +217,8 @@ else if (currentOption.hasArgs())
215217
}
216218

217219
/**
218-
* <p>If it is a hyphen then add the hyphen directly to
219-
* the processed tokens list.</p>
220+
* If it is a hyphen then add the hyphen directly to
221+
* the processed tokens list.
220222
*
221223
* @param hyphen The hyphen token
222224
*/
@@ -229,6 +231,7 @@ private void processSingleHyphen(String hyphen)
229231
* <p>If an {@link Option} exists for <code>token</code> then
230232
* set the current option and add the token to the processed
231233
* list.</p>
234+
*
232235
* <p>If an {@link Option} does not exist and <code>stopAtNonOption</code>
233236
* is set then ignore the current token and add the remaining tokens
234237
* to the processed tokens list directly.</p>
@@ -278,14 +281,11 @@ else if (stopAtNonOption)
278281
*/
279282
protected void burstToken(String token, boolean stopAtNonOption)
280283
{
281-
int tokenLength = token.length();
282-
283-
for (int i = 1; i < tokenLength; i++)
284+
for (int i = 1; i < token.length(); i++)
284285
{
285286
String ch = String.valueOf(token.charAt(i));
286-
boolean hasOption = options.hasOption(ch);
287287

288-
if (hasOption)
288+
if (options.hasOption(ch))
289289
{
290290
tokens.add("-" + ch);
291291
currentOption = options.getOption(ch);
@@ -300,6 +300,7 @@ protected void burstToken(String token, boolean stopAtNonOption)
300300
else if (stopAtNonOption)
301301
{
302302
process(token.substring(i));
303+
break;
303304
}
304305
else
305306
{

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

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

133+
public void testStopBursting() throws Exception
134+
{
135+
String[] args = new String[] { "-azc" };
136+
137+
CommandLine cl = parser.parse(options, args, true);
138+
assertTrue( "Confirm -a is set", cl.hasOption("a") );
139+
assertFalse( "Confirm -c is not set", cl.hasOption("c") );
140+
141+
assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
142+
assertTrue(cl.getArgList().contains("zc"));
143+
}
144+
133145
public void testMultiple() throws Exception
134146
{
135147
String[] args = new String[] { "-c",
@@ -155,8 +167,7 @@ public void testMultipleWithLong() throws Exception
155167
"foobar",
156168
"--bfile", "toast" };
157169

158-
CommandLine cl = parser.parse(options,args,
159-
true);
170+
CommandLine cl = parser.parse(options,args, true);
160171
assertTrue( "Confirm -c is set", cl.hasOption("c") );
161172
assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
162173

0 commit comments

Comments
 (0)