/usr/include/ola/base/FlagsPrivate.h is in libola-dev 0.9.1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | /*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* FlagsPrivate.h
* Command line flag (option) handling.
* Copyright (C) 2013 Simon Newton
*/
/**
* @addtogroup flags
* @{
* @file FlagsPrivate.h
*/
#ifndef INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
#define INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
#include <getopt.h>
#include <string.h>
#include <ola/StringUtils.h>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
namespace ola {
/**
* @brief The interface for the Flag classes.
*/
class FlagInterface {
public:
virtual ~FlagInterface() {}
virtual const char* name() const = 0;
virtual char short_opt() const = 0;
virtual bool has_arg() const = 0;
virtual const char* arg_type() const = 0;
virtual std::string help() const = 0;
virtual bool SetValue(const std::string &input) = 0;
};
/**
* @brief The common implementation.
*/
class BaseFlag : public FlagInterface {
public:
BaseFlag(const char *arg_type, const char *short_opt, const char *help)
: m_arg_type(arg_type),
m_short_opt(short_opt[0]),
m_help(help) {
}
char short_opt() const { return m_short_opt; }
const char* arg_type() const { return m_arg_type; }
std::string help() const { return m_help; }
protected:
void ReplaceUnderscoreWithHyphen(char *input);
const char* NewCanonicalName(const char *name);
private:
const char *m_arg_type;
char m_short_opt;
const char *m_help;
};
/**
* @brief A templated Flag class.
* @tparam T the type of the flag.
*/
template <typename T>
class Flag : public BaseFlag {
public:
Flag(const char *name, const char *arg_type, const char *short_opt,
T default_value, const char *help)
: BaseFlag(arg_type, short_opt, help),
m_name(name),
m_default(default_value),
m_value(default_value) {
m_name = NewCanonicalName(name);
}
const char *name() const { return m_name; }
bool has_arg() const { return true; }
bool default_value() const { return m_default; }
operator T() const { return m_value; }
Flag &operator=(T v) {
m_value = v;
return *this;
}
bool SetValue(const std::string &input);
private:
const char *m_name;
T m_default;
T m_value;
};
/**
* @brief a bool flag
*/
template<>
class Flag<bool> : public BaseFlag {
public:
Flag(const char *name, const char *arg_type, const char *short_opt,
bool default_value, const char *help)
: BaseFlag(arg_type, short_opt, help),
m_name(name),
m_default(default_value),
m_value(default_value) {
if (default_value) {
// prefix the long option with 'no'
size_t total_size = strlen(NO_PREFIX) + strlen(name) + 1;
char* new_name = new char[total_size];
strncpy(new_name, NO_PREFIX, strlen(NO_PREFIX) + 1);
strncat(new_name, name, total_size);
ReplaceUnderscoreWithHyphen(new_name);
m_name = new_name;
} else {
m_name = NewCanonicalName(name);
}
}
const char *name() const { return m_name; }
bool has_arg() const { return false; }
bool default_value() const { return m_default; }
operator bool() const { return m_value; }
Flag &operator=(bool v) {
m_value = v;
return *this;
}
bool SetValue(const std::string&) {
m_value = !m_default;
return true;
}
private:
const char *m_name;
bool m_default;
bool m_value;
static const char NO_PREFIX[];
};
/**
* @brief a string flag
*/
template<>
class Flag<std::string> : public BaseFlag {
public:
Flag(const char *name, const char *arg_type, const char *short_opt,
std::string default_value, const char *help)
: BaseFlag(arg_type, short_opt, help),
m_name(name),
m_default(default_value),
m_value(default_value) {
m_name = NewCanonicalName(name);
}
const char *name() const { return m_name; }
bool has_arg() const { return true; }
std::string default_value() const { return m_default; }
const char* arg_type() const { return "string"; }
operator const char*() const { return m_value.c_str(); }
operator std::string() const { return m_value; }
std::string str() const { return m_value; }
Flag &operator=(const std::string &v) {
m_value = v;
return *this;
}
bool SetValue(const std::string &input) {
m_value = input;
return true;
}
private:
const char *m_name;
std::string m_default;
std::string m_value;
};
/**
* @brief Used to set the value of a flag
*/
template <typename T>
bool Flag<T>::SetValue(const std::string &input) {
return ola::StringToInt(input, &m_value, true);
}
/**
* @brief This class holds all the flags, and is responsbile for parsing the
* command line.
*/
class FlagRegistry {
public:
FlagRegistry() {}
void RegisterFlag(FlagInterface *flag);
void ParseFlags(int *argc, char **argv);
void SetFirstLine(const std::string &help);
void SetDescription(const std::string &help);
void DisplayUsage();
void DisplayVersion();
void GenManPage();
private:
typedef std::map<std::string, FlagInterface*> LongOpts;
typedef std::map<char, FlagInterface*> ShortOpts;
typedef std::map<int, FlagInterface*> FlagMap;
// <flag, description>
typedef std::pair<std::string, std::string> OptionPair;
LongOpts m_long_opts;
ShortOpts m_short_opts;
std::string m_argv0;
std::string m_first_line;
std::string m_description;
std::string GetShortOptsString() const;
struct option *GetLongOpts(FlagMap *flag_map);
void PrintFlags(std::vector<std::string> *lines);
void PrintManPageFlags(std::vector<OptionPair> *lines);
};
/**
* @brief Get the global FlagRegistry.
*/
FlagRegistry *GetRegistry();
/**
* @brief This class is responsible for registering a flag
*/
class FlagRegisterer {
public:
explicit FlagRegisterer(FlagInterface *flag) {
GetRegistry()->RegisterFlag(flag);
}
FlagRegisterer(FlagInterface *flag, char *short_opt) {
*short_opt = flag->short_opt();
GetRegistry()->RegisterFlag(flag);
}
};
} // namespace ola
/**
* @brief Declare a flag which was defined in another file.
*/
#define DECLARE_flag(type, name) \
namespace ola_flags { extern ola::Flag<type> FLAGS_##name; } \
using ola_flags::FLAGS_##name;
/**
* @brief Generic macro to define a flag
*/
#define DEFINE_flag(type, name, short_opt, default_value, help_str) \
namespace ola_flags { \
ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
help_str); \
ola::FlagRegisterer flag_registerer_##name(&FLAGS_##name); \
} \
using ola_flags::FLAGS_##name
/**
* @brief Generic macro to define a flag with a short option.
*/
#define DEFINE_flag_with_short(type, name, short_opt, default_value, help_str) \
namespace ola_flags { char flag_short_##short_opt = 0; } \
namespace ola_flags { \
ola::Flag<type> FLAGS_##name(#name, #type, #short_opt, default_value, \
help_str); \
ola::FlagRegisterer flag_registerer_##name( \
&FLAGS_##name, &flag_short_##short_opt); \
} \
using ola_flags::FLAGS_##name
#endif // INCLUDE_OLA_BASE_FLAGSPRIVATE_H_
/**@}*/
|