Skip to content

Commit fc0723b

Browse files
author
John Keyes
committed
bug #12210 fixed
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129808 13f79535-47bb-0310-9956-ffa450edef68
1 parent e618635 commit fc0723b

3 files changed

Lines changed: 140 additions & 16 deletions

File tree

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

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/GnuParser.java,v 1.8 2002/08/26 20:15:02 jkeyes Exp $
3-
* $Revision: 1.8 $
4-
* $Date: 2002/08/26 20:15:02 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/GnuParser.java,v 1.9 2002/08/31 17:53:11 jkeyes Exp $
3+
* $Revision: 1.9 $
4+
* $Date: 2002/08/31 17:53:11 $
55
*
66
* ====================================================================
77
*
@@ -64,16 +64,14 @@
6464
import java.util.ArrayList;
6565
import java.util.Collection;
6666
import java.util.Iterator;
67-
import java.util.Map;
68-
import java.util.Iterator;
6967

7068
/**
7169
* The class GnuParser provides an implementation of the
7270
* {@link Parser#flatten(Options,String[],boolean) flatten} method.
7371
*
7472
* @author John Keyes (jbjk at mac.com)
7573
* @see Parser
76-
* @version $Revision: 1.8 $
74+
* @version $Revision: 1.9 $
7775
*/
7876
public class GnuParser extends Parser {
7977

@@ -106,21 +104,83 @@ protected String[] flatten( Options options,
106104
boolean stopAtNonOption )
107105
{
108106
init();
107+
boolean eatTheRest = false;
108+
Option currentOption = null;
109+
109110
for( int i = 0; i < arguments.length; i++ ) {
110-
Option option = options.getOption( arguments[i] );
111-
try {
112-
Option specialOption = options.getOption( arguments[i].substring(0,2) );
113-
if( specialOption != null && option == null ) {
114-
tokens.add( arguments[i].substring(0,2) );
115-
tokens.add( arguments[i].substring(2) );
111+
if( "--".equals( arguments[i] ) ) {
112+
eatTheRest = true;
113+
tokens.add( "--" );
114+
}
115+
else if ( "-".equals( arguments[i] ) ) {
116+
tokens.add( "-" );
117+
}
118+
else if( arguments[i].startsWith( "-" ) ) {
119+
Option option = options.getOption( arguments[i] );
120+
121+
// this is not an Option
122+
if( option == null ) {
123+
// handle special properties Option
124+
Option specialOption = options.getOption( arguments[i].substring(0,2) );
125+
if( specialOption != null ) {
126+
tokens.add( arguments[i].substring(0,2) );
127+
tokens.add( arguments[i].substring(2) );
128+
}
129+
else if( stopAtNonOption ) {
130+
eatTheRest = true;
131+
tokens.add( arguments[i] );
132+
}
133+
else {
134+
tokens.add( arguments[i] );
135+
}
116136
}
117137
else {
118-
tokens.add( arguments[i] );
138+
currentOption = option;
139+
// special option
140+
Option specialOption = options.getOption( arguments[i].substring(0,2) );
141+
if( specialOption != null && option == null ) {
142+
tokens.add( arguments[i].substring(0,2) );
143+
tokens.add( arguments[i].substring(2) );
144+
}
145+
else if( currentOption != null && currentOption.hasArg() ) {
146+
if( currentOption.hasArg() ) {
147+
tokens.add( arguments[i] );
148+
currentOption= null;
149+
}
150+
else if ( currentOption.hasArgs() ) {
151+
tokens.add( arguments[i] );
152+
}
153+
else if ( stopAtNonOption ) {
154+
eatTheRest = true;
155+
tokens.add( "--" );
156+
tokens.add( arguments[i] );
157+
}
158+
else {
159+
tokens.add( arguments[i] );
160+
}
161+
}
162+
else if (currentOption != null ) {
163+
tokens.add( arguments[i] );
164+
}
165+
else if ( stopAtNonOption ) {
166+
eatTheRest = true;
167+
tokens.add( "--" );
168+
tokens.add( arguments[i] );
169+
}
170+
else {
171+
tokens.add( arguments[i] );
172+
}
119173
}
120174
}
121-
catch( IndexOutOfBoundsException exp ) {
175+
else {
122176
tokens.add( arguments[i] );
123177
}
178+
179+
if( eatTheRest ) {
180+
for( i++; i < arguments.length; i++ ) {
181+
tokens.add( arguments[i] );
182+
}
183+
}
124184
}
125185
return (String[])tokens.toArray( new String[] {} );
126186
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ else if( t.startsWith( "-" ) ) {
4949
cmd.addArg( t );
5050
}
5151
}
52+
else if ( stopAtNonOption && !options.hasOption( t ) ) {
53+
eatTheRest = true;
54+
cmd.addArg( t );
55+
}
5256
else {
5357
processOption( t, iterator );
5458
}
@@ -62,7 +66,10 @@ else if( t.startsWith( "-" ) ) {
6266

6367
if( eatTheRest ) {
6468
while( iterator.hasNext() ) {
65-
cmd.addArg( (String)iterator.next() );
69+
String str = (String)iterator.next();
70+
if( !"--".equals( str ) ) {
71+
cmd.addArg( str );
72+
}
6673
}
6774
}
6875
}

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* version 1.1, a copy of which has been included with this distribution in
66
* the LICENSE file.
77
*
8-
* $Id: BugsTest.java,v 1.4 2002/08/18 19:07:42 jkeyes Exp $
8+
* $Id: BugsTest.java,v 1.5 2002/08/31 17:53:11 jkeyes Exp $
99
*/
1010

1111
package org.apache.commons.cli;
@@ -172,4 +172,61 @@ public void test11456()
172172

173173
}
174174

175+
public void test12210() {
176+
// create the main options object which will handle the first parameter
177+
Options mainOptions = new Options();
178+
// There can be 2 main exclusive options: -exec|-rep
179+
180+
// Therefore, place them in an option group
181+
182+
String[] argv = new String[] { "-exec", "-execopto", "-execoptt" };
183+
OptionGroup grp = new OptionGroup();
184+
185+
grp.addOption(new Option("exec",false,"description for this option"));
186+
187+
grp.addOption(new Option("rep",false,"description for this option"));
188+
189+
mainOptions.addOptionGroup(grp);
190+
191+
// for the exec option, there are 2 options...
192+
Options execOptions = new Options();
193+
execOptions.addOption("execopto",false," desc");
194+
execOptions.addOption("execoptt",false," desc");
195+
196+
// similarly, for rep there are 2 options...
197+
Options repOptions = new Options();
198+
repOptions.addOption("repopto",false,"desc");
199+
repOptions.addOption("repoptt",false,"desc");
200+
201+
// create the parser
202+
GnuParser parser = new GnuParser();
203+
204+
// finally, parse the arguments:
205+
206+
// first parse the main options to see what the user has specified
207+
// We set stopAtNonOption to true so it does not touch the remaining
208+
// options
209+
try {
210+
CommandLine cmd = parser.parse(mainOptions,argv,true);
211+
// get the remaining options...
212+
argv = cmd.getArgs();
213+
214+
if(cmd.hasOption("exec")){
215+
cmd = parser.parse(execOptions,argv,false);
216+
// process the exec_op1 and exec_opt2...
217+
assertTrue( cmd.hasOption("execopto") );
218+
assertTrue( cmd.hasOption("execoptt") );
219+
}
220+
else if(cmd.hasOption("rep")){
221+
cmd = parser.parse(repOptions,argv,false);
222+
// process the rep_op1 and rep_opt2...
223+
}
224+
else {
225+
fail( "exec option not found" );
226+
}
227+
}
228+
catch( ParseException exp ) {
229+
fail( "Unexpected exception: " + exp.getMessage() );
230+
}
231+
}
175232
}

0 commit comments

Comments
 (0)