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