29#ifndef INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
30#define INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
59 virtual const char*
name()
const = 0;
79 virtual std::string
help()
const = 0;
93 virtual bool SetValue(
const std::string &input) = 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;
153 T default_value,
const char *
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);
185 const Flag &operator=(
const Flag &) =
delete;
195 bool default_value,
const char *
help,
const bool has_arg)
198 m_default(default_value),
199 m_value(default_value),
201 if (!
has_arg && default_value) {
203 size_t prefix_size = strlen(NO_PREFIX);
204 size_t name_size = strlen(
name);
205 char* new_name =
new char[prefix_size + name_size + 1];
206 memcpy(new_name, NO_PREFIX, prefix_size);
207 memcpy(new_name + prefix_size,
name, name_size);
208 new_name[prefix_size + name_size] = 0;
209 ReplaceUnderscoreWithHyphen(new_name);
212 m_name = NewCanonicalName(
name);
220 const char *
name()
const {
return m_name; }
222 bool default_value()
const {
return m_default; }
224 operator bool()
const {
return m_value; }
226 Flag &operator=(
bool v) {
236 m_value = !m_default;
247 static const char NO_PREFIX[];
250 const Flag &operator=(
const Flag &) =
delete;
260 std::string default_value,
const char *
help,
264 m_default(default_value),
265 m_value(default_value) {
266 m_name = NewCanonicalName(
name);
273 const char *
name()
const {
return m_name; }
275 std::string default_value()
const {
return m_default; }
278 operator const char*()
const {
return m_value.c_str(); }
279 operator std::string()
const {
return m_value; }
280 std::string str()
const {
return m_value; }
282 Flag &operator=(
const std::string &v) {
295 std::string m_default;
299 const Flag &operator=(
const Flag &) =
delete;
321 void ParseFlags(
int *argc,
char **argv);
323 void SetFirstLine(
const std::string &help);
324 void SetDescription(
const std::string &help);
326 void DisplayVersion();
330 typedef std::map<std::string, FlagInterface*> LongOpts;
331 typedef std::map<char, FlagInterface*> ShortOpts;
332 typedef std::map<int, FlagInterface*> FlagMap;
334 typedef std::pair<std::string, std::string> OptionPair;
336 LongOpts m_long_opts;
337 ShortOpts m_short_opts;
339 std::string m_first_line;
340 std::string m_description;
342 std::string GetShortOptsString()
const;
343 struct option *GetLongOpts(FlagMap *flag_map);
344 void PrintFlags(std::vector<std::string> *lines);
345 void PrintManPageFlags(std::vector<OptionPair> *lines);
386#define DECLARE_flag(type, name) \
387 namespace ola_flags { extern ola::Flag<type> FLAGS_##name; } \
388 using ola_flags::FLAGS_##name;
393#define DEFINE_flag(type, name, short_opt, default_value, help_str, \
395 namespace ola_flags { \
396 ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
397 help_str, has_arg); \
398 ola::FlagRegisterer flag_registerer_##name(&FLAGS_##name); \
400 using ola_flags::FLAGS_##name
405#define DEFINE_flag_with_short(type, name, short_opt, default_value, help_str, \
407 namespace ola_flags { char flag_short_##short_opt = 0; } \
408 namespace ola_flags { \
409 ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
410 help_str, has_arg); \
411 ola::FlagRegisterer flag_registerer_##name( \
412 &FLAGS_##name, &flag_short_##short_opt); \
414 using ola_flags::FLAGS_##name
#define OLA_UNUSED
Mark unused arguments & types.
Definition Macro.h:63
Various string utility functions.
The common implementation.
Definition FlagsPrivate.h:99
std::string help() const
Get the flag help string.
Definition FlagsPrivate.h:116
BaseFlag(const char *arg_type, const char *short_opt, const char *help)
Create a new BaseFlag.
Definition FlagsPrivate.h:107
char short_opt() const
Get the flag short option.
Definition FlagsPrivate.h:114
bool present() const
Check if the flag was present on the command line. Good for switching behaviour when a flag is used.
Definition FlagsPrivate.h:117
const char * arg_type() const
Get the flag argument type.
Definition FlagsPrivate.h:115
void MarkAsPresent()
Set that the flag was present on the command line.
Definition FlagsPrivate.h:122
const char * name() const
Get the flag name.
Definition FlagsPrivate.h:220
bool has_arg() const
Whether the flag requires an argument.
Definition FlagsPrivate.h:221
bool SetValue(const std::string &input)
Set the flag value.
Definition FlagsPrivate.h:231
const char * arg_type() const
Get the flag argument type.
Definition FlagsPrivate.h:276
const char * name() const
Get the flag name.
Definition FlagsPrivate.h:273
bool has_arg() const
Whether the flag requires an argument.
Definition FlagsPrivate.h:274
bool SetValue(const std::string &input)
Set the flag value.
Definition FlagsPrivate.h:287
A templated Flag class.
Definition FlagsPrivate.h:140
bool has_arg() const
Whether the flag requires an argument.
Definition FlagsPrivate.h:167
const char * name() const
Get the flag name.
Definition FlagsPrivate.h:166
Flag(const char *name, const char *arg_type, const char *short_opt, T default_value, const char *help, const bool has_arg)
Create a new Flag.
Definition FlagsPrivate.h:152
The interface for the Flag classes.
Definition FlagsPrivate.h:52
virtual const char * arg_type() const =0
Get the flag argument type.
virtual bool SetValue(const std::string &input)=0
Set the flag value.
virtual bool present() const =0
Check if the flag was present on the command line. Good for switching behaviour when a flag is used.
virtual char short_opt() const =0
Get the flag short option.
virtual const char * name() const =0
Get the flag name.
virtual std::string help() const =0
Get the flag help string.
virtual bool has_arg() const =0
Whether the flag requires an argument.
This class is responsible for registering a flag.
Definition FlagsPrivate.h:359
This class holds all the flags, and is responsible for parsing the command line.
Definition FlagsPrivate.h:316
FlagRegistry * GetRegistry()
Get the global FlagRegistry.
bool SetValue(const std::string &input)
Used to set the value of a flag.
Definition FlagsPrivate.h:306
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
bool StringToBoolTolerant(const string &value, bool *output)
Convert a string to a bool in a tolerant way.
Definition StringUtils.cpp:136
bool StringToInt(const string &value, unsigned int *output, bool strict)
Convert a string to a unsigned int.
Definition StringUtils.cpp:155