b85deac
commit 77218790904f40395304669f5d79740f459c0a90 (HEAD -> cli-253, origin/cli-253)
b85deac
Author:     Michal Srb <msrb@redhat.com>
b85deac
AuthorDate: Mon Jun 22 15:01:30 2015 +0200
b85deac
Commit:     Michal Srb <msrb@redhat.com>
b85deac
CommitDate: Mon Jun 22 15:04:05 2015 +0200
b85deac
b85deac
    [CLI-253] Prevent "Unrecognized option: --null" when handling long opts in PosixParser
b85deac
b85deac
diff --git a/src/main/java/org/apache/commons/cli/Options.java b/src/main/java/org/apache/commons/cli/Options.java
b85deac
index 0ee4eea..1c38194 100644
b85deac
--- a/src/main/java/org/apache/commons/cli/Options.java
b85deac
+++ b/src/main/java/org/apache/commons/cli/Options.java
b85deac
@@ -224,6 +224,20 @@ public class Options implements Serializable
b85deac
     }
b85deac
 
b85deac
     /**
b85deac
+     * Retrieve the {@link Option} matching the long name specified.
b85deac
+     * The leading hyphens in the name are ignored (up to 2).
b85deac
+     *
b85deac
+     * @param opt long name of the {@link Option}
b85deac
+     * @return the option represented by opt
b85deac
+     */
b85deac
+    Option getLongOption(String opt)
b85deac
+    {
b85deac
+        opt = Util.stripLeadingHyphens(opt);
b85deac
+
b85deac
+        return longOpts.get(opt);
b85deac
+    }
b85deac
+
b85deac
+    /**
b85deac
      * Returns the options with a long name starting with the name specified.
b85deac
      * 
b85deac
      * @param opt the partial name of the option
b85deac
diff --git a/src/main/java/org/apache/commons/cli/PosixParser.java b/src/main/java/org/apache/commons/cli/PosixParser.java
b85deac
index c13a65e..14d2936 100644
b85deac
--- a/src/main/java/org/apache/commons/cli/PosixParser.java
b85deac
+++ b/src/main/java/org/apache/commons/cli/PosixParser.java
b85deac
@@ -131,7 +131,7 @@ public class PosixParser extends Parser
b85deac
                 }
b85deac
                 else
b85deac
                 {
b85deac
-                    currentOption = options.getOption(matchingOpts.get(0));
b85deac
+                    currentOption = options.getLongOption(matchingOpts.get(0));
b85deac
                     
b85deac
                     tokens.add("--" + currentOption.getLongOpt());
b85deac
                     if (pos != -1)
b85deac
diff --git a/src/test/java/org/apache/commons/cli/bug/BugCLI253Test.java b/src/test/java/org/apache/commons/cli/bug/BugCLI253Test.java
b85deac
new file mode 100644
b85deac
index 0000000..e37b7bc
b85deac
--- /dev/null
b85deac
+++ b/src/test/java/org/apache/commons/cli/bug/BugCLI253Test.java
b85deac
@@ -0,0 +1,44 @@
b85deac
+/*
b85deac
+ * Licensed to the Apache Software Foundation (ASF) under one or more
b85deac
+ * contributor license agreements.  See the NOTICE file distributed with
b85deac
+ * this work for additional information regarding copyright ownership.
b85deac
+ * The ASF licenses this file to You under the Apache License, Version 2.0
b85deac
+ * (the "License"); you may not use this file except in compliance with
b85deac
+ * the License.  You may obtain a copy of the License at
b85deac
+ *
b85deac
+ *      http://www.apache.org/licenses/LICENSE-2.0
b85deac
+ *
b85deac
+ * Unless required by applicable law or agreed to in writing, software
b85deac
+ * distributed under the License is distributed on an "AS IS" BASIS,
b85deac
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b85deac
+ * See the License for the specific language governing permissions and
b85deac
+ * limitations under the License.
b85deac
+ */
b85deac
+
b85deac
+package org.apache.commons.cli.bug;
b85deac
+
b85deac
+import static org.junit.Assert.assertTrue;
b85deac
+
b85deac
+import org.apache.commons.cli.CommandLine;
b85deac
+import org.apache.commons.cli.Option;
b85deac
+import org.apache.commons.cli.Options;
b85deac
+import org.apache.commons.cli.ParseException;
b85deac
+import org.apache.commons.cli.PosixParser;
b85deac
+import org.junit.Test;
b85deac
+
b85deac
+@SuppressWarnings("deprecation") // tests some deprecated classes
b85deac
+public class BugCLI253Test {
b85deac
+
b85deac
+    @Test
b85deac
+    public void testGroovyUseCase() throws ParseException {
b85deac
+        CommandLine cli = new PosixParser().parse(getOptions(), new String[] { "--classpath" });
b85deac
+        assertTrue(cli.hasOption("--classpath"));
b85deac
+    }
b85deac
+
b85deac
+    private Options getOptions() {
b85deac
+        Options options = new Options();
b85deac
+        options.addOption(Option.builder("classpath").build());
b85deac
+        options.addOption(Option.builder("cp").longOpt("classpath").build());
b85deac
+        return options;
b85deac
+    }
b85deac
+}