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