Skip to content

Commit a931391

Browse files
author
Oliver Heger
committed
CLI-158: Default arguments are now applied even if arguments are passed. The default values for missing arguments will be set.
Thanks to tobias dot bocanegra at day dot com for the patch. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@679511 13f79535-47bb-0310-9956-ffa450edef68
1 parent c338c8a commit a931391

2 files changed

Lines changed: 136 additions & 15 deletions

File tree

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,33 @@ public Option getOption(final String trigger) {
109109
}
110110

111111
public List getValues(final Option option,
112-
final List defaultValues) {
113-
// First grab the command line values
112+
List defaultValues) {
113+
// initialize the return list
114114
List valueList = (List) values.get(option);
115115

116-
// Secondly try the defaults supplied to the method
117-
if ((valueList == null) || valueList.isEmpty()) {
118-
valueList = defaultValues;
119-
}
120-
121-
// Thirdly try the option's default values
122-
if ((valueList == null) || valueList.isEmpty()) {
123-
valueList = (List) this.defaultValues.get(option);
116+
// grab the correct default values
117+
if (defaultValues == null || defaultValues.isEmpty()) {
118+
defaultValues = (List) this.defaultValues.get(option);
124119
}
125120

126-
// Finally use an empty list
127-
if (valueList == null) {
128-
valueList = Collections.EMPTY_LIST;
121+
// augment the list with the default values
122+
if (defaultValues != null && !defaultValues.isEmpty()) {
123+
if (valueList == null || valueList.isEmpty()) {
124+
valueList = defaultValues;
125+
} else {
126+
// if there are more default values as specified, add them to
127+
// the list.
128+
if (defaultValues.size() > valueList.size()) {
129+
// copy the list first
130+
valueList = new ArrayList(valueList);
131+
for (int i=valueList.size(); i<defaultValues.size(); i++) {
132+
valueList.add(defaultValues.get(i));
133+
}
134+
}
135+
}
129136
}
130-
131-
return valueList;
137+
138+
return valueList == null ? Collections.EMPTY_LIST : valueList;
132139
}
133140

134141
public List getUndefaultedValues(Option option) {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
package org.apache.commons.cli2.bug;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
import junit.framework.TestCase;
23+
24+
import org.apache.commons.cli2.CommandLine;
25+
import org.apache.commons.cli2.Group;
26+
import org.apache.commons.cli2.builder.ArgumentBuilder;
27+
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
28+
import org.apache.commons.cli2.builder.GroupBuilder;
29+
import org.apache.commons.cli2.commandline.Parser;
30+
import org.apache.commons.cli2.option.DefaultOption;
31+
32+
/**
33+
* http://issues.apache.org/jira/browse/CLI-158
34+
*/
35+
public class BugCLI158Test extends TestCase {
36+
37+
private Parser createDefaultValueParser(String[] defaults) {
38+
final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
39+
final ArgumentBuilder abuilder = new ArgumentBuilder();
40+
final GroupBuilder gbuilder = new GroupBuilder();
41+
42+
DefaultOption bOption = obuilder.withShortName("b")
43+
.withLongName("b")
44+
.withArgument(abuilder.withName("b")
45+
.withMinimum(0)
46+
.withMaximum(defaults.length)
47+
.withDefaults(Arrays.asList(defaults))
48+
.create())
49+
.create();
50+
51+
Group options = gbuilder
52+
.withName("options")
53+
.withOption(bOption)
54+
.create();
55+
56+
Parser parser = new Parser();
57+
parser.setHelpTrigger("--help");
58+
parser.setGroup(options);
59+
return parser;
60+
}
61+
62+
public void testSingleOptionSingleArgument() throws Exception {
63+
Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
64+
String enteredValue1 = "1";
65+
String[] args = new String[]{"-b", enteredValue1};
66+
CommandLine cl = parser.parse(args);
67+
CommandLine cmd = cl;
68+
assertNotNull(cmd);
69+
List b = cmd.getValues("-b");
70+
assertEquals("[" + enteredValue1 + ", 1000]", b + "");
71+
}
72+
73+
public void testSingleOptionNoArgument() throws Exception {
74+
Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
75+
String[] args = new String[]{"-b"};
76+
CommandLine cl = parser.parse(args);
77+
CommandLine cmd = cl;
78+
assertNotNull(cmd);
79+
List b = cmd.getValues("-b");
80+
assertEquals("[100, 1000]", b + "");
81+
}
82+
83+
public void testSingleOptionMaximumNumberOfArgument() throws Exception {
84+
String[] args = new String[]{"-b", "1", "2"};
85+
final ArgumentBuilder abuilder = new ArgumentBuilder();
86+
final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
87+
final GroupBuilder gbuilder = new GroupBuilder();
88+
89+
DefaultOption bOption = obuilder.withShortName("b")
90+
.withLongName("b")
91+
.withArgument(abuilder.withName("b")
92+
.withMinimum(2)
93+
.withMaximum(4)
94+
.withDefault("100")
95+
.withDefault("1000")
96+
.withDefault("10000")
97+
.create())
98+
.create();
99+
100+
Group options = gbuilder
101+
.withName("options")
102+
.withOption(bOption)
103+
.create();
104+
105+
Parser parser = new Parser();
106+
parser.setHelpTrigger("--help");
107+
parser.setGroup(options);
108+
CommandLine cl = parser.parse(args);
109+
CommandLine cmd = cl;
110+
assertNotNull(cmd);
111+
List b = cmd.getValues("-b");
112+
assertEquals("[1, 2, 10000]", b + "");
113+
}
114+
}

0 commit comments

Comments
 (0)