30 #include <string_view> 52 #define DECLARE_bool(name) extern bool FST_FLAGS_ ## name 53 #define DECLARE_string(name) extern std::string FST_FLAGS_##name 54 #define DECLARE_int32(name) extern int32_t FST_FLAGS_##name 55 #define DECLARE_int64(name) extern int64_t FST_FLAGS_##name 56 #define DECLARE_uint64(name) extern uint64_t FST_FLAGS_##name 57 #define DECLARE_double(name) extern double FST_FLAGS_ ## name 62 std::string_view file,
const T val)
86 auto it = flag_table_.find(name);
87 return it != flag_table_.end() ? it->second : 0;
92 flag_table_.insert(std::make_pair(name, desc));
96 if (val ==
"true" || val ==
"1" || val.empty()) {
99 }
else if (val ==
"false" || val ==
"0") {
114 *address = strtol(val.c_str(), &p, 0);
115 return !val.empty() && *p ==
'\0';
120 *address = strtoll(val.c_str(), &p, 0);
121 return !val.empty() && *p ==
'\0';
126 *address = strtoull(val.c_str(), &p, 0);
127 return !val.empty() && *p ==
'\0';
132 *address = strtod(val.c_str(), &p);
133 return !val.empty() && *p ==
'\0';
136 bool SetFlag(
const std::string &arg,
const std::string &val)
const {
137 for (
const auto &kv : flag_table_) {
138 const auto &name = kv.first;
146 std::set<std::pair<std::string, std::string>> *usage_set)
const {
147 for (
auto it = flag_table_.begin(); it != flag_table_.end(); ++it) {
148 const auto &name = it->first;
150 std::string usage =
" --" + name;
151 usage +=
": type = ";
153 usage +=
", default = ";
156 usage_set->insert(std::make_pair(std::string(desc.
file_name), usage));
162 return default_value ?
"true" :
"false";
165 std::string GetDefault(
const std::string &default_value)
const {
166 return "\"" + default_value +
"\"";
170 std::string GetDefault(
const V &default_value)
const {
171 std::ostringstream strm;
177 std::map<std::string, FlagDescription<T>> flag_table_;
180 template <
typename T>
185 registr->SetDescription(name, desc);
194 #define DEFINE_VAR(type, name, value, doc) \ 195 type FST_FLAGS_ ## name = value; \ 196 static FlagRegisterer<type> \ 197 name ## _flags_registerer(#name, FlagDescription<type>(&FST_FLAGS_ ## name, \ 203 #define DEFINE_bool(name, value, doc) DEFINE_VAR(bool, name, value, doc) 204 #define DEFINE_string(name, value, doc) \ 205 DEFINE_VAR(std::string, name, value, doc) 206 #define DEFINE_int32(name, value, doc) DEFINE_VAR(int32_t, name, value, doc) 207 #define DEFINE_int64(name, value, doc) DEFINE_VAR(int64_t, name, value, doc) 208 #define DEFINE_uint64(name, value, doc) DEFINE_VAR(uint64_t, name, value, doc) 209 #define DEFINE_double(name, value, doc) DEFINE_VAR(double, name, value, doc) 215 void SetFlags(
const char *usage,
int *argc,
char ***argv,
bool remove_flags,
216 const char *src =
"");
219 template <
typename Type,
typename Value>
226 #define SET_FLAGS(usage, argc, argv, rmflags) \ 227 std::set_new_handler(FailedNewHandler); \ 228 SetFlags(usage, argc, argv, rmflags, __FILE__) 231 inline void InitFst(
const char *usage,
int *argc,
char ***argv,
bool rmflags) {
232 return SetFlags(usage, argc, argv, rmflags);
237 #endif // FST_FLAGS_H_ void ShowUsage(bool long_usage=true)
std::string_view doc_string
void InitFst(const char *usage, int *argc, char ***argv, bool rmflags)
bool SetFlag(const std::string &val, double *address) const
bool SetFlag(const std::string &val, std::string *address) const
bool SetFlag(const std::string &val, bool *address) const
const FlagDescription< T > & GetFlagDescription(const std::string &name) const
void SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags, const char *src="")
static FlagRegister< T > * GetRegister()
bool SetFlag(const std::string &val, int64_t *address) const
std::string_view file_name
FlagDescription(T *addr, std::string_view doc, std::string_view type, std::string_view file, const T val)
bool SetFlag(const std::string &arg, const std::string &val) const
void GetUsage(std::set< std::pair< std::string, std::string >> *usage_set) const
#define DECLARE_string(name)
FlagRegisterer(const std::string &name, const FlagDescription< T > &desc)
std::string_view type_name
bool SetFlag(const std::string &val, int32_t *address) const
void SetDescription(const std::string &name, const FlagDescription< T > &desc)
void SetFlag(Type *flag, Value value)
bool SetFlag(const std::string &val, uint64_t *address) const