6 #include "caffe2/core/logging.h" 10 #ifdef CAFFE2_USE_GFLAGS 17 gflags::SetUsageMessage(str);
21 return gflags::ProgramUsage();
25 if (*pargc == 0)
return true;
26 return gflags::ParseCommandLineFlags(pargc, pargv,
true);
34 #else // CAFFE2_USE_GFLAGS 37 CAFFE_DEFINE_REGISTRY(Caffe2FlagsRegistry, Caffe2FlagParser,
const string&);
40 static bool gCommandLineFlagsParsed =
false;
44 std::stringstream& GlobalInitStream() {
45 static std::stringstream ss;
48 static string gUsageMessage =
"(Usage message not set.)";
56 if (*pargc == 0)
return true;
59 GlobalInitStream() <<
"Parsing commandline arguments for caffe2." 63 for (
int i = 1; i < *pargc; ++i) {
66 if (arg ==
"--help") {
69 std::cout <<
"Arguments: " << std::endl;
70 for (
const auto& help_msg : Caffe2FlagsRegistry()->HelpMessage()) {
71 std::cout <<
" " << help_msg.first <<
": " << help_msg.second
77 if (arg[0] !=
'-' || arg[1] !=
'-') {
79 <<
"Caffe2 flag: commandline argument does not match --name=var " 81 << arg <<
". Ignoring this argument." << std::endl;
82 argv[write_head++] = argv[i];
88 int prefix_idx = arg.find(
'=');
89 if (prefix_idx == string::npos) {
92 key = arg.substr(2, arg.size() - 2);
96 <<
"Caffe2 flag: reached the last commandline argument, but " 97 "I am expecting a value for " << arg;
101 value = string(argv[i]);
105 key = arg.substr(2, prefix_idx - 2);
106 value = arg.substr(prefix_idx + 1, string::npos);
109 if (!Caffe2FlagsRegistry()->Has(key)) {
110 GlobalInitStream() <<
"Caffe2 flag: unrecognized commandline argument: " 115 std::unique_ptr<Caffe2FlagParser> parser(
116 Caffe2FlagsRegistry()->Create(key, value));
117 if (!parser->success()) {
118 GlobalInitStream() <<
"Caffe2 flag: illegal argument: " 125 gCommandLineFlagsParsed =
true;
132 std::cerr << GlobalInitStream().str();
135 GlobalInitStream().str(std::string());
140 return gCommandLineFlagsParsed;
144 bool Caffe2FlagParser::Parse<string>(
const string& content,
string* value) {
150 bool Caffe2FlagParser::Parse<int>(
const string& content,
int* value) {
152 *value = std::atoi(content.c_str());
155 GlobalInitStream() <<
"Caffe2 flag error: Cannot convert argument to int: " 156 << content << std::endl;
162 bool Caffe2FlagParser::Parse<int64_t>(
const string& content, int64_t* value) {
164 static_assert(
sizeof(
long long) ==
sizeof(int64_t),
"");
167 *value = atoll(content.c_str());
169 *value = std::atoll(content.c_str());
173 GlobalInitStream() <<
"Caffe2 flag error: Cannot convert argument to int: " 174 << content << std::endl;
180 bool Caffe2FlagParser::Parse<double>(
const string& content,
double* value) {
182 *value = std::atof(content.c_str());
186 <<
"Caffe2 flag error: Cannot convert argument to double: " 187 << content << std::endl;
193 bool Caffe2FlagParser::Parse<bool>(
const string& content,
bool* value) {
194 if (content ==
"false" || content ==
"False" || content ==
"FALSE" ||
198 }
else if (content ==
"true" || content ==
"True" || content ==
"TRUE" ||
204 <<
"Caffe2 flag error: Cannot convert argument to bool: " 205 << content << std::endl
206 <<
"Note that if you are passing in a bool flag, you need to " 207 "explicitly specify it, like --arg=True or --arg True. Otherwise, " 208 "the next argument may be inadvertently used as the argument, " 209 "causing the above error." 215 #endif // CAFFE2_USE_GFLAGS bool ParseCaffeCommandLineFlags(int *pargc, char ***pargv)
Parses the commandline flags.
const char * UsageMessage()
Returns the usage message for the commandline tool set by SetUsageMessage.
void SetUsageMessage(const string &str)
Sets the usage message when a commandline tool is called with "--help".
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
Commandline flags support for Caffe2.
bool CommandLineFlagsHasBeenParsed()
Checks if the commandline flags has already been passed.