Make Search{Unary,Nullary}Option skip the rest of the args if "--" is found.

--
PiperOrigin-RevId: 151145409
MOS_MIGRATED_REVID=151145409
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc
index 08f01a2..c45aabc 100644
--- a/src/main/cpp/blaze_util.cc
+++ b/src/main/cpp/blaze_util.cc
@@ -87,6 +87,9 @@
 
   vector<string>::size_type i = 0;
   for (; i < args.size() - 1; ++i) {
+    if (args[i] == "--") {
+      return NULL;
+    }
     const char* result = GetUnaryOption(args[i].c_str(),
                                         args[i + 1].c_str(),
                                         key);
@@ -99,6 +102,9 @@
 
 bool SearchNullaryOption(const vector<string>& args, const char *key) {
   for (vector<string>::size_type i = 0; i < args.size(); i++) {
+    if (args[i] == "--") {
+      return false;
+    }
     if (GetNullaryOption(args[i].c_str(), key)) {
       return true;
     }
diff --git a/src/main/cpp/blaze_util.h b/src/main/cpp/blaze_util.h
index 3c472da..f320c80 100644
--- a/src/main/cpp/blaze_util.h
+++ b/src/main/cpp/blaze_util.h
@@ -55,12 +55,14 @@
 // Returns false otherwise.
 bool GetNullaryOption(const char *arg, const char *key);
 
-// Searches for 'key' in 'args' using GetUnaryOption.
+// Searches for 'key' in 'args' using GetUnaryOption. Arguments found after '--'
+// are omitted from the search.
 // Returns true iff key is a flag in args.
 const char* SearchUnaryOption(const std::vector<std::string>& args,
                               const char* key);
 
-// Searches for 'key' in 'args' using GetNullaryOption.
+// Searches for 'key' in 'args' using GetNullaryOption. Arguments found after
+// '--' are omitted from the search.
 // Returns the value of the 'key' flag iff it occurs in args.
 bool SearchNullaryOption(const std::vector<std::string>& args,
                          const char* key);
diff --git a/src/test/cpp/blaze_util_test.cc b/src/test/cpp/blaze_util_test.cc
index 3662cdf..e20d50d 100644
--- a/src/test/cpp/blaze_util_test.cc
+++ b/src/test/cpp/blaze_util_test.cc
@@ -190,6 +190,16 @@
                                   "--flag"));
 }
 
+TEST_F(BlazeUtilTest, TestSearchNullarySkipsAfterDashDash) {
+  ASSERT_FALSE(SearchNullaryOption(
+      {"bazel", "build", ":target", "--", "--flag"}, "--flag"));
+}
+
+TEST_F(BlazeUtilTest, TestSearchNullarySucceedsWithEqualsAndDashDash) {
+  ASSERT_FALSE(SearchNullaryOption(
+      {"bazel", "build", ":target", "--", "--flag=value"}, "--flag"));
+}
+
 TEST_F(BlazeUtilTest, TestSearchUnaryForEmpty) {
   ASSERT_STREQ(nullptr, SearchUnaryOption({"bazel", "build", ":target"}, ""));
 }
@@ -223,4 +233,18 @@
                    {"bazel", "build", ":target", "--flag=value"}, "--flag"));
 }
 
+TEST_F(BlazeUtilTest, TestSearchUnarySkipsAfterDashDashWithEquals) {
+  ASSERT_STREQ(nullptr,
+               SearchUnaryOption(
+                   {"bazel", "build", ":target", "--", "--flag", "value"},
+                   "--flag"));
+}
+
+TEST_F(BlazeUtilTest, TestSearchUnarySkipsAfterDashDashWithoutEquals) {
+  ASSERT_STREQ(nullptr,
+               SearchUnaryOption(
+                   {"bazel", "build", ":target", "--", "--flag=value"},
+                   "--flag"));
+}
+
 }  // namespace blaze