29 #ifndef INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
30 #define INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
59 virtual const char*
name()
const = 0;
69 virtual bool has_arg()
const = 0;
74 virtual const char*
arg_type()
const = 0;
79 virtual std::string
help()
const = 0;
86 virtual bool present()
const = 0;
93 virtual bool SetValue(
const std::string &input) = 0;
108 : m_arg_type(arg_type),
109 m_short_opt(short_opt[0]),
115 const char*
arg_type()
const {
return m_arg_type; }
116 std::string
help()
const {
return m_help; }
125 void ReplaceUnderscoreWithHyphen(
char *input);
126 const char* NewCanonicalName(
const char *
name);
129 const char *m_arg_type;
139 template <
typename T>
153 T default_value,
const char *
help,
155 :
BaseFlag(arg_type, short_opt, help),
157 m_default(default_value),
158 m_value(default_value) {
159 m_name = NewCanonicalName(name);
162 const char *
name()
const {
return m_name; }
164 bool default_value()
const {
return m_default; }
166 operator T()
const {
return m_value; }
168 Flag &operator=(T v) {
173 bool SetValue(
const std::string &input);
188 bool default_value,
const char *
help,
const bool has_arg)
189 :
BaseFlag(arg_type, short_opt, help),
191 m_default(default_value),
192 m_value(default_value),
194 if (!has_arg && default_value) {
196 size_t total_size = strlen(NO_PREFIX) + strlen(name) + 1;
197 char* new_name =
new char[total_size];
198 strncpy(new_name, NO_PREFIX, strlen(NO_PREFIX) + 1);
199 strncat(new_name, name, total_size);
200 ReplaceUnderscoreWithHyphen(new_name);
203 m_name = NewCanonicalName(name);
207 const char *
name()
const {
return m_name; }
209 bool default_value()
const {
return m_default; }
211 operator bool()
const {
return m_value; }
213 Flag &operator=(
bool v) {
223 m_value = !m_default;
234 static const char NO_PREFIX[];
244 std::string default_value,
const char *
help,
246 :
BaseFlag(arg_type, short_opt, help),
248 m_default(default_value),
249 m_value(default_value) {
250 m_name = NewCanonicalName(name);
253 const char *
name()
const {
return m_name; }
255 std::string default_value()
const {
return m_default; }
258 operator const char*()
const {
return m_value.c_str(); }
259 operator std::string()
const {
return m_value; }
260 std::string str()
const {
return m_value; }
262 Flag &operator=(
const std::string &v) {
275 std::string m_default;
282 template <
typename T>
298 void ParseFlags(
int *argc,
char **argv);
300 void SetFirstLine(
const std::string &help);
301 void SetDescription(
const std::string &help);
303 void DisplayVersion();
307 typedef std::map<std::string, FlagInterface*> LongOpts;
308 typedef std::map<char, FlagInterface*> ShortOpts;
309 typedef std::map<int, FlagInterface*> FlagMap;
311 typedef std::pair<std::string, std::string> OptionPair;
313 LongOpts m_long_opts;
314 ShortOpts m_short_opts;
316 std::string m_first_line;
317 std::string m_description;
319 std::string GetShortOptsString()
const;
320 struct option *GetLongOpts(FlagMap *flag_map);
321 void PrintFlags(std::vector<std::string> *lines);
322 void PrintManPageFlags(std::vector<OptionPair> *lines);
356 #define DECLARE_flag(type, name) \
357 namespace ola_flags { extern ola::Flag<type> FLAGS_##name; } \
358 using ola_flags::FLAGS_##name;
363 #define DEFINE_flag(type, name, short_opt, default_value, help_str, \
365 namespace ola_flags { \
366 ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
367 help_str, has_arg); \
368 ola::FlagRegisterer flag_registerer_##name(&FLAGS_##name); \
370 using ola_flags::FLAGS_##name
375 #define DEFINE_flag_with_short(type, name, short_opt, default_value, help_str, \
377 namespace ola_flags { char flag_short_##short_opt = 0; } \
378 namespace ola_flags { \
379 ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
380 help_str, has_arg); \
381 ola::FlagRegisterer flag_registerer_##name( \
382 &FLAGS_##name, &flag_short_##short_opt); \
384 using ola_flags::FLAGS_##name
391 #endif // INCLUDE_OLA_BASE_FLAGSPRIVATE_H_