Author: britter
Date: Wed Sep 7 20:19:32 2016
New Revision: 1759695
URL: http://svn.apache.org/viewvc?rev=1759695&view=rev
Log:
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.
Added:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
(with props)
Modified:
commons/proper/cli/trunk/src/changes/changes.xml
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
Modified: commons/proper/cli/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/changes/changes.xml?rev=1759695&r1=1759694&r2=1759695&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/changes/changes.xml (original)
+++ commons/proper/cli/trunk/src/changes/changes.xml Wed Sep 7 20:19:32 2016
@@ -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>
Modified:
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java?rev=1759695&r1=1759694&r2=1759695&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
(original)
+++
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
Wed Sep 7 20:19:32 2016
@@ -299,7 +299,15 @@ public class DefaultParser implements Co
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);
}
/**
Added:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java?rev=1759695&view=auto
==============================================================================
---
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
(added)
+++
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
Wed Sep 7 20:19:32 2016
@@ -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"));
+ }
+
+}
Propchange:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
------------------------------------------------------------------------------
svn:eol-style = native