Skip to content

Commit d72a578

Browse files
committed
Parser no longer removes the required options from the Options instance (CLI-156)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@661332 13f79535-47bb-0310-9956-ffa450edef68
1 parent d0f6128 commit d72a578

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.cli;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Enumeration;
2122
import java.util.Iterator;
@@ -43,7 +44,7 @@ public abstract class Parser implements CommandLineParser {
4344

4445
protected void setOptions(final Options options) {
4546
this.options = options;
46-
this.requiredOptions = options.getRequiredOptions();
47+
this.requiredOptions = new ArrayList(options.getRequiredOptions());
4748
}
4849

4950
protected Options getOptions() {

src/test/org/apache/commons/cli/ParseRequiredTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,26 @@ public void testMissingRequiredOptions()
140140
}
141141
}
142142

143+
public void testReuseOptionsTwice() throws Exception
144+
{
145+
Options opts = new Options();
146+
opts.addOption(OptionBuilder.isRequired().create('v'));
147+
148+
GnuParser parser = new GnuParser();
149+
150+
// first parsing
151+
parser.parse(opts, new String[] { "-v" });
152+
153+
try
154+
{
155+
// second parsing, with the same Options instance and an invalid command line
156+
parser.parse(opts, new String[0]);
157+
fail("MissingOptionException not thrown");
158+
}
159+
catch (MissingOptionException e)
160+
{
161+
// expected
162+
}
163+
}
164+
143165
}

0 commit comments

Comments
 (0)