Skip to content

Commit 84d333a

Browse files
committed
Added the method getOptionProperties() in CommandLine to retrieve easily a set of properties specified by an option (-Dparam=value)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@667595 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3011f7b commit 84d333a

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Iterator;
2323
import java.util.LinkedList;
2424
import java.util.List;
25+
import java.util.Properties;
2526

2627
/**
2728
* <p>Represents list of arguments parsed against
@@ -228,6 +229,45 @@ public String getOptionValue(char opt, String defaultValue)
228229
return getOptionValue(String.valueOf(opt), defaultValue);
229230
}
230231

232+
/**
233+
* Retrieve the map of values associated to the option. This is convenient
234+
* for options specifying Java properties like <tt>-Dparam1=value1
235+
* -Dparam2=value2</tt>. The first argument of the option is the key, and
236+
* the 2nd argument is the value. If the option has only one argument
237+
* (<tt>-Dfoo</tt>) it is considered as a boolean flag and the value is
238+
* <tt>"true"</tt>.
239+
*
240+
* @param opt name of the option
241+
* @return The Properties mapped by the option, never <tt>null</tt>
242+
* even if the option doesn't exists
243+
*/
244+
public Properties getOptionProperties(String opt)
245+
{
246+
Properties props = new Properties();
247+
248+
for (Iterator it = options.iterator(); it.hasNext();)
249+
{
250+
Option option = (Option) it.next();
251+
252+
if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt()))
253+
{
254+
List values = option.getValuesList();
255+
if (values.size() >= 2)
256+
{
257+
// use the first 2 arguments as the key/value pair
258+
props.put(values.get(0), values.get(1));
259+
}
260+
else if (values.size() == 1)
261+
{
262+
// no explicit value, handle it as a boolean
263+
props.put(values.get(0), "true");
264+
}
265+
}
266+
}
267+
268+
return props;
269+
}
270+
231271
/**
232272
* Retrieve any left-over non-recognized options and arguments
233273
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.cli;
19+
20+
import java.util.Properties;
21+
22+
import junit.framework.TestCase;
23+
24+
/**
25+
* @author Emmanuel Bourg
26+
* @version $Revision$, $Date$
27+
*/
28+
public class CommandLineTest extends TestCase
29+
{
30+
public void testGetOptionProperties() throws Exception
31+
{
32+
String[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" };
33+
34+
Options options = new Options();
35+
options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D'));
36+
options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create());
37+
38+
Parser parser = new GnuParser();
39+
CommandLine cl = parser.parse(options, args);
40+
41+
Properties props = cl.getOptionProperties("D");
42+
assertNotNull("null properties", props);
43+
assertEquals("number of properties in " + props, 4, props.size());
44+
assertEquals("property 1", "value1", props.getProperty("param1"));
45+
assertEquals("property 2", "value2", props.getProperty("param2"));
46+
assertEquals("property 3", "true", props.getProperty("param3"));
47+
assertEquals("property 4", "value4", props.getProperty("param4"));
48+
49+
assertEquals("property with long format", "bar", cl.getOptionProperties("property").getProperty("foo"));
50+
}
51+
}

0 commit comments

Comments
 (0)