commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r679530 - in /commons/proper/cli/trunk/src: java/org/apache/commons/cli2/ java/org/apache/commons/cli2/commandline/ java/org/apache/commons/cli2/option/ test/org/apache/commons/cli2/ test/org/apache/commons/cli2/bug/
Date Thu, 24 Jul 2008 20:28:17 GMT
Author: oheger
Date: Thu Jul 24 13:28:16 2008
New Revision: 679530

URL: http://svn.apache.org/viewvc?rev=679530&view=rev
Log:
CLI-123: Groups are now added to the command line if any of their child options are found.
This makes it possible to test for the presence of a group and also validate minimum and maximum
constraints when child groups are involved.

Added:
    commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI123Test.java   (with
props)
Modified:
    commons/proper/cli/trunk/src/java/org/apache/commons/cli2/Option.java
    commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
    commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
    commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/OptionImpl.java
    commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CommandLineTestCase.java

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/Option.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/Option.java?rev=679530&r1=679529&r2=679530&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/Option.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/Option.java Thu Jul 24 13:28:16
2008
@@ -194,4 +194,25 @@
      * @return true iff the CommandLine will be invalid without this Option
      */
     boolean isRequired();
+
+    /**
+     * Returns the parent of this option. Options can be organized in a
+     * hierarchical manner if they are added to groups. This method can be used
+     * for obtaining the parent option of this option. The result may be
+     * <b>null</b> if this option does not have a parent.
+     *
+     * @return the parent of this option
+     */
+    Option getParent();
+
+    /**
+     * Sets the parent of this option. This method is called when the option is
+     * added to a group. Storing the parent of an option makes it possible to
+     * keep track of hierarchical relations between options. For instance, if an
+     * option is identified while parsing a command line, the group this option
+     * belongs to can also be added to the command line.
+     *
+     * @param parent the parent option
+     */
+    void setParent(Option parent);
 }

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java?rev=679530&r1=679529&r2=679530&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
(original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
Thu Jul 24 13:28:16 2008
@@ -69,6 +69,13 @@
         for (Iterator i = option.getTriggers().iterator(); i.hasNext();) {
             nameToOption.put(i.next(), option);
         }
+
+        // ensure that all parent options are also added
+        Option parent = option.getParent();
+        while (parent != null && !options.contains(parent)) {
+            options.add(parent);
+            parent = parent.getParent();
+        }
     }
 
     public void addValue(final Option option,

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java?rev=679530&r1=679529&r2=679530&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java Thu Jul
24 13:28:16 2008
@@ -89,6 +89,7 @@
         // process the options
         for (final Iterator i = options.iterator(); i.hasNext();) {
             final Option option = (Option) i.next();
+            option.setParent(this);
 
             if (option instanceof Argument) {
                 i.remove();

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/OptionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/OptionImpl.java?rev=679530&r1=679529&r2=679530&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/OptionImpl.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/OptionImpl.java Thu Jul
24 13:28:16 2008
@@ -33,6 +33,7 @@
 public abstract class OptionImpl implements Option {
     private final int id;
     private final boolean required;
+    private Option parent;
 
     /**
      * Creates an OptionImpl with the specified id
@@ -95,7 +96,9 @@
 
     public int hashCode() {
         int hashCode = getId();
-        hashCode = (hashCode * 37) + getPreferredName().hashCode();
+        if (getPreferredName() != null) {
+            hashCode = (hashCode * 37) + getPreferredName().hashCode();
+        }
 
         if (getDescription() != null) {
             hashCode = (hashCode * 37) + getDescription().hashCode();
@@ -123,6 +126,14 @@
         // nothing to do normally
     }
 
+    public Option getParent() {
+        return parent;
+    }
+
+    public void setParent(Option parent) {
+        this.parent = parent;
+    }
+
     protected void checkPrefixes(final Set prefixes) {
         // nothing to do if empty prefix list
         if (prefixes.isEmpty()) {

Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CommandLineTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CommandLineTestCase.java?rev=679530&r1=679529&r2=679530&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CommandLineTestCase.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CommandLineTestCase.java Thu
Jul 24 13:28:16 2008
@@ -433,6 +433,7 @@
         final Iterator i = cl.getOptions().iterator();
 
         assertSame(login, i.next());
+        assertSame(group, i.next());
         assertSame(help, i.next());
         assertSame(targets, i.next());
         assertSame(targets, i.next());

Added: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI123Test.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI123Test.java?rev=679530&view=auto
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI123Test.java (added)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI123Test.java Thu Jul
24 13:28:16 2008
@@ -0,0 +1,126 @@
+/**
+ * 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.cli2.bug;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.cli2.CommandLine;
+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;
+
+/**
+ * Group options are not added to the command line when child elements are
+ * detected. This causes the validation of maximum and minimum to fail.
+ *
+ * @author Oliver Heger
+ * @version $Id$
+ */
+public class BugCLI123Test extends TestCase {
+    /** An option of the parent group. */
+    private Option parentOption;
+
+    /** An option of the child group. */
+    private Option childOption1;
+
+    /** Another option of the child group. */
+    private Option childOption2;
+
+    /** The parent group. */
+    private Group parentGroup;
+
+    /** The child group. */
+    private Group childGroup;
+
+    /** The parser. */
+    private Parser parser;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
+        final ArgumentBuilder abuilder = new ArgumentBuilder();
+        final GroupBuilder gbuilder = new GroupBuilder();
+        parentOption = obuilder.withLongName("parent").withShortName("p")
+                .withArgument(abuilder.withName("name").create()).create();
+        childOption1 = obuilder.withLongName("child").withShortName("c")
+                .withArgument(abuilder.withName("c").create()).create();
+        childOption2 = obuilder.withLongName("sub").withShortName("s")
+                .withArgument(abuilder.withName("s").create()).create();
+        childGroup = gbuilder.withName("childOptions").withMinimum(0)
+                .withMaximum(2).withOption(childOption1).withOption(
+                        childOption2).create();
+        parentGroup = gbuilder.withName("parentOptions").withMinimum(1)
+                .withMaximum(1).withOption(parentOption).withOption(childGroup)
+                .create();
+        parser = new Parser();
+        parser.setGroup(parentGroup);
+    }
+
+    /**
+     * A single option of the child group is specified.
+     */
+    public void testSingleChildOption() throws OptionException {
+        CommandLine cl = parser.parse(new String[] { "--child", "test" });
+        assertTrue("Child option not found", cl.hasOption(childOption1));
+        assertEquals("Wrong value for option", "test", cl
+                .getValue(childOption1));
+        assertTrue("Child group not found", cl.hasOption(childGroup));
+    }
+
+    /**
+     * Two options of the child group are specified.
+     */
+    public void testMultipleChildOptions() throws OptionException {
+        CommandLine cl = parser.parse(new String[] { "--child", "test",
+                "--sub", "anotherTest" });
+        assertTrue("Child option not found", cl.hasOption(childOption1));
+        assertEquals("Wrong value for option", "test", cl
+                .getValue(childOption1));
+        assertTrue("Sub option not found", cl.hasOption(childOption2));
+        assertEquals("Wrong value for sub option", "anotherTest", cl
+                .getValue(childOption2));
+        assertTrue("Child group not found", cl.hasOption(childGroup));
+    }
+
+    /**
+     * The option defined for the parent group is specified.
+     */
+    public void testSingleParentOption() throws OptionException {
+        CommandLine cl = parser.parse(new String[] { "--parent", "yes" });
+        assertTrue("Parent option not found", cl.hasOption(parentOption));
+        assertEquals("Wrong value for option", "yes", cl.getValue(parentOption));
+        assertFalse("Found child group", cl.hasOption(childGroup));
+    }
+
+    /**
+     * The parent option and an option of the child group is specified. This
+     * should cause an exception.
+     */
+    public void testParentOptionAndChildOption() throws OptionException {
+        try {
+            parser.parse(new String[] { "--parent", "error", "--child",
+                    "exception" });
+            fail("Maximum restriction for parent not verified!");
+        } catch (OptionException oex) {
+            // ok
+        }
+    }
+}

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

Propchange: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI123Test.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI123Test.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message