Fix incorrect handling of "option=value value2" in OptionHelper. Discovered
when trying to use --retry with an --exclude-filter on the command line.

Bug: 111841241
Test: run sts-engbuild --retry x, where x is a session with command line
--exclude-filter

Change-Id: Ie6a719c7576637e998fef34aaed82ce4d06bc1f4
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/util/OptionHelper.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/util/OptionHelper.java
index 8847f92..3c221dd 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/util/OptionHelper.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/util/OptionHelper.java
@@ -118,10 +118,15 @@
         while (matcher.find()) {
             String optionInput = cleanNameValueDelimiter(matcher.group());
             // split between the option name and value
-            String[] keyNameTokens = optionInput.split(" ", 2);
+            String[] keyNameTokens = optionInput.split("[ =]", 2);
             // remove initial hyphens and any starting double quote from option args
             String keyName = keyNameTokens[0].replaceFirst("^\"?--?", "");
 
+            // Convert "option=value a b" back into option="value a b"
+            if (optionInput.charAt(0) == '"') {
+              optionInput = keyNameTokens[0].substring(1) + " \"" + keyNameTokens[1];
+            }
+
             // add substrings only when the options are recognized
             if (optionShortNames.contains(keyName) || optionNames.contains(keyName)) {
                 // add values separated by spaces or in quotes separately to the return array
diff --git a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/util/OptionHelperTest.java b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/util/OptionHelperTest.java
index dde28de..a4344b0 100644
--- a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/util/OptionHelperTest.java
+++ b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/util/OptionHelperTest.java
@@ -87,7 +87,7 @@
             "--" + TEST_LOGPATH, "path/to/log-directory/");
 
         List<String> validQuoteSubset = Arrays.asList("-" + TEST_CLASS_SHORTNAME, fakeTestClass,
-            "--" + TEST_NAME + " " + fakeTestMethod, "--" + TEST_FILTER, fakeTestClass + " "
+            "--" + TEST_NAME, fakeTestMethod, "--" + TEST_FILTER, fakeTestClass + " "
             + fakeTestMethod);
         String[] inputArray = {"foocts ", "-", TEST_CLASS_SHORTNAME, " ", fakeTestClass, " \"--",
             TEST_NAME, "=", fakeTestMethod, "\" -z \"FAKE1 FAKE2\" --", TEST_FILTER, " \"",
@@ -107,4 +107,14 @@
             OptionHelper.getValidCliArgs(inputString, this));
     }
 
+    public void testGetValidCliArgs_surroundingQuotes() throws Exception {
+      List<String> expectedTokens = Arrays.asList("--" + TEST_FILTER, "a b c");
+      String[] inputArray = {"\"--", TEST_FILTER, "=a b c\""};
+      String inputString = String.join("", inputArray);
+
+      assertEquals("Expected matching arrays", expectedTokens,
+          OptionHelper.getValidCliArgs(inputString, this));
+
+    }
+
 }