1 #include "c10/util/Flags.h" 2 #include "c10/macros/Macros.h" 15 C10_DEFINE_REGISTRY(C10FlagsRegistry, C10FlagParser,
const string&);
18 static bool gCommandLineFlagsParsed =
false;
22 std::stringstream& GlobalInitStream() {
23 static std::stringstream ss;
26 static const char* gUsageMessage =
"(Usage message not set.)";
30 static string usage_message_safe_copy = str;
31 gUsageMessage = usage_message_safe_copy.c_str();
43 GlobalInitStream() <<
"Parsing commandline arguments for c10." << std::endl;
46 for (
int i = 1; i < *pargc; ++i) {
49 if (arg.find(
"--help") != string::npos) {
52 std::cout <<
"Arguments: " << std::endl;
53 for (
const auto& help_msg : C10FlagsRegistry()->HelpMessage()) {
54 std::cout <<
" " << help_msg.first <<
": " << help_msg.second
60 if (arg[0] !=
'-' || arg[1] !=
'-') {
62 <<
"C10 flag: commandline argument does not match --name=var " 64 << arg <<
". Ignoring this argument." << std::endl;
65 argv[write_head++] = argv[i];
71 size_t prefix_idx = arg.find(
'=');
72 if (prefix_idx == string::npos) {
75 key = arg.substr(2, arg.size() - 2);
79 <<
"C10 flag: reached the last commandline argument, but " 80 "I am expecting a value for " 85 value = string(argv[i]);
89 key = arg.substr(2, prefix_idx - 2);
90 value = arg.substr(prefix_idx + 1, string::npos);
93 if (!C10FlagsRegistry()->Has(key)) {
94 GlobalInitStream() <<
"C10 flag: unrecognized commandline argument: " 99 std::unique_ptr<C10FlagParser> parser(
100 C10FlagsRegistry()->Create(key, value));
101 if (!parser->success()) {
102 GlobalInitStream() <<
"C10 flag: illegal argument: " << arg << std::endl;
108 gCommandLineFlagsParsed =
true;
115 std::cerr << GlobalInitStream().str();
118 GlobalInitStream().str(std::string());
123 return gCommandLineFlagsParsed;
127 C10_EXPORT
bool C10FlagParser::Parse<string>(
128 const string& content,
135 C10_EXPORT
bool C10FlagParser::Parse<int>(
const string& content,
int* value) {
137 *value = std::atoi(content.c_str());
140 GlobalInitStream() <<
"C10 flag error: Cannot convert argument to int: " 141 << content << std::endl;
147 C10_EXPORT
bool C10FlagParser::Parse<int64_t>(
148 const string& content,
151 static_assert(
sizeof(
long long) ==
sizeof(int64_t),
"");
154 *value = atoll(content.c_str());
156 *value = std::atoll(content.c_str());
160 GlobalInitStream() <<
"C10 flag error: Cannot convert argument to int: " 161 << content << std::endl;
167 C10_EXPORT
bool C10FlagParser::Parse<double>(
168 const string& content,
171 *value = std::atof(content.c_str());
174 GlobalInitStream() <<
"C10 flag error: Cannot convert argument to double: " 175 << content << std::endl;
181 C10_EXPORT
bool C10FlagParser::Parse<bool>(
const string& content,
bool* value) {
182 if (content ==
"false" || content ==
"False" || content ==
"FALSE" ||
187 content ==
"true" || content ==
"True" || content ==
"TRUE" ||
193 <<
"C10 flag error: Cannot convert argument to bool: " << content
195 <<
"Note that if you are passing in a bool flag, you need to " 196 "explicitly specify it, like --arg=True or --arg True. Otherwise, " 197 "the next argument may be inadvertently used as the argument, " 198 "causing the above error." 206 #endif // C10_USE_GFLAGS C10_API void SetUsageMessage(const std::string &str)
Sets the usage message when a commandline tool is called with "--help".
C10_API const char * UsageMessage()
Returns the usage message for the commandline tool set by SetUsageMessage.
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
C10_API bool CommandLineFlagsHasBeenParsed()
Checks if the commandline flags has already been passed.
C10_API bool ParseCommandLineFlags(int *pargc, char ***pargv)
Parses the commandline flags.