Skip to content

Commit 2391d47

Browse files
author
Oliver Heger
committed
CLI-126: Applied patch from Brian Egge
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@678886 13f79535-47bb-0310-9956-ffa450edef68
1 parent f989927 commit 2391d47

14 files changed

Lines changed: 165 additions & 47 deletions

src/java/org/apache/commons/cli2/CommandLine.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public interface CommandLine {
158158

159159

160160
/**
161-
* Retrieves the value associated with the specified property
161+
* Retrieves the value associated with the specified property for the default property set
162162
*
163163
* @param property the property name to lookup
164164
* @return the value of the property or null
@@ -168,14 +168,32 @@ public interface CommandLine {
168168
/**
169169
* Retrieves the value associated with the specified property
170170
*
171+
* @param option the option i.e., -D
172+
* @param property the property name to lookup
173+
* @return the value of the property or null
174+
*/
175+
String getProperty(final Option option, final String property);
176+
177+
/**
178+
* Retrieves the value associated with the specified property
179+
*
180+
* @param option the option i.e., -D
171181
* @param property the property name to lookup
172182
* @param defaultValue the value to use if no other is found
173183
* @return the value of the property or defaultValue
174184
*/
175-
String getProperty(final String property, final String defaultValue);
185+
String getProperty(final Option option, final String property, final String defaultValue);
186+
187+
/**
188+
* Retrieves the set of all property names associated with this option
189+
*
190+
* @param option the option i.e., -D
191+
* @return a none null set of property names
192+
*/
193+
Set getProperties(final Option option);
176194

177195
/**
178-
* Retrieves the set of all property names associated with this CommandLine
196+
* Retrieves the set of all property names associated with the default property option
179197
*
180198
* @return a none null set of property names
181199
*/

src/java/org/apache/commons/cli2/WriteableCommandLine.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public interface WriteableCommandLine extends CommandLine {
7272
* Adds a property value to a name in the CommandLine.
7373
* Replaces any existing value for the property.
7474
*
75+
* @param option the Option to add to
76+
* @param property the name of the property
77+
* @param value the value of the property
78+
*/
79+
void addProperty(final Option option, final String property, final String value);
80+
81+
/**
82+
* Adds a property value to the default property set.
83+
* Replaces any existing value for the property.
84+
*
7585
* @param property the name of the property
7686
* @param value the value of the property
7787
*/

src/java/org/apache/commons/cli2/commandline/CommandLineImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public final Boolean getSwitch(final Option option) {
9494
return getSwitch(option, null);
9595
}
9696

97-
public final String getProperty(final String property) {
98-
return getProperty(property, null);
97+
public final String getProperty(final Option option, final String property) {
98+
return getProperty(option, property, null);
9999
}
100100

101101
public final int getOptionCount(final String trigger) {

src/java/org/apache/commons/cli2/commandline/DefaultingCommandLine.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.commons.cli2.CommandLine;
2727
import org.apache.commons.cli2.Option;
28+
import org.apache.commons.cli2.option.PropertyOption;
2829

2930
/**
3031
* Manages a queue of default CommandLines. This CommandLine implementation is
@@ -150,23 +151,31 @@ public Boolean getSwitch(Option option, Boolean defaultValue) {
150151
return defaultValue;
151152
}
152153

153-
public String getProperty(String property, String defaultValue) {
154+
public String getProperty(final String property) {
155+
return getProperty(new PropertyOption(), property);
156+
}
157+
158+
public String getProperty(final Option option, String property, String defaultValue) {
154159
for (final Iterator i = commandLines.iterator(); i.hasNext();) {
155160
final CommandLine commandLine = (CommandLine)i.next();
156-
final String actual = commandLine.getProperty(property);
161+
final String actual = commandLine.getProperty(option, property);
157162
if (actual != null) {
158163
return actual;
159164
}
160165
}
161166
return defaultValue;
162167
}
163168

164-
public Set getProperties() {
169+
public Set getProperties(final Option option) {
165170
final Set all = new HashSet();
166171
for (final Iterator i = commandLines.iterator(); i.hasNext();) {
167172
final CommandLine commandLine = (CommandLine)i.next();
168-
all.addAll(commandLine.getProperties());
173+
all.addAll(commandLine.getProperties(option));
169174
}
170175
return Collections.unmodifiableSet(all);
171176
}
177+
178+
public Set getProperties() {
179+
return getProperties(new PropertyOption());
180+
}
172181
}

src/java/org/apache/commons/cli2/commandline/PreferencesCommandLine.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.prefs.Preferences;
2929

3030
import org.apache.commons.cli2.Option;
31+
import org.apache.commons.cli2.option.PropertyOption;
3132

3233
/**
3334
* A CommandLine implementation using the Preferences API, useful when
@@ -129,18 +130,26 @@ else if("false".equals(value)){
129130
}
130131
}
131132

132-
public String getProperty(final String property, final String defaultValue) {
133+
public String getProperty(final String property) {
134+
return getProperty(new PropertyOption(), property);
135+
}
136+
137+
public String getProperty(final Option option, final String property, final String defaultValue) {
133138
return preferences.get(property, defaultValue);
134139
}
135140

136-
public Set getProperties() {
141+
public Set getProperties(final Option option) {
137142
try {
138143
return new HashSet(Arrays.asList(preferences.keys()));
139144
} catch (BackingStoreException e) {
140145
return Collections.EMPTY_SET;
141146
}
142147
}
143148

149+
public Set getProperties() {
150+
return getProperties(new PropertyOption());
151+
}
152+
144153
public List getOptions() {
145154
try {
146155
final List options = new ArrayList();

src/java/org/apache/commons/cli2/commandline/PropertiesCommandLine.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.StringTokenizer;
2727

2828
import org.apache.commons.cli2.Option;
29+
import org.apache.commons.cli2.option.PropertyOption;
2930

3031
/**
3132
* A CommandLine implementation using a java Properties instance, useful for
@@ -122,14 +123,22 @@ else if("false".equals(value)){
122123
}
123124
}
124125

125-
public String getProperty(final String property, final String defaultValue) {
126+
public String getProperty(final String property) {
127+
return getProperty(new PropertyOption(), property);
128+
}
129+
130+
public String getProperty(final Option option, final String property, final String defaultValue) {
126131
return properties.getProperty(property,defaultValue);
127132
}
128133

129-
public Set getProperties() {
134+
public Set getProperties(final Option option) {
130135
return properties.keySet();
131136
}
132137

138+
public Set getProperties() {
139+
return getProperties(new PropertyOption());
140+
}
141+
133142
public List getOptions() {
134143
final List options = new ArrayList();
135144
final Iterator keys = properties.keySet().iterator();

src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.cli2.Argument;
2929
import org.apache.commons.cli2.Option;
3030
import org.apache.commons.cli2.WriteableCommandLine;
31+
import org.apache.commons.cli2.option.PropertyOption;
3132
import org.apache.commons.cli2.resource.ResourceConstants;
3233
import org.apache.commons.cli2.resource.ResourceHelper;
3334

@@ -37,7 +38,8 @@
3738
*/
3839
public class WriteableCommandLineImpl
3940
extends CommandLineImpl implements WriteableCommandLine {
40-
private final Properties properties = new Properties();
41+
private final Map optionToProperties = new HashMap();
42+
// private final Properties properties = new Properties();
4143
private final List options = new ArrayList();
4244
private final Map nameToOption = new HashMap();
4345
private final Map values = new HashMap();
@@ -159,20 +161,47 @@ public Boolean getSwitch(final Option option,
159161
return bool;
160162
}
161163

162-
public void addProperty(final String property,
164+
public String getProperty(final String property) {
165+
return getProperty(new PropertyOption(), property);
166+
}
167+
168+
public void addProperty(final Option option,
169+
final String property,
163170
final String value) {
171+
Properties properties = (Properties) optionToProperties.get(option);
172+
if (properties == null) {
173+
properties = new Properties();
174+
optionToProperties.put(option, properties);
175+
}
164176
properties.setProperty(property, value);
165177
}
166178

167-
public String getProperty(final String property,
179+
public void addProperty(final String property, final String value) {
180+
addProperty(new PropertyOption(), property, value);
181+
}
182+
183+
public String getProperty(final Option option,
184+
final String property,
168185
final String defaultValue) {
186+
Properties properties = (Properties) optionToProperties.get(option);
187+
if (properties == null) {
188+
return defaultValue;
189+
}
169190
return properties.getProperty(property, defaultValue);
170191
}
171192

172-
public Set getProperties() {
193+
public Set getProperties(final Option option) {
194+
Properties properties = (Properties) optionToProperties.get(option);
195+
if (properties == null) {
196+
return Collections.EMPTY_SET;
197+
}
173198
return Collections.unmodifiableSet(properties.keySet());
174199
}
175200

201+
public Set getProperties() {
202+
return getProperties(new PropertyOption());
203+
}
204+
176205
public boolean looksLikeOption(final String trigger) {
177206
for (final Iterator i = prefixes.iterator(); i.hasNext();) {
178207
final String prefix = (String) i.next();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void process(final WriteableCommandLine commandLine,
100100
value = arg.substring(equalsIndex + 1);
101101
}
102102

103-
commandLine.addProperty(property, value);
103+
commandLine.addProperty(this, property, value);
104104
}
105105

106106
public Set getTriggers() {

src/test/org/apache/commons/cli2/CommandLineTestCase.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ public final void testGetPropertyString() {
234234
* Class to test for String getProperty(String, String)
235235
*/
236236
public final void testGetPropertyStringString() {
237-
assertEquals("present property", commandLine.getProperty("present", "default property"));
238-
assertEquals("default property", commandLine.getProperty("missing", "default property"));
237+
assertEquals("present property", commandLine.getProperty(new PropertyOption(), "present", "default property"));
238+
assertEquals("default property", commandLine.getProperty(new PropertyOption(), "missing", "default property"));
239239
}
240240

241241
public final void testGetProperties() {
@@ -272,23 +272,23 @@ public final void testGetOptionTriggers() {
272272

273273
// OLD TESTS FOLLOW
274274
public final void testProperties() {
275-
final Option option = new PropertyOption();
275+
final PropertyOption option = new PropertyOption();
276276
final List args = CLITestCase.list();
277277
final WriteableCommandLine writeable = OptionTestCase.commandLine(option, args);
278278

279-
assertTrue(writeable.getProperties().isEmpty());
279+
assertTrue(writeable.getProperties(option).isEmpty());
280280

281-
writeable.addProperty("myprop", "myval");
282-
assertEquals(1, writeable.getProperties().size());
283-
assertEquals("myval", writeable.getProperty("myprop"));
281+
writeable.addProperty(option, "myprop", "myval");
282+
assertEquals(1, writeable.getProperties(option).size());
283+
assertEquals("myval", writeable.getProperty(option, "myprop"));
284284

285-
writeable.addProperty("myprop", "myval2");
286-
assertEquals(1, writeable.getProperties().size());
287-
assertEquals("myval2", writeable.getProperty("myprop"));
285+
writeable.addProperty(option, "myprop", "myval2");
286+
assertEquals(1, writeable.getProperties(option).size());
287+
assertEquals("myval2", writeable.getProperty(option, "myprop"));
288288

289-
writeable.addProperty("myprop2", "myval3");
290-
assertEquals(2, writeable.getProperties().size());
291-
assertEquals("myval3", writeable.getProperty("myprop2"));
289+
writeable.addProperty(option, "myprop2", "myval3");
290+
assertEquals(2, writeable.getProperties(option).size());
291+
assertEquals("myval3", writeable.getProperty(option, "myprop2"));
292292
}
293293

294294
public final void testOptions() {

src/test/org/apache/commons/cli2/WriteableCommandLineTestCase.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.cli2;
1818

1919
import org.apache.commons.cli2.option.ArgumentTest;
20+
import org.apache.commons.cli2.option.PropertyOption;
2021

2122
/**
2223
* @author Rob Oxspring
@@ -33,7 +34,7 @@ public abstract class WriteableCommandLineTestCase extends CommandLineTestCase {
3334
protected final CommandLine createCommandLine() {
3435
final WriteableCommandLine cl = createWriteableCommandLine();
3536
cl.addOption(present);
36-
cl.addProperty("present","present property");
37+
cl.addProperty(new PropertyOption(), "present","present property");
3738
cl.addSwitch(bool,true);
3839
cl.addValue(present,"present value");
3940
cl.addOption(multiple);
@@ -82,9 +83,9 @@ public final void testAddSwitch() {
8283
assertTrue(writeable.hasOption(present));
8384
}
8485
public final void testAddProperty() {
85-
assertNull(writeable.getProperty("present"));
86-
writeable.addProperty("present","present value");
87-
assertEquals("present value",writeable.getProperty("present"));
86+
assertNull(writeable.getProperty(new PropertyOption(), "present"));
87+
writeable.addProperty(new PropertyOption(), "present","present value");
88+
assertEquals("present value",writeable.getProperty(new PropertyOption(), "present"));
8889
}
8990
public final void testLooksLikeOption() {
9091
//TODO Implement looksLikeOption().

0 commit comments

Comments
 (0)