summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Lipcon <todd@cloudera.com>2016-10-12 10:23:42 -0700
committerAndreas Schuh <andreas.schuh.84@gmail.com>2016-11-22 00:11:14 +0000
commit14c0e93755d5a32c3d2029d83094564b8823b7b4 (patch)
tree4080086a26f9a4843f2216d093f37884fdd3526a
parentcce68f0c9c5d054017425e6e6fd54f696d36e8ee (diff)
downloadgflags-14c0e93755d5a32c3d2029d83094564b8823b7b4.tar.gz
gflags-14c0e93755d5a32c3d2029d83094564b8823b7b4.tar.bz2
gflags-14c0e93755d5a32c3d2029d83094564b8823b7b4.zip
Convert dashes to underscores for unknown flags (#177)
-rw-r--r--src/gflags.cc7
-rwxr-xr-xtest/gflags_unittest.cc13
2 files changed, 19 insertions, 1 deletions
diff --git a/src/gflags.cc b/src/gflags.cc
index fe5300f..42dcd04 100644
--- a/src/gflags.cc
+++ b/src/gflags.cc
@@ -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;
}
diff --git a/test/gflags_unittest.cc b/test/gflags_unittest.cc
index 47dfd3c..9a922ef 100755
--- a/test/gflags_unittest.cc
+++ b/test/gflags_unittest.cc
@@ -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