Skip to content

Commit 436ef8b

Browse files
author
John Keyes
committed
fix pr 13935 - still need to improve the exception message
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129827 13f79535-47bb-0310-9956-ffa450edef68
1 parent db90678 commit 436ef8b

4 files changed

Lines changed: 102 additions & 6 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public class OptionGroup {
7878
/** the name of the selected option */
7979
private String selected;
8080

81+
/** specified whether this group is required */
82+
private boolean required;
83+
8184
/**
8285
* add <code>opt</code> to this group
8386
*
@@ -136,6 +139,22 @@ public String getSelected() {
136139
return selected;
137140
}
138141

142+
/**
143+
* @param required specifies if this group is required
144+
*/
145+
public void setRequired( boolean required ) {
146+
this.required = required;
147+
}
148+
149+
/**
150+
* Returns whether this option group is required.
151+
*
152+
* @returns whether this option group is required
153+
*/
154+
public boolean isRequired() {
155+
return this.required;
156+
}
157+
139158
/**
140159
* <p>Returns the stringified version of this OptionGroup.</p>
141160
* @return the stringified representation of this group

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,16 @@ public Options() {
113113
public Options addOptionGroup( OptionGroup group ) {
114114
Iterator options = group.getOptions().iterator();
115115

116+
if( group.isRequired() ) {
117+
requiredOpts.add( group );
118+
}
119+
116120
while( options.hasNext() ) {
117121
Option option = (Option)options.next();
122+
// an Option cannot be required if it is in an
123+
// OptionGroup, either the group is required or
124+
// nothing is required
125+
option.setRequired( false );
118126
addOption( option );
119127

120128
optionGroups.put( option.getOpt(), group );

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

Lines changed: 9 additions & 5 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/Parser.java,v 1.6 2002/10/08 21:24:11 jkeyes Exp $
3-
* $Revision: 1.6 $
4-
* $Date: 2002/10/08 21:24:11 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.7 2002/10/24 23:17:49 jkeyes Exp $
3+
* $Revision: 1.7 $
4+
* $Date: 2002/10/24 23:17:49 $
55
*
66
* ====================================================================
77
*
@@ -72,7 +72,7 @@
7272
*
7373
* @author John Keyes (john at integralsource.com)
7474
* @see Parser
75-
* @version $Revision: 1.6 $
75+
* @version $Revision: 1.7 $
7676
*/
7777
public abstract class Parser implements CommandLineParser {
7878

@@ -265,7 +265,11 @@ private void processOption( String arg, ListIterator iter )
265265
// if the option is in an OptionGroup make that option the selected
266266
// option of the group
267267
if ( options.getOptionGroup( opt ) != null ) {
268-
( (OptionGroup)( options.getOptionGroup( opt ) ) ).setSelected( opt );
268+
OptionGroup group = ( OptionGroup ) options.getOptionGroup( opt );
269+
if( group.isRequired() ) {
270+
requiredOptions.remove( group );
271+
}
272+
group.setSelected( opt );
269273
}
270274

271275
// if the option takes an argument value

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

Lines changed: 66 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.9 2002/10/19 21:18:26 jkeyes Exp $
8+
* $Id: BugsTest.java,v 1.10 2002/10/24 23:17:49 jkeyes Exp $
99
*/
1010

1111
package org.apache.commons.cli;
@@ -278,4 +278,69 @@ public void test13666() {
278278
}
279279
}
280280

281+
public void test13935() {
282+
OptionGroup directions = new OptionGroup();
283+
284+
Option left = new Option( "l", "left", false, "go left" );
285+
Option right = new Option( "r", "right", false, "go right" );
286+
Option straight = new Option( "s", "straight", false, "go straight" );
287+
Option forward = new Option( "f", "forward", false, "go forward" );
288+
forward.setRequired( true );
289+
290+
directions.addOption( left );
291+
directions.addOption( right );
292+
directions.setRequired( true );
293+
294+
Options opts = new Options();
295+
opts.addOptionGroup( directions );
296+
opts.addOption( straight );
297+
298+
CommandLineParser parser = new PosixParser();
299+
boolean exception = false;
300+
301+
String[] args = new String[] { };
302+
try {
303+
CommandLine line = parser.parse( opts, args );
304+
}
305+
catch( ParseException exp ) {
306+
exception = true;
307+
}
308+
309+
if( !exception ) {
310+
fail( "Expected exception not caught.");
311+
}
312+
313+
exception = false;
314+
315+
args = new String[] { "-s" };
316+
try {
317+
CommandLine line = parser.parse( opts, args );
318+
}
319+
catch( ParseException exp ) {
320+
exception = true;
321+
}
322+
323+
if( !exception ) {
324+
fail( "Expected exception not caught.");
325+
}
326+
327+
exception = false;
328+
329+
args = new String[] { "-s", "-l" };
330+
try {
331+
CommandLine line = parser.parse( opts, args );
332+
}
333+
catch( ParseException exp ) {
334+
fail( "Unexpected exception: " + exp.getMessage() );
335+
}
336+
337+
opts.addOption( forward );
338+
args = new String[] { "-s", "-l", "-f" };
339+
try {
340+
CommandLine line = parser.parse( opts, args );
341+
}
342+
catch( ParseException exp ) {
343+
fail( "Unexpected exception: " + exp.getMessage() );
344+
}
345+
}
281346
}

0 commit comments

Comments
 (0)