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