/usr/include/wibble/commandline/options.h is in libwibble-dev 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | #ifndef WIBBLE_COMMANDLINE_OPTIONS_H
#define WIBBLE_COMMANDLINE_OPTIONS_H
#include <wibble/commandline/core.h>
#include <string>
#include <vector>
namespace wibble {
namespace commandline {
// Types of values for the command line options
struct Bool
{
typedef bool value_type;
static bool parse(const std::string& val);
static bool toBool(const value_type& val);
static int toInt(const value_type& val);
static std::string toString(const value_type& val);
static bool init_val;
};
struct Int
{
typedef int value_type;
static int parse(const std::string& val);
static bool toBool(const value_type& val);
static int toInt(const value_type& val);
static std::string toString(const value_type& val);
static int init_val;
};
struct String
{
typedef std::string value_type;
static std::string parse(const std::string& val);
static bool toBool(const value_type& val);
static int toInt(const value_type& val);
static std::string toString(const value_type& val);
static std::string init_val;
};
struct ExistingFile
{
typedef std::string value_type;
static std::string parse(const std::string& val);
static std::string toString(const value_type& val);
static std::string init_val;
};
/// Interface for a parser for one commandline option
class Option : public Managed
{
std::string m_name;
mutable std::string m_fullUsage;
protected:
bool m_isset;
Option(const std::string& name) : m_name(name), m_isset(false), hidden(false) {}
Option(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
: m_name(name), m_isset(false), usage(usage), description(description), hidden(false)
{
if (shortName != 0)
shortNames.push_back(shortName);
if (!longName.empty())
longNames.push_back(longName);
}
/**
* Parse the next commandline parameter after the short form of the command
* has been found. It may or may not remove the parameter from the list,
* depending on if the option wants a value or not.
*
* Signal that the option has been found, with the given argument (or 0 if
* no argument).
*
* @returns
* true if it used the argument, else false
*/
virtual ArgList::iterator parse(ArgList& list, ArgList::iterator begin) = 0;
/**
* Parse the commandline parameter of a long commandline switch
*
* @returns true if the parameter has been used
*/
virtual bool parse(const std::string& param) = 0;
/**
* Notify that the option is present in the command line, but has no
* arguments
*/
virtual void parse_noarg() = 0;
/// Return true if the argument to this function can be omitted
virtual bool arg_is_optional() const { return false; }
public:
Option();
virtual ~Option() {}
bool isSet() const { return m_isset; }
const std::string& name() const { return m_name; }
void addAlias(char c) { shortNames.push_back(c); }
void addAlias(const std::string& str) { longNames.push_back(str); }
/// Return a full usage message including all the aliases for this option
const std::string& fullUsage() const;
std::string fullUsageForMan() const;
std::vector<char> shortNames;
std::vector<std::string> longNames;
std::string usage;
std::string description;
// Set to true if the option should not be documented
bool hidden;
friend class OptionGroup;
friend class Engine;
};
/// Boolean option
class BoolOption : public Option
{
bool m_value;
protected:
BoolOption(const std::string& name)
: Option(name), m_value(false) {}
BoolOption(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
: Option(name, shortName, longName, usage, description), m_value(false) {}
virtual ArgList::iterator parse(ArgList&, ArgList::iterator begin) { parse_noarg(); return begin; }
virtual bool parse(const std::string&) { parse_noarg(); return false; }
virtual void parse_noarg() { m_isset = true; m_value = true; }
public:
bool boolValue() const { return m_value; }
std::string stringValue() const { return m_value ? "true" : "false"; }
friend class OptionGroup;
friend class Engine;
};
template<typename T>
class SingleOption : public Option
{
protected:
typename T::value_type m_value;
SingleOption(const std::string& name)
: Option(name), m_value(T::init_val)
{
usage = "<val>";
}
SingleOption(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
: Option(name, shortName, longName, usage, description)
{
if (usage.empty())
this->usage = "<val>";
}
ArgList::iterator parse(ArgList& list, ArgList::iterator begin)
{
if (begin == list.end())
throw exception::BadOption("option requires an argument");
m_value = T::parse(*begin);
m_isset = true;
// Remove the parsed element
return list.eraseAndAdvance(begin);
}
bool parse(const std::string& param)
{
m_value = T::parse(param);
m_isset = true;
return true;
}
void parse_noarg()
{
throw exception::BadOption("option requires an argument");
}
public:
void setValue( const typename T::value_type &a ) {
m_value = a;
}
typename T::value_type value() const { return m_value; }
// Deprecated
bool boolValue() const { return T::toBool(m_value); }
int intValue() const { return T::toInt(m_value); }
std::string stringValue() const { return T::toString(m_value); }
friend class OptionGroup;
friend class Engine;
};
/**
* Single option whose value can be or not be specified.
*
* It works for long option style only: short options with an optional argument
* would be ambiguous.
*/
template<typename T>
class SingleOptvalOption : public Option
{
protected:
typename T::value_type m_value;
bool m_hasval;
SingleOptvalOption(const std::string& name)
: Option(name)
{
usage = "<val>";
}
SingleOptvalOption(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
: Option(name, 0, longName, usage, description)
{
if (shortName != 0)
throw wibble::exception::Consistency(
"creating option " + name + " with optional value"
"short options with optional values are not allowed");
if (usage.empty())
this->usage = "<val>";
}
ArgList::iterator parse(ArgList& list, ArgList::iterator begin)
{
throw wibble::exception::Consistency(
"parsing option with optional value"
"short options with optional values are not allowed");
}
bool parse(const std::string& param)
{
m_value = T::parse(param);
m_isset = true;
m_hasval = true;
return true;
}
void parse_noarg()
{
m_isset = true;
m_hasval = false;
}
virtual bool arg_is_optional() const { return true; }
public:
bool hasValue() const { return m_hasval; }
void setValue( const typename T::value_type &a ) {
m_value = a;
}
typename T::value_type value() const { return m_value; }
friend class OptionGroup;
friend class Engine;
};
// Option needing a compulsory string value
typedef SingleOption<String> StringOption;
// Option with an optional string value
typedef SingleOptvalOption<String> OptvalStringOption;
// Option needing a compulsory int value
typedef SingleOption<Int> IntOption;
// Option with an optional int value
typedef SingleOptvalOption<Int> OptvalIntOption;
/// Commandline option with a mandatory argument naming a file which must exist.
typedef SingleOption<ExistingFile> ExistingFileOption;
/// Commandline option with an optional argument naming a file which must exist.
typedef SingleOptvalOption<ExistingFile> OptvalExistingFileOption;
// Option that can be specified multiple times
template<typename T>
class VectorOption : public Option
{
std::vector< typename T::value_type > m_values;
protected:
VectorOption(const std::string& name)
: Option(name)
{
usage = "<val>";
}
VectorOption(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
: Option(name, shortName, longName, usage, description)
{
if (usage.empty())
this->usage = "<val>";
}
ArgList::iterator parse(ArgList& list, ArgList::iterator begin)
{
if (begin == list.end())
throw exception::BadOption("no string argument found");
m_isset = true;
m_values.push_back(T::parse(*begin));
// Remove the parsed element
return list.eraseAndAdvance(begin);
}
bool parse(const std::string& param)
{
m_isset = true;
m_values.push_back(T::parse(param));
return true;
}
void parse_noarg()
{
throw exception::BadOption("option requires an argument");
}
public:
bool boolValue() const { return !m_values.empty(); }
const std::vector< typename T::value_type >& values() const { return m_values; }
friend class OptionGroup;
friend class Engine;
};
/**
* Group related commandline options
*/
class OptionGroup : public Managed
{
MemoryManager* m_manager;
protected:
OptionGroup(MemoryManager* mman = 0, const std::string& description = std::string())
: m_manager(mman), description(description), hidden(false) {}
public:
Option* add(Option* o) { options.push_back(o); return o; }
std::vector<Option*> options;
std::string description;
// Set to true if the option group should not be documented
bool hidden;
/**
* Create a new option
*/
template<typename T>
T* create(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
{
T* item = new T(name, shortName, longName, usage, description);
if (m_manager) m_manager->add(item);
return item;
}
/**
* Create a new option and add it to this group
*/
template<typename T>
T* add(const std::string& name,
char shortName,
const std::string& longName,
const std::string& usage = std::string(),
const std::string& description = std::string())
{
T* res = create<T>(name, shortName, longName, usage, description);
add(res);
return res;
}
friend class Engine;
};
}
}
// vim:set ts=4 sw=4:
#endif
|