Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 11339 invoked from network); 24 Jul 2008 22:56:59 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 24 Jul 2008 22:56:59 -0000 Received: (qmail 39390 invoked by uid 500); 24 Jul 2008 22:56:58 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 39372 invoked by uid 500); 24 Jul 2008 22:56:58 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 39363 invoked by uid 99); 24 Jul 2008 22:56:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Jul 2008 15:56:58 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Jul 2008 22:56:03 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 568E92388892; Thu, 24 Jul 2008 15:55:59 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r679606 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/AlreadySelectedException.java java/org/apache/commons/cli/OptionGroup.java test/org/apache/commons/cli/OptionGroupTest.java Date: Thu, 24 Jul 2008 22:55:59 -0000 To: commits@commons.apache.org From: ebourg@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080724225559.568E92388892@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ebourg Date: Thu Jul 24 15:55:58 2008 New Revision: 679606 URL: http://svn.apache.org/viewvc?rev=679606&view=rev Log: Changed AlreadySelectedException to include the related option group and the option that triggered the exception Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java?rev=679606&r1=679605&r2=679606&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java (original) +++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java Thu Jul 24 15:55:58 2008 @@ -26,6 +26,12 @@ */ public class AlreadySelectedException extends ParseException { + /** The option group selected. */ + private OptionGroup group; + + /** The option that triggered the exception. */ + private Option option; + /** * Construct a new AlreadySelectedException * with the specified detail message. @@ -36,4 +42,40 @@ { super(message); } + + /** + * Construct a new AlreadySelectedException + * for the specified option group. + * + * @param group the option group already selected + * @param option the option that triggered the exception + * @since 1.2 + */ + public AlreadySelectedException(OptionGroup group, Option option) + { + this("The option '" + option.getKey() + "' was specified but an option from this group " + + "has already been selected: '" + group.getSelected() + "'"); + this.group = group; + this.option = option; + } + + /** + * Returns the option group where another option has been selected. + * + * @since 1.2 + */ + public OptionGroup getOptionGroup() + { + return group; + } + + /** + * Returns the option that was added to the group and triggered the exception. + * + * @since 1.2 + */ + public Option getOption() + { + return option; + } } Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java?rev=679606&r1=679605&r2=679606&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java (original) +++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java Thu Jul 24 15:55:58 2008 @@ -14,15 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.cli; import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; +import java.util.Map; /** * A group of mutually exclusive options. + * * @author John Keyes ( john at integralsource.com ) * @version $Revision$ */ @@ -31,7 +34,7 @@ private static final long serialVersionUID = 1L; /** hold the options */ - private HashMap optionMap = new HashMap(); + private Map optionMap = new HashMap(); /** the name of the selected option */ private String selected; @@ -40,16 +43,16 @@ private boolean required; /** - * add opt to this group + * Add the specified Option to this group. * - * @param opt the option to add to this group - * @return this option group with opt added + * @param option the option to add to this group + * @return this option group with the option added */ - public OptionGroup addOption(Option opt) + public OptionGroup addOption(Option option) { // key - option name // value - the option - optionMap.put(opt.getKey(), opt); + optionMap.put(option.getKey(), option); return this; } @@ -75,25 +78,22 @@ /** * set the selected option of this group to name. - * @param opt the option that is selected + * @param option the option that is selected * @throws AlreadySelectedException if an option from this group has * already been selected. */ - public void setSelected(Option opt) - throws AlreadySelectedException + public void setSelected(Option option) throws AlreadySelectedException { // if no option has already been selected or the // same option is being reselected then set the // selected member variable - if ((this.selected == null) || this.selected.equals(opt.getOpt())) + if ((this.selected == null) || this.selected.equals(option.getOpt())) { - this.selected = opt.getOpt(); + this.selected = option.getOpt(); } else { - throw new AlreadySelectedException("an option from this group has " - + "already been selected: '" - + selected + "'"); + throw new AlreadySelectedException(this, option); } } @@ -163,4 +163,4 @@ return buff.toString(); } -} \ No newline at end of file +} Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java?rev=679606&r1=679605&r2=679606&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java (original) +++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java Thu Jul 24 15:55:58 2008 @@ -139,7 +139,7 @@ assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2); } - public void testTwoOptionsFromGroup() + public void testTwoOptionsFromGroup() throws Exception { String[] args = new String[] { "-f", "-d" }; @@ -148,16 +148,15 @@ parser.parse( _options, args); fail( "two arguments from group not allowed" ); } - catch (ParseException e) + catch (AlreadySelectedException e) { - if( !( e instanceof AlreadySelectedException ) ) - { - fail( "incorrect exception caught:" + e.getMessage() ); - } + assertNotNull("null option group", e.getOptionGroup()); + assertEquals("selected option", "f", e.getOptionGroup().getSelected()); + assertEquals("option", "d", e.getOption().getOpt()); } } - public void testTwoLongOptionsFromGroup() + public void testTwoLongOptionsFromGroup() throws Exception { String[] args = new String[] { "--file", "--directory" }; @@ -166,12 +165,11 @@ parser.parse(_options, args); fail( "two arguments from group not allowed" ); } - catch (ParseException e) + catch (AlreadySelectedException e) { - if( !( e instanceof AlreadySelectedException ) ) - { - fail( "incorrect exception caught:" + e.getMessage() ); - } + assertNotNull("null option group", e.getOptionGroup()); + assertEquals("selected option", "f", e.getOptionGroup().getSelected()); + assertEquals("option", "d", e.getOption().getOpt()); } }