You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ro...@apache.org on 2005/02/24 19:07:01 UTC

svn commit: r155218 - in jakarta/commons/proper/cli/trunk/src: java/org/apache/commons/cli2/option/GroupImpl.java test/org/apache/commons/cli2/bug/Bug32533Test.java test/org/apache/commons/cli2/option/NestedGroupTest.java

Author: roxspring
Date: Thu Feb 24 10:07:00 2005
New Revision: 155218

URL: http://svn.apache.org/viewcvs?view=rev&rev=155218
Log:
Nested group handling improved
Applied the test from the bug and implemented an alternate fix
PR: 32533
Submitted by: David Morris

Added:
    jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/Bug32533Test.java   (with props)
    jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java   (with props)
Modified:
    jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java

Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java?view=diff&r1=155217&r2=155218
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java Thu Feb 24 10:07:00 2005
@@ -178,13 +178,20 @@
                     // narrow the search
                     final Collection values = optionMap.tailMap(arg).values();
                     
-                    for (Iterator i = values.iterator(); i.hasNext();) {
+                    boolean foundMemberOption = false;
+                    for (Iterator i = values.iterator(); i.hasNext() && !foundMemberOption;) {
                         final Option option = (Option) i.next();
                         
                         if (option.canProcess(arg)) {
+                        	foundMemberOption = true;
                             arguments.previous();
                             option.process(commandLine, arguments);
                         }
+                    }
+                    // back track and abort this group if necessary
+                    if(!foundMemberOption) {
+                    	arguments.previous();
+                    	return;
                     }
                 } // [END argument may be anonymous
                 

Added: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/Bug32533Test.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/Bug32533Test.java?view=auto&rev=155218
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/Bug32533Test.java (added)
+++ jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/Bug32533Test.java Thu Feb 24 10:07:00 2005
@@ -0,0 +1,49 @@
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.cli2.bug;
+
+import org.apache.commons.cli2.Argument;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.Option;
+import org.apache.commons.cli2.OptionException;
+import org.apache.commons.cli2.builder.ArgumentBuilder;
+import org.apache.commons.cli2.builder.DefaultOptionBuilder;
+import org.apache.commons.cli2.builder.GroupBuilder;
+import org.apache.commons.cli2.commandline.Parser;
+
+import junit.framework.TestCase;
+
+/**
+ * @author roxspring
+ */
+public class Bug32533Test extends TestCase {
+    
+    public void testBlah() throws OptionException {
+        
+        Option a1 = new DefaultOptionBuilder().withLongName("a1").create();
+        Option b1 = new DefaultOptionBuilder().withLongName("b1").create();
+        Option c1 = new DefaultOptionBuilder().withLongName("c1").create();
+        
+        Group b = new GroupBuilder().withOption(b1).create();
+        Group c = new GroupBuilder().withOption(c1).create();
+        Group a = new GroupBuilder().withOption(a1).withOption(b).withOption(c).create();
+        
+        Parser parser = new Parser();  
+        parser.setGroup(a);
+        parser.parse(new String[]{"--a1","--b1"});
+    }
+
+}

Propchange: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/Bug32533Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java?view=auto&rev=155218
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java (added)
+++ jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java Thu Feb 24 10:07:00 2005
@@ -0,0 +1,199 @@
+/**
+ * Copyright 2003-2004 The Apache Software Foundation
+ *
+ * Licensed 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.cli2.option;
+
+import org.apache.commons.cli2.CLITestCase;
+import org.apache.commons.cli2.CommandLine;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.OptionException;
+import org.apache.commons.cli2.builder.ArgumentBuilder;
+import org.apache.commons.cli2.builder.DefaultOptionBuilder;
+import org.apache.commons.cli2.builder.GroupBuilder;
+import org.apache.commons.cli2.commandline.Parser;
+import org.apache.commons.cli2.util.HelpFormatter;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * Test to exercise nested groups developed to demonstrate bug 32533
+ */
+public class NestedGroupTest extends CLITestCase {
+    final static DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
+    final static ArgumentBuilder abuilder = new ArgumentBuilder();
+    final static GroupBuilder gbuilder = new GroupBuilder();
+
+    static Group buildActionGroup() {
+        return gbuilder.withName("Action").withDescription("Action")
+                       .withMinimum(1).withMaximum(1)
+                       .withOption(obuilder.withId(5).withShortName("e")
+                                           .withLongName("encrypt")
+                                           .withDescription("Encrypt input")
+                                           .create())
+                       .withOption(obuilder.withId(6).withShortName("d")
+                                           .withLongName("decrypt")
+                                           .withDescription("Decrypt input")
+                                           .create()).create();
+    }
+
+    static Group buildAlgorithmGroup() {
+        return gbuilder.withName("Algorithm")
+                       .withDescription("Encryption Algorithm").withMaximum(1)
+                       .withOption(obuilder.withId(0).withShortName("b")
+                                           .withLongName("blowfish")
+                                           .withDescription("Blowfish").create())
+                       .withOption(obuilder.withId(1).withShortName("3")
+                                           .withLongName("3DES")
+                                           .withDescription("Triple DES")
+                                           .create()).create();
+    }
+
+    static Group buildInputGroup() {
+        return gbuilder.withName("Input").withDescription("Input").withMinimum(1)
+                       .withMaximum(1)
+                       .withOption(obuilder.withId(2).withShortName("f")
+                                           .withLongName("file")
+                                           .withDescription("Input file")
+                                           .withArgument(abuilder.withName(
+                    "file").withMinimum(1).withMaximum(1).create()).create())
+                       .withOption(obuilder.withId(3).withShortName("s")
+                                           .withLongName("string")
+                                           .withDescription("Input string")
+                                           .withArgument(abuilder.withName(
+                    "string").withMinimum(1).withMaximum(1).create()).create())
+                       .create();
+    }
+
+    static Group buildEncryptionServiceGroup(Group[] nestedGroups) {
+        gbuilder.withName("encryptionService")
+                .withOption(obuilder.withId(4).withShortName("h")
+                                    .withLongName("help")
+                                    .withDescription("Print this message")
+                                    .create()).withOption(obuilder.withShortName(
+                "k").withLongName("key").withDescription("Encryption key")
+                                                                  .create());
+
+        for (int i = 0; i < nestedGroups.length; i++) {
+            gbuilder.withOption(nestedGroups[i]);
+        }
+
+        return gbuilder.create();
+    }
+
+    public void testNestedGroup()
+        throws OptionException {
+        final String[] args = {
+                "-eb",
+                "--file",
+                "/tmp/filename.txt"
+            };
+
+        Group[] nestedGroups = {
+                buildActionGroup(),
+                buildAlgorithmGroup(),
+                buildInputGroup()
+            };
+
+        Parser parser = new Parser();
+        parser.setGroup(buildEncryptionServiceGroup(nestedGroups));
+
+        CommandLine commandLine = parser.parse(args);
+
+        assertTrue("/tmp/filename.txt".equals(
+                (String) commandLine.getValue("-f")));
+        assertTrue(commandLine.hasOption("-e"));
+        assertTrue(commandLine.hasOption("-b"));
+        assertFalse(commandLine.hasOption("-d"));
+    }
+
+    public void testNestedGroupHelp()
+        throws OptionException {
+        final String[] args = {
+                "-eb",
+                "--file",
+                "/tmp/filename.txt"
+            };
+
+        Group[] nestedGroups = {
+                buildActionGroup(),
+                buildAlgorithmGroup(),
+                buildInputGroup()
+            };
+
+        HelpFormatter helpFormatter = new HelpFormatter();
+        helpFormatter.setGroup(buildEncryptionServiceGroup(nestedGroups));
+
+        final StringWriter out = new StringWriter();
+        helpFormatter.setPrintWriter(new PrintWriter(out));
+
+        try {
+            helpFormatter.print();
+
+            final BufferedReader bufferedReader = new BufferedReader(new StringReader(
+                        out.toString()));
+            final String[] expected = new String[] {
+                    "Usage:                                                                          ",
+                    " [-h -k -e|-d -b|-3 -f <file>|-s <string>]                                      ",
+                    "encryptionService                                                               ",
+                    "  -h (--help)               Print this message                                  ",
+                    "  -k (--key)                Encryption key                                      ",
+                    "  Action                    Action                                              ",
+                    "    -e (--encrypt)          Encrypt input                                       ",
+                    "    -d (--decrypt)          Decrypt input                                       ",
+                    "  Algorithm                 Encryption Algorithm                                ",
+                    "    -b (--blowfish)         Blowfish                                            ",
+                    "    -3 (--3DES)             Triple DES                                          ",
+                    "  Input                     Input                                               ",
+                    "    -f (--file) file        Input file                                          ",
+                    "    -s (--string) string    Input string                                        "
+                };
+
+            List actual = new ArrayList(expected.length);
+            String input;
+
+            while ((input = bufferedReader.readLine()) != null) {
+                actual.add(input);
+            }
+
+            // Show they are the same number of lines
+            assertEquals("Help text lines should be " + expected.length,
+                actual.size(), expected.length);
+
+            for (int i = 0; i < expected.length; i++) {
+                if (!expected[i].equals(actual.get(i))) {
+                    for (int x = 0; x < expected.length; i++) {
+                        System.out.println("   " + expected[i]);
+                        System.out.println((expected[i].equals(actual.get(i))
+                            ? "== "
+                            : "!= ") + actual.get(i));
+                    }
+                }
+
+                assertEquals(expected[i], actual.get(i));
+            }
+        }
+        catch (IOException e) {
+            fail(e.getLocalizedMessage());
+        }
+    }
+}

Propchange: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org