You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2017/06/08 17:36:51 UTC
[22/50] [abbrv] commons-cli git commit: CLI-265: Optional argument
picking up next regular option as its argument. Thank you to Lynn Henderson,
Martin Sandiford and Veit Guna for providing reproductions.
CLI-265: Optional argument picking up next regular option as its argument. Thank you to Lynn Henderson, Martin Sandiford and Veit Guna for providing reproductions.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1759695 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/commons-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-cli/commit/1bf9e6c5
Tree: http://git-wip-us.apache.org/repos/asf/commons-cli/tree/1bf9e6c5
Diff: http://git-wip-us.apache.org/repos/asf/commons-cli/diff/1bf9e6c5
Branch: refs/heads/master
Commit: 1bf9e6c551b6a2e7d37291673a1ff77c338ce131
Parents: 9e51962
Author: Benedikt Ritter <br...@apache.org>
Authored: Wed Sep 7 20:19:32 2016 +0000
Committer: Benedikt Ritter <br...@apache.org>
Committed: Wed Sep 7 20:19:32 2016 +0000
----------------------------------------------------------------------
src/changes/changes.xml | 3 ++
.../org/apache/commons/cli/DefaultParser.java | 10 +++-
.../apache/commons/cli/bug/BugCLI265Test.java | 56 ++++++++++++++++++++
3 files changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-cli/blob/1bf9e6c5/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 76730ca..d54c4dc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -23,6 +23,9 @@
<body>
<release version="1.4" date="tba" description="tba">
+ <action type="fix" dev="britter" issue="CLI-265">
+ Optional argument picking up next regular option as its argument
+ </action>
<action type="add" dev="britter" issue="CLI-267" due-to="Ricardo Ribeiro">
Add an addRequiredOption method to Options
</action>
http://git-wip-us.apache.org/repos/asf/commons-cli/blob/1bf9e6c5/src/main/java/org/apache/commons/cli/DefaultParser.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/cli/DefaultParser.java b/src/main/java/org/apache/commons/cli/DefaultParser.java
index efd8b4a..f34173e 100644
--- a/src/main/java/org/apache/commons/cli/DefaultParser.java
+++ b/src/main/java/org/apache/commons/cli/DefaultParser.java
@@ -299,7 +299,15 @@ public class DefaultParser implements CommandLineParser
private boolean isShortOption(String token)
{
// short options (-S, -SV, -S=V, -SV1=V2, -S1S2)
- return token.startsWith("-") && token.length() >= 2 && options.hasShortOption(token.substring(1, 2));
+ if (!token.startsWith("-") || token.length() == 1)
+ {
+ return false;
+ }
+
+ // remove leading "-" and "=value"
+ int pos = token.indexOf("=");
+ String optName = pos == -1 ? token.substring(1) : token.substring(1, pos);
+ return options.hasShortOption(optName);
}
/**
http://git-wip-us.apache.org/repos/asf/commons-cli/blob/1bf9e6c5/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java b/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
new file mode 100644
index 0000000..7eb6208
--- /dev/null
+++ b/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
@@ -0,0 +1,56 @@
+package org.apache.commons.cli.bug;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test for CLI-265.
+ * <p>
+ * The issue is that a short option with an optional value will use whatever comes next as value.
+ */
+public class BugCLI265Test {
+
+ private DefaultParser parser;
+ private Options options;
+
+ @Before
+ public void setUp() throws Exception {
+ parser = new DefaultParser();
+
+ Option TYPE1 = Option.builder("t1").hasArg().numberOfArgs(1).optionalArg(true).argName("t1_path").build();
+ Option LAST = Option.builder("last").hasArg(false).build();
+
+ options = new Options().addOption(TYPE1).addOption(LAST);
+ }
+
+ @Test
+ public void shouldParseShortOptionWithValue() throws Exception {
+ String[] shortOptionWithValue = new String[]{"-t1", "path/to/my/db"};
+
+ final CommandLine commandLine = parser.parse(options, shortOptionWithValue);
+
+ assertEquals("path/to/my/db", commandLine.getOptionValue("t1"));
+ assertFalse(commandLine.hasOption("last"));
+ }
+
+ @Test
+ public void shouldParseShortOptionWithoutValue() throws Exception {
+ String[] twoShortOptions = new String[]{"-t1", "-last"};
+
+ final CommandLine commandLine = parser.parse(options, twoShortOptions);
+
+ assertTrue(commandLine.hasOption("t1"));
+ assertNotEquals("Second option has been used as value for first option", "-last", commandLine.getOptionValue("t1"));
+ assertTrue("Second option has not been detected", commandLine.hasOption("last"));
+ }
+
+}