Skip to content

Commit 8d450c9

Browse files
committed
Changed AlreadySelectedException to include the related option group and the option that triggered the exception
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@679606 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0a8de54 commit 8d450c9

3 files changed

Lines changed: 67 additions & 27 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
*/
2727
public class AlreadySelectedException extends ParseException
2828
{
29+
/** The option group selected. */
30+
private OptionGroup group;
31+
32+
/** The option that triggered the exception. */
33+
private Option option;
34+
2935
/**
3036
* Construct a new <code>AlreadySelectedException</code>
3137
* with the specified detail message.
@@ -36,4 +42,40 @@ public AlreadySelectedException(String message)
3642
{
3743
super(message);
3844
}
45+
46+
/**
47+
* Construct a new <code>AlreadySelectedException</code>
48+
* for the specified option group.
49+
*
50+
* @param group the option group already selected
51+
* @param option the option that triggered the exception
52+
* @since 1.2
53+
*/
54+
public AlreadySelectedException(OptionGroup group, Option option)
55+
{
56+
this("The option '" + option.getKey() + "' was specified but an option from this group " +
57+
"has already been selected: '" + group.getSelected() + "'");
58+
this.group = group;
59+
this.option = option;
60+
}
61+
62+
/**
63+
* Returns the option group where another option has been selected.
64+
*
65+
* @since 1.2
66+
*/
67+
public OptionGroup getOptionGroup()
68+
{
69+
return group;
70+
}
71+
72+
/**
73+
* Returns the option that was added to the group and triggered the exception.
74+
*
75+
* @since 1.2
76+
*/
77+
public Option getOption()
78+
{
79+
return option;
80+
}
3981
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
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.io.Serializable;
2021
import java.util.Collection;
2122
import java.util.HashMap;
2223
import java.util.Iterator;
24+
import java.util.Map;
2325

2426
/**
2527
* A group of mutually exclusive options.
28+
*
2629
* @author John Keyes ( john at integralsource.com )
2730
* @version $Revision$
2831
*/
@@ -31,7 +34,7 @@ public class OptionGroup implements Serializable {
3134
private static final long serialVersionUID = 1L;
3235

3336
/** hold the options */
34-
private HashMap optionMap = new HashMap();
37+
private Map optionMap = new HashMap();
3538

3639
/** the name of the selected option */
3740
private String selected;
@@ -40,16 +43,16 @@ public class OptionGroup implements Serializable {
4043
private boolean required;
4144

4245
/**
43-
* add <code>opt</code> to this group
46+
* Add the specified <code>Option</code> to this group.
4447
*
45-
* @param opt the option to add to this group
46-
* @return this option group with opt added
48+
* @param option the option to add to this group
49+
* @return this option group with the option added
4750
*/
48-
public OptionGroup addOption(Option opt)
51+
public OptionGroup addOption(Option option)
4952
{
5053
// key - option name
5154
// value - the option
52-
optionMap.put(opt.getKey(), opt);
55+
optionMap.put(option.getKey(), option);
5356

5457
return this;
5558
}
@@ -75,25 +78,22 @@ public Collection getOptions()
7578

7679
/**
7780
* set the selected option of this group to <code>name</code>.
78-
* @param opt the option that is selected
81+
* @param option the option that is selected
7982
* @throws AlreadySelectedException if an option from this group has
8083
* already been selected.
8184
*/
82-
public void setSelected(Option opt)
83-
throws AlreadySelectedException
85+
public void setSelected(Option option) throws AlreadySelectedException
8486
{
8587
// if no option has already been selected or the
8688
// same option is being reselected then set the
8789
// selected member variable
88-
if ((this.selected == null) || this.selected.equals(opt.getOpt()))
90+
if ((this.selected == null) || this.selected.equals(option.getOpt()))
8991
{
90-
this.selected = opt.getOpt();
92+
this.selected = option.getOpt();
9193
}
9294
else
9395
{
94-
throw new AlreadySelectedException("an option from this group has "
95-
+ "already been selected: '"
96-
+ selected + "'");
96+
throw new AlreadySelectedException(this, option);
9797
}
9898
}
9999

@@ -163,4 +163,4 @@ public String toString()
163163

164164
return buff.toString();
165165
}
166-
}
166+
}

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void testNoOptionsExtraArgs() throws Exception
139139
assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2);
140140
}
141141

142-
public void testTwoOptionsFromGroup()
142+
public void testTwoOptionsFromGroup() throws Exception
143143
{
144144
String[] args = new String[] { "-f", "-d" };
145145

@@ -148,16 +148,15 @@ public void testTwoOptionsFromGroup()
148148
parser.parse( _options, args);
149149
fail( "two arguments from group not allowed" );
150150
}
151-
catch (ParseException e)
151+
catch (AlreadySelectedException e)
152152
{
153-
if( !( e instanceof AlreadySelectedException ) )
154-
{
155-
fail( "incorrect exception caught:" + e.getMessage() );
156-
}
153+
assertNotNull("null option group", e.getOptionGroup());
154+
assertEquals("selected option", "f", e.getOptionGroup().getSelected());
155+
assertEquals("option", "d", e.getOption().getOpt());
157156
}
158157
}
159158

160-
public void testTwoLongOptionsFromGroup()
159+
public void testTwoLongOptionsFromGroup() throws Exception
161160
{
162161
String[] args = new String[] { "--file", "--directory" };
163162

@@ -166,12 +165,11 @@ public void testTwoLongOptionsFromGroup()
166165
parser.parse(_options, args);
167166
fail( "two arguments from group not allowed" );
168167
}
169-
catch (ParseException e)
168+
catch (AlreadySelectedException e)
170169
{
171-
if( !( e instanceof AlreadySelectedException ) )
172-
{
173-
fail( "incorrect exception caught:" + e.getMessage() );
174-
}
170+
assertNotNull("null option group", e.getOptionGroup());
171+
assertEquals("selected option", "f", e.getOptionGroup().getSelected());
172+
assertEquals("option", "d", e.getOption().getOpt());
175173
}
176174
}
177175

0 commit comments

Comments
 (0)