Skip to content

Commit 94dff20

Browse files
author
Robert James Oxspring
committed
Added checks and documentation to ensure preferredName and any aliases begin with a specified prefix
Bug: 30979 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@130091 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7fb448f commit 94dff20

6 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/java/org/apache/commons/cli2/builder/DefaultOptionBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ public void reset() {
128128
}
129129

130130
/**
131-
* Use this short option name
131+
* Use this short option name. The first name is used as the preferred
132+
* display name for the Command and then later names are used as aliases.
133+
*
132134
* @param shortName the name to use
133135
* @return this builder
134136
*/
@@ -150,7 +152,9 @@ public DefaultOptionBuilder withShortName(final String shortName) {
150152
}
151153

152154
/**
153-
* Use this long option name
155+
* Use this long option name. The first name is used as the preferred
156+
* display name for the Command and then later names are used as aliases.
157+
*
154158
* @param longName the name to use
155159
* @return this builder
156160
*/

src/java/org/apache/commons/cli2/builder/SwitchBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public SwitchBuilder withDescription(final String newDescription) {
120120
}
121121

122122
/**
123-
* Use this option name
123+
* Use this option name. The first name is used as the preferred
124+
* display name for the Command and then later names are used as aliases.
125+
*
124126
* @param name the name to use
125127
* @return this builder
126128
*/

src/java/org/apache/commons/cli2/option/DefaultOption.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ public class DefaultOption extends ParentImpl {
6767
* @param shortPrefix the prefix used for short options
6868
* @param longPrefix the prefix used for long options
6969
* @param burstEnabled should option bursting be enabled
70-
* @param preferredName the preferred name for this Option
70+
* @param preferredName the preferred name for this Option, this should begin with either shortPrefix or longPrefix
7171
* @param description a description of this Option
7272
* @param aliases the alternative names for this Option
7373
* @param burstAliases the aliases that can be burst
7474
* @param required whether the Option is strictly required
7575
* @param argument the Argument belonging to this Parent, or null
7676
* @param children the Group children belonging to this Parent, ot null
7777
* @param id the unique identifier for this Option
78+
* @throws IllegalArgumentException if the preferredName or an alias isn't
79+
* prefixed with shortPrefix or longPrefix
7880
*/
7981
public DefaultOption(
8082
final String shortPrefix,
@@ -121,6 +123,8 @@ public DefaultOption(
121123
newPrefixes.add(shortPrefix);
122124
newPrefixes.add(longPrefix);
123125
this.prefixes = Collections.unmodifiableSet(newPrefixes);
126+
127+
checkPrefixes(newPrefixes);
124128
}
125129

126130
public boolean canProcess(final String argument) {

src/java/org/apache/commons/cli2/option/OptionImpl.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
*/
1616
package org.apache.commons.cli2.option;
1717

18+
import java.util.Iterator;
1819
import java.util.ListIterator;
20+
import java.util.Set;
1921

2022
import org.apache.commons.cli2.DisplaySetting;
2123
import org.apache.commons.cli2.Option;
2224
import org.apache.commons.cli2.WriteableCommandLine;
25+
import org.apache.commons.cli2.resource.ResourceHelper;
2326

2427
/**
2528
* A base implementation of Option providing limited ground work for further
@@ -104,4 +107,39 @@ public boolean isRequired() {
104107
public void defaults(final WriteableCommandLine commandLine) {
105108
// nothing to do normally
106109
}
110+
111+
protected void checkPrefixes(final Set prefixes) {
112+
113+
// nothing to do if empty prefix list
114+
if(prefixes.isEmpty()) {
115+
return;
116+
}
117+
118+
// check preferred name
119+
checkPrefix(prefixes, getPreferredName());
120+
121+
// check triggers
122+
this.getTriggers();
123+
for (final Iterator i = getTriggers().iterator(); i.hasNext();) {
124+
checkPrefix(prefixes, (String) i.next());
125+
}
126+
}
127+
128+
private void checkPrefix(final Set prefixes, final String trigger) {
129+
for (final Iterator i = prefixes.iterator(); i.hasNext();) {
130+
String prefix = (String) i.next();
131+
if(trigger.startsWith(prefix)) {
132+
return;
133+
}
134+
}
135+
136+
final ResourceHelper helper =
137+
ResourceHelper.getResourceHelper(OptionImpl.class);
138+
final String message =
139+
helper.getMessage("cli.error.trigger.needs.prefix",
140+
trigger,prefixes.toString());
141+
throw new IllegalArgumentException(message);
142+
}
143+
144+
107145
}

src/java/org/apache/commons/cli2/option/Switch.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class Switch extends ParentImpl {
6565
* @param argument the Argument belonging to this Parent, or null
6666
* @param children the Group children belonging to this Parent, ot null
6767
* @param id the unique identifier for this Option
68+
* @throws IllegalArgumentException if the preferredName or an alias isn't
69+
* prefixed with enabledPrefix or disabledPrefix
6870
*/
6971
public Switch(
7072
final String enabledPrefix,
@@ -126,6 +128,8 @@ public Switch(
126128
this.prefixes = Collections.unmodifiableSet(newPrefixes);
127129

128130
this.defaultSwitch = switchDefault;
131+
132+
checkPrefixes(newPrefixes);
129133
}
130134

131135
public void processParent(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
cli.error.minimum.exceeds.maximum = Minimum number of values must not exceed maximum number
22
cli.error.too.few.defaults = Not enough default values.
33
cli.error.too.many.defaults = Too many default values.
4+
cli.error.trigger.needs.prefix = Trigger {0} must be prefixed with a value from {1}
5+
cli.error.badproperty = Could not understand property: {0}

0 commit comments

Comments
 (0)