Skip to content

Commit c155ceb

Browse files
committed
Applying Brian Egge and my work from CLI-71 to fix a lingering data problem in the parser and to confirm that other bugs have already been fixed
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/branches/cli-1.0.x@541408 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2ff9573 commit c155ceb

4 files changed

Lines changed: 131 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,15 @@ public int hashCode()
631631
return result;
632632
}
633633

634+
/**
635+
* <p>Clear the Option values. After a
636+
* parse is complete, these are left with data in them
637+
* and they need clearing if another parse is done. </p>
638+
*
639+
* See: <a href="https://issues.apache.org/jira/browse/CLI-71">CLI-71</a>
640+
*/
641+
void clearValues() {
642+
this.values.clear();
643+
}
644+
634645
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ public CommandLine parse(Options options, String[] arguments,
132132
{
133133
// initialise members
134134
this.options = options;
135+
136+
// clear out the data in options in case it's been used before (CLI-71)
137+
for (Iterator it = options.helpOptions().iterator(); it.hasNext();) {
138+
Option opt = (Option) it.next();
139+
opt.clearValues();
140+
}
141+
135142
requiredOptions = options.getRequiredOptions();
136143
cmd = new CommandLine();
137144

@@ -403,4 +410,4 @@ private void processOption(String arg, ListIterator iter)
403410
// set the option on the command line
404411
cmd.addOption(opt);
405412
}
406-
}
413+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.cli;
18+
19+
import junit.framework.TestCase;
20+
21+
/**
22+
* @author brianegge
23+
*/
24+
public class OptionTest extends TestCase {
25+
26+
public void testClear() {
27+
Option option = new Option("x", true, "");
28+
assertEquals(0, option.getValuesList().size());
29+
option.addValue("a");
30+
assertEquals(1, option.getValuesList().size());
31+
option.clearValues();
32+
assertEquals(0, option.getValuesList().size());
33+
}
34+
35+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.cli.bug;
18+
19+
import junit.framework.TestCase;
20+
21+
import org.apache.commons.cli.*;
22+
23+
public class BugCLI71Test extends TestCase {
24+
25+
private Options options;
26+
private CommandLineParser parser;
27+
28+
public void setUp() {
29+
options = new Options();
30+
31+
Option algorithm = new Option("a" , "algo", true, "the algorithm which it to perform executing");
32+
algorithm.setArgName("algorithm name");
33+
options.addOption(algorithm);
34+
35+
Option key = new Option("k" , "key", true, "the key the setted algorithm uses to process");
36+
algorithm.setArgName("value");
37+
options.addOption(key);
38+
39+
parser = new PosixParser();
40+
}
41+
42+
public void testBasic() throws Exception {
43+
String[] args = new String[] { "-a", "Caesar", "-k", "A" };
44+
CommandLine line = parser.parse( options, args);
45+
assertEquals( "Caesar", line.getOptionValue("a") );
46+
assertEquals( "A", line.getOptionValue("k") );
47+
}
48+
49+
public void testMistakenArgument() throws Exception {
50+
String[] args = new String[] { "-a", "Caesar", "-k", "A" };
51+
CommandLine line = parser.parse( options, args);
52+
args = new String[] { "-a", "Caesar", "-k", "a" };
53+
line = parser.parse( options, args);
54+
assertEquals( "Caesar", line.getOptionValue("a") );
55+
assertEquals( "a", line.getOptionValue("k") );
56+
}
57+
58+
public void testLackOfError() throws Exception {
59+
String[] args = new String[] { "-k", "-a", "Caesar" };
60+
try {
61+
CommandLine line = parser.parse( options, args);
62+
fail("MissingArgumentException expected");
63+
} catch(MissingArgumentException mae) {
64+
// expected
65+
}
66+
}
67+
68+
public void testGetsDefaultIfOptional() throws Exception {
69+
String[] args = new String[] { "-k", "-a", "Caesar" };
70+
options.getOption("k").setOptionalArg(true);
71+
CommandLine line = parser.parse( options, args);
72+
73+
assertEquals( "Caesar", line.getOptionValue("a") );
74+
assertEquals( "a", line.getOptionValue("k", "a") );
75+
}
76+
77+
}

0 commit comments

Comments
 (0)