jkeyes 2002/10/24 16:17:49 Modified: cli/src/java/org/apache/commons/cli Parser.java OptionGroup.java Options.java cli/src/test/org/apache/commons/cli BugsTest.java Log: fix pr 13935 - still need to improve the exception message Revision Changes Path 1.7 +9 -5 jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java Index: Parser.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Parser.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- Parser.java 8 Oct 2002 21:24:11 -0000 1.6 +++ Parser.java 24 Oct 2002 23:17:49 -0000 1.7 @@ -265,7 +265,11 @@ // if the option is in an OptionGroup make that option the selected // option of the group if ( options.getOptionGroup( opt ) != null ) { - ( (OptionGroup)( options.getOptionGroup( opt ) ) ).setSelected( opt ); + OptionGroup group = ( OptionGroup ) options.getOptionGroup( opt ); + if( group.isRequired() ) { + requiredOptions.remove( group ); + } + group.setSelected( opt ); } // if the option takes an argument value 1.6 +19 -0 jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java Index: OptionGroup.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionGroup.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- OptionGroup.java 19 Sep 2002 22:59:43 -0000 1.5 +++ OptionGroup.java 24 Oct 2002 23:17:49 -0000 1.6 @@ -78,6 +78,9 @@ /** the name of the selected option */ private String selected; + /** specified whether this group is required */ + private boolean required; + /** * add <code>opt</code> to this group * @@ -134,6 +137,22 @@ */ public String getSelected() { return selected; + } + + /** + * @param required specifies if this group is required + */ + public void setRequired( boolean required ) { + this.required = required; + } + + /** + * Returns whether this option group is required. + * + * @returns whether this option group is required + */ + public boolean isRequired() { + return this.required; } /** 1.14 +8 -0 jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java Index: Options.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Options.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- Options.java 8 Oct 2002 21:24:11 -0000 1.13 +++ Options.java 24 Oct 2002 23:17:49 -0000 1.14 @@ -113,8 +113,16 @@ public Options addOptionGroup( OptionGroup group ) { Iterator options = group.getOptions().iterator(); + if( group.isRequired() ) { + requiredOpts.add( group ); + } + while( options.hasNext() ) { Option option = (Option)options.next(); + // an Option cannot be required if it is in an + // OptionGroup, either the group is required or + // nothing is required + option.setRequired( false ); addOption( option ); optionGroups.put( option.getOpt(), group ); 1.10 +66 -1 jakarta-commons/cli/src/test/org/apache/commons/cli/BugsTest.java Index: BugsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/BugsTest.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- BugsTest.java 19 Oct 2002 21:18:26 -0000 1.9 +++ BugsTest.java 24 Oct 2002 23:17:49 -0000 1.10 @@ -278,4 +278,69 @@ } } + public void test13935() { + OptionGroup directions = new OptionGroup(); + + Option left = new Option( "l", "left", false, "go left" ); + Option right = new Option( "r", "right", false, "go right" ); + Option straight = new Option( "s", "straight", false, "go straight" ); + Option forward = new Option( "f", "forward", false, "go forward" ); + forward.setRequired( true ); + + directions.addOption( left ); + directions.addOption( right ); + directions.setRequired( true ); + + Options opts = new Options(); + opts.addOptionGroup( directions ); + opts.addOption( straight ); + + CommandLineParser parser = new PosixParser(); + boolean exception = false; + + String[] args = new String[] { }; + try { + CommandLine line = parser.parse( opts, args ); + } + catch( ParseException exp ) { + exception = true; + } + + if( !exception ) { + fail( "Expected exception not caught."); + } + + exception = false; + + args = new String[] { "-s" }; + try { + CommandLine line = parser.parse( opts, args ); + } + catch( ParseException exp ) { + exception = true; + } + + if( !exception ) { + fail( "Expected exception not caught."); + } + + exception = false; + + args = new String[] { "-s", "-l" }; + try { + CommandLine line = parser.parse( opts, args ); + } + catch( ParseException exp ) { + fail( "Unexpected exception: " + exp.getMessage() ); + } + + opts.addOption( forward ); + args = new String[] { "-s", "-l", "-f" }; + try { + CommandLine line = parser.parse( opts, args ); + } + catch( ParseException exp ) { + fail( "Unexpected exception: " + exp.getMessage() ); + } + } }
-- To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>