From 260c39871b4d02f4fabe701b344a44cd8f9d85ca Mon Sep 17 00:00:00 2001 From: Edward Lemur Date: Wed, 7 Feb 2018 15:45:40 +0100 Subject: [PATCH] Add support for hyphens to rtc_base/flags Make it possible to specify flags both with hyphens (--flag-name) and underscores (--flag_name). Bug: None Change-Id: Ic02cdc2d5b9f7c75d06cdb6287a86ed432fd9daa Reviewed-on: https://webrtc-review.googlesource.com/49204 Commit-Queue: Edward Lemur Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#21945} --- rtc_base/flags.cc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/rtc_base/flags.cc b/rtc_base/flags.cc index b4137981e3..a2fb7089b0 100644 --- a/rtc_base/flags.cc +++ b/rtc_base/flags.cc @@ -22,6 +22,23 @@ #include #endif + +namespace { +bool FlagEq(const char* arg, const char* flag) { + // Compare two flags for equality. + // 'arg' is the name of a flag passed via the command line and 'flag' is the + // name of a flag defined with the DEFINE_* macros. + // We compare the flags for equality, considering hyphens (-) and + // underscores (_) to be equivalent, so that --flag-name and --flag_name both + // match with --flag_name. + while (*arg != '\0' && (*arg == *flag || (*arg == '-' && *flag == '_'))) { + ++arg; + ++flag; + } + return *arg == '\0' && *flag == '\0'; +} +} // namespace + namespace rtc { // ----------------------------------------------------------------------------- // Implementation of Flag @@ -131,7 +148,7 @@ void FlagList::Print(const char* file, bool print_current_value) { Flag* FlagList::Lookup(const char* name) { Flag* f = list_; - while (f != nullptr && strcmp(name, f->name()) != 0) + while (f != nullptr && !FlagEq(name, f->name())) f = f->next(); return f; }