Author: bayard Date: Thu May 24 13:07:06 2007 New Revision: 541408 URL: http://svn.apache.org/viewvc?view=rev&rev=541408 Log: 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
Added: jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI71Test.java Modified: jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Parser.java Modified: jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java?view=diff&rev=541408&r1=541407&r2=541408 ============================================================================== --- jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java (original) +++ jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java Thu May 24 13:07:06 2007 @@ -631,4 +631,15 @@ return result; } + /** + * <p>Clear the Option values. After a + * parse is complete, these are left with data in them + * and they need clearing if another parse is done. </p> + * + * See: <a href="https://issues.apache.org/jira/browse/CLI-71">CLI-71</a> + */ + void clearValues() { + this.values.clear(); + } + } Modified: jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Parser.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Parser.java?view=diff&rev=541408&r1=541407&r2=541408 ============================================================================== --- jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Parser.java (original) +++ jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Parser.java Thu May 24 13:07:06 2007 @@ -132,6 +132,13 @@ { // initialise members this.options = options; + + // clear out the data in options in case it's been used before (CLI-71) + for (Iterator it = options.helpOptions().iterator(); it.hasNext();) { + Option opt = (Option) it.next(); + opt.clearValues(); + } + requiredOptions = options.getRequiredOptions(); cmd = new CommandLine(); @@ -403,4 +410,4 @@ // set the option on the command line cmd.addOption(opt); } -} \ No newline at end of file +} Added: jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java?view=auto&rev=541408 ============================================================================== --- jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java (added) +++ jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java Thu May 24 13:07:06 2007 @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.cli; + +import junit.framework.TestCase; + +/** + * @author brianegge + */ +public class OptionTest extends TestCase { + + public void testClear() { + Option option = new Option("x", true, ""); + assertEquals(0, option.getValuesList().size()); + option.addValue("a"); + assertEquals(1, option.getValuesList().size()); + option.clearValues(); + assertEquals(0, option.getValuesList().size()); + } + +} Added: jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI71Test.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI71Test.java?view=auto&rev=541408 ============================================================================== --- jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI71Test.java (added) +++ jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/bug/BugCLI71Test.java Thu May 24 13:07:06 2007 @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.cli.bug; + +import junit.framework.TestCase; + +import org.apache.commons.cli.*; + +public class BugCLI71Test extends TestCase { + + private Options options; + private CommandLineParser parser; + + public void setUp() { + options = new Options(); + + Option algorithm = new Option("a" , "algo", true, "the algorithm which it to perform executing"); + algorithm.setArgName("algorithm name"); + options.addOption(algorithm); + + Option key = new Option("k" , "key", true, "the key the setted algorithm uses to process"); + algorithm.setArgName("value"); + options.addOption(key); + + parser = new PosixParser(); + } + + public void testBasic() throws Exception { + String[] args = new String[] { "-a", "Caesar", "-k", "A" }; + CommandLine line = parser.parse( options, args); + assertEquals( "Caesar", line.getOptionValue("a") ); + assertEquals( "A", line.getOptionValue("k") ); + } + + public void testMistakenArgument() throws Exception { + String[] args = new String[] { "-a", "Caesar", "-k", "A" }; + CommandLine line = parser.parse( options, args); + args = new String[] { "-a", "Caesar", "-k", "a" }; + line = parser.parse( options, args); + assertEquals( "Caesar", line.getOptionValue("a") ); + assertEquals( "a", line.getOptionValue("k") ); + } + + public void testLackOfError() throws Exception { + String[] args = new String[] { "-k", "-a", "Caesar" }; + try { + CommandLine line = parser.parse( options, args); + fail("MissingArgumentException expected"); + } catch(MissingArgumentException mae) { + // expected + } + } + + public void testGetsDefaultIfOptional() throws Exception { + String[] args = new String[] { "-k", "-a", "Caesar" }; + options.getOption("k").setOptionalArg(true); + CommandLine line = parser.parse( options, args); + + assertEquals( "Caesar", line.getOptionValue("a") ); + assertEquals( "a", line.getOptionValue("k", "a") ); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]