Convert dashes to underscores for unknown flags (#177)
authorTodd Lipcon <todd@cloudera.com>
Wed, 12 Oct 2016 17:23:42 +0000 (10:23 -0700)
committerAndreas Schuh <andreas.schuh.84@gmail.com>
Tue, 22 Nov 2016 00:11:14 +0000 (00:11 +0000)
src/gflags.cc
test/gflags_unittest.cc

index fe5300f..42dcd04 100644 (file)
@@ -771,7 +771,12 @@ void FlagRegistry::RegisterFlag(CommandLineFlag* flag) {
 CommandLineFlag* FlagRegistry::FindFlagLocked(const char* name) {
   FlagConstIterator i = flags_.find(name);
   if (i == flags_.end()) {
-    return NULL;
+    // If the name has dashes in it, try again after replacing with
+    // underscores.
+    if (strchr(name, '-') == NULL) return NULL;
+    string name_rep = name;
+    std::replace(name_rep.begin(), name_rep.end(), '-', '_');
+    return FindFlagLocked(name_rep.c_str());
   } else {
     return i->second;
   }
index 47dfd3c..9a922ef 100755 (executable)
@@ -357,6 +357,19 @@ TEST(FlagFileTest, ReadFlagsFromString) {
       false,
       123,
       123.0);
+
+  // Test that flags can use dashes instead of underscores.
+  TestFlagString(
+      // Flag string
+      "-test-string=initial\n"
+      "--test-bool=false\n"
+      "--test-int32=123\n"
+      "--test-double=123.0\n",
+      // Expected values
+      "initial",
+      false,
+      123,
+      123.0);
 }
 
 // Tests the filename part of the flagfile