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);
166 const char *
name()
const {
return m_name; }
168 bool default_value()
const {
return m_default; }
170 operator T()
const {
return m_value; }
172 Flag &operator=(T v) {
177 bool SetValue(
const std::string &input);
192 bool default_value,
const char *
help,
const bool has_arg)
193 :
BaseFlag(arg_type, short_opt, help),
195 m_default(default_value),
196 m_value(default_value),
198 if (!has_arg && default_value) {
200 size_t prefix_size = strlen(NO_PREFIX);
201 size_t name_size = strlen(name);
202 char* new_name =
new char[prefix_size + name_size + 1];
203 memcpy(new_name, NO_PREFIX, prefix_size);
204 memcpy(new_name + prefix_size, name, name_size);
205 new_name[prefix_size + name_size] = 0;
206 ReplaceUnderscoreWithHyphen(new_name);
209 m_name = NewCanonicalName(name);
217 const char *
name()
const {
return m_name; }
219 bool default_value()
const {
return m_default; }
221 operator bool()
const {
return m_value; }
223 Flag &operator=(
bool v) {
233 m_value = !m_default;
244 static const char NO_PREFIX[];
254 std::string default_value,
const char *
help,
256 :
BaseFlag(arg_type, short_opt, help),
258 m_default(default_value),
259 m_value(default_value) {
260 m_name = NewCanonicalName(name);
267 const char *
name()
const {
return m_name; }
269 std::string default_value()
const {
return m_default; }
272 operator const char*()
const {
return m_value.c_str(); }
273 operator std::string()
const {
return m_value; }
274 std::string str()
const {
return m_value; }
276 Flag &operator=(
const std::string &v) {
289 std::string m_default;
296 template <
typename T>
312 void ParseFlags(
int *argc,
char **argv);
314 void SetFirstLine(
const std::string &help);
315 void SetDescription(
const std::string &help);
317 void DisplayVersion();
321 typedef std::map<std::string, FlagInterface*> LongOpts;
322 typedef std::map<char, FlagInterface*> ShortOpts;
323 typedef std::map<int, FlagInterface*> FlagMap;
325 typedef std::pair<std::string, std::string> OptionPair;
327 LongOpts m_long_opts;
328 ShortOpts m_short_opts;
330 std::string m_first_line;
331 std::string m_description;
333 std::string GetShortOptsString()
const;
334 struct option *GetLongOpts(FlagMap *flag_map);
335 void PrintFlags(std::vector<std::string> *lines);
336 void PrintManPageFlags(std::vector<OptionPair> *lines);
370 #define DECLARE_flag(type, name) \
371 namespace ola_flags { extern ola::Flag<type> FLAGS_##name; } \
372 using ola_flags::FLAGS_##name;
377 #define DEFINE_flag(type, name, short_opt, default_value, help_str, \
379 namespace ola_flags { \
380 ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
381 help_str, has_arg); \
382 ola::FlagRegisterer flag_registerer_##name(&FLAGS_##name); \
384 using ola_flags::FLAGS_##name
389 #define DEFINE_flag_with_short(type, name, short_opt, default_value, help_str, \
391 namespace ola_flags { char flag_short_##short_opt = 0; } \
392 namespace ola_flags { \
393 ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
394 help_str, has_arg); \
395 ola::FlagRegisterer flag_registerer_##name( \
396 &FLAGS_##name, &flag_short_##short_opt); \
398 using ola_flags::FLAGS_##name
405 #endif // INCLUDE_OLA_BASE_FLAGSPRIVATE_H_