I use gFlags in a c++ application, to collect command line flags:
DEFINE_string("output_dir", ".", "existing directory to dump output");
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(argc, argv, true);
...
}
This flag has a default value, so the user may choose not to provide the same on the command line. Is there any API in gFlags to know if the flag was supplied in the command line? I did not find any, so using the following hack:
DEFINE_string("output_dir", ".", "existing directory to dump output");
static bool flag_set = false;
static void CheckFlags(const int argc, char** const argv) {
for (int i = 0; i < argc; i++) {
if (string(argv[i]).find("output_dir") != string::npos) {
flag_set = true;
break;
}
}
}
int main(int argc, char** argv) {
CheckFlags(argc, argv);
gflags::ParseCommandLineFlags(argc, argv, true);
if (flag_set) {
// blah.. blah..
}
return 0;
}
After studying the gflags code in detail, I found an API
gflags::GetCommandLineFlagInfoOrDie(const char* name)which returnsCommandLineFlagInfo, which contains a boolean flag namedis_defaultwhich is false if the flag was provided in the command line:So I do not need the hack any more: