options.h
Go to the documentation of this file.
#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;
};
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);
}
virtual ArgList::iterator parse(ArgList& list, ArgList::iterator begin) = 0;
virtual bool parse(const std::string& param) = 0;
virtual void parse_noarg() = 0;
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); }
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;
};
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;
};
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;
typedef SingleOption<ExistingFile> ExistingFileOption;
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;
};
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;
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;
}
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
wibble::commandline::ArgList
Definition: core.h:30
wibble::commandline::ArgList::eraseAndAdvance
iterator & eraseAndAdvance(iterator &i)
Definition: core.h:34
wibble::commandline::BoolOption
Boolean option.
Definition: options.h:135
wibble::commandline::BoolOption::BoolOption
BoolOption(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Definition: options.h:141
wibble::commandline::BoolOption::BoolOption
BoolOption(const std::string &name)
Definition: options.h:139
wibble::commandline::BoolOption::stringValue
std::string stringValue() const
Definition: options.h:154
wibble::commandline::BoolOption::boolValue
bool boolValue() const
Definition: options.h:153
wibble::commandline::BoolOption::parse
virtual ArgList::iterator parse(ArgList &, ArgList::iterator begin)
Parse the next commandline parameter after the short form of the command has been found.
Definition: options.h:148
wibble::commandline::BoolOption::parse_noarg
virtual void parse_noarg()
Notify that the option is present in the command line, but has no arguments.
Definition: options.h:150
wibble::commandline::BoolOption::parse
virtual bool parse(const std::string &)
Parse the commandline parameter of a long commandline switch.
Definition: options.h:149
wibble::commandline::Engine
Parse commandline options.
Definition: engine.h:39
wibble::commandline::Managed
Definition: core.h:52
wibble::commandline::MemoryManager
Keep track of various wibble::commandline components, and deallocate them at object destruction.
Definition: core.h:63
wibble::commandline::MemoryManager::add
T * add(T *item)
Definition: core.h:76
wibble::commandline::OptionGroup
Group related commandline options.
Definition: options.h:360
wibble::commandline::OptionGroup::options
std::vector< Option * > options
Definition: options.h:370
wibble::commandline::OptionGroup::description
std::string description
Definition: options.h:372
wibble::commandline::OptionGroup::OptionGroup
OptionGroup(MemoryManager *mman=0, const std::string &description=std::string())
Definition: options.h:364
wibble::commandline::OptionGroup::add
Option * add(Option *o)
Definition: options.h:368
wibble::commandline::OptionGroup::create
T * create(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Create a new option.
Definition: options.h:381
wibble::commandline::OptionGroup::add
T * add(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Create a new option and add it to this group.
Definition: options.h:396
wibble::commandline::OptionGroup::hidden
bool hidden
Definition: options.h:375
wibble::commandline::Option
Interface for a parser for one commandline option.
Definition: options.h:56
wibble::commandline::Option::parse
virtual bool parse(const std::string &param)=0
Parse the commandline parameter of a long commandline switch.
wibble::commandline::Option::arg_is_optional
virtual bool arg_is_optional() const
Return true if the argument to this function can be omitted.
Definition: options.h:104
wibble::commandline::Option::longNames
std::vector< std::string > longNames
Definition: options.h:121
wibble::commandline::Option::Option
Option()
Definition: options.cpp:99
wibble::commandline::Option::usage
std::string usage
Definition: options.h:123
wibble::commandline::Option::addAlias
void addAlias(const std::string &str)
Definition: options.h:114
wibble::commandline::Option::~Option
virtual ~Option()
Definition: options.h:108
wibble::commandline::Option::parse_noarg
virtual void parse_noarg()=0
Notify that the option is present in the command line, but has no arguments.
wibble::commandline::Option::parse
virtual ArgList::iterator parse(ArgList &list, ArgList::iterator begin)=0
Parse the next commandline parameter after the short form of the command has been found.
wibble::commandline::Option::name
const std::string & name() const
Definition: options.h:111
wibble::commandline::Option::description
std::string description
Definition: options.h:124
wibble::commandline::Option::Option
Option(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Definition: options.h:64
wibble::commandline::Option::fullUsage
const std::string & fullUsage() const
Return a full usage message including all the aliases for this option.
Definition: options.cpp:101
wibble::commandline::Option::shortNames
std::vector< char > shortNames
Definition: options.h:120
wibble::commandline::Option::hidden
bool hidden
Definition: options.h:127
wibble::commandline::Option::addAlias
void addAlias(char c)
Definition: options.h:113
wibble::commandline::Option::isSet
bool isSet() const
Definition: options.h:110
wibble::commandline::Option::m_isset
bool m_isset
Definition: options.h:61
wibble::commandline::Option::fullUsageForMan
std::string fullUsageForMan() const
Definition: options.cpp:124
wibble::commandline::Option::Option
Option(const std::string &name)
Definition: options.h:63
wibble::commandline::SingleOption
Definition: options.h:162
wibble::commandline::SingleOption::SingleOption
SingleOption(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Definition: options.h:171
wibble::commandline::SingleOption::parse
bool parse(const std::string &param)
Parse the commandline parameter of a long commandline switch.
Definition: options.h:191
wibble::commandline::SingleOption::parse
ArgList::iterator parse(ArgList &list, ArgList::iterator begin)
Parse the next commandline parameter after the short form of the command has been found.
Definition: options.h:182
wibble::commandline::SingleOption::m_value
T::value_type m_value
Definition: options.h:164
wibble::commandline::SingleOption::value
T::value_type value() const
Definition: options.h:207
wibble::commandline::SingleOption::intValue
int intValue() const
Definition: options.h:211
wibble::commandline::SingleOption::stringValue
std::string stringValue() const
Definition: options.h:212
wibble::commandline::SingleOption::setValue
void setValue(const typename T::value_type &a)
Definition: options.h:203
wibble::commandline::SingleOption::parse_noarg
void parse_noarg()
Notify that the option is present in the command line, but has no arguments.
Definition: options.h:197
wibble::commandline::SingleOption::SingleOption
SingleOption(const std::string &name)
Definition: options.h:166
wibble::commandline::SingleOption::boolValue
bool boolValue() const
Definition: options.h:210
wibble::commandline::SingleOptvalOption
Single option whose value can be or not be specified.
Definition: options.h:226
wibble::commandline::SingleOptvalOption::arg_is_optional
virtual bool arg_is_optional() const
Return true if the argument to this function can be omitted.
Definition: options.h:270
wibble::commandline::SingleOptvalOption::SingleOptvalOption
SingleOptvalOption(const std::string &name)
Definition: options.h:231
wibble::commandline::SingleOptvalOption::m_value
T::value_type m_value
Definition: options.h:228
wibble::commandline::SingleOptvalOption::parse
bool parse(const std::string &param)
Parse the commandline parameter of a long commandline switch.
Definition: options.h:257
wibble::commandline::SingleOptvalOption::parse
ArgList::iterator parse(ArgList &list, ArgList::iterator begin)
Parse the next commandline parameter after the short form of the command has been found.
Definition: options.h:251
wibble::commandline::SingleOptvalOption::hasValue
bool hasValue() const
Definition: options.h:273
wibble::commandline::SingleOptvalOption::value
T::value_type value() const
Definition: options.h:279
wibble::commandline::SingleOptvalOption::setValue
void setValue(const typename T::value_type &a)
Definition: options.h:275
wibble::commandline::SingleOptvalOption::SingleOptvalOption
SingleOptvalOption(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Definition: options.h:236
wibble::commandline::SingleOptvalOption::parse_noarg
void parse_noarg()
Notify that the option is present in the command line, but has no arguments.
Definition: options.h:264
wibble::commandline::SingleOptvalOption::m_hasval
bool m_hasval
Definition: options.h:229
wibble::commandline::VectorOption
Definition: options.h:307
wibble::commandline::VectorOption::values
const std::vector< typename T::value_type > & values() const
Definition: options.h:349
wibble::commandline::VectorOption::parse
ArgList::iterator parse(ArgList &list, ArgList::iterator begin)
Parse the next commandline parameter after the short form of the command has been found.
Definition: options.h:327
wibble::commandline::VectorOption::VectorOption
VectorOption(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Definition: options.h:316
wibble::commandline::VectorOption::VectorOption
VectorOption(const std::string &name)
Definition: options.h:311
wibble::commandline::VectorOption::parse
bool parse(const std::string &param)
Parse the commandline parameter of a long commandline switch.
Definition: options.h:336
wibble::commandline::VectorOption::parse_noarg
void parse_noarg()
Notify that the option is present in the command line, but has no arguments.
Definition: options.h:342
wibble::commandline::VectorOption::boolValue
bool boolValue() const
Definition: options.h:348
wibble::exception::BadOption
Definition: core.h:13
wibble::exception::Consistency
Exception thrown when some consistency check fails.
Definition: exception.h:255
usage
void usage(ostream &out, const string &argv0)
Definition: commandline-demo.cpp:75
core.h
wibble::commandline::OptvalExistingFileOption
SingleOptvalOption< ExistingFile > OptvalExistingFileOption
Commandline option with an optional argument naming a file which must exist.
Definition: options.h:301
wibble::commandline::OptvalIntOption
SingleOptvalOption< Int > OptvalIntOption
Definition: options.h:295
wibble::commandline::IntOption
SingleOption< Int > IntOption
Definition: options.h:292
wibble::commandline::OptvalStringOption
SingleOptvalOption< String > OptvalStringOption
Definition: options.h:289
wibble::commandline::StringOption
SingleOption< String > StringOption
Definition: options.h:286
wibble::commandline::ExistingFileOption
SingleOption< ExistingFile > ExistingFileOption
Commandline option with a mandatory argument naming a file which must exist.
Definition: options.h:298
wibble::list::begin
ListIterator< List > begin(List l)
Definition: list.h:420
wibble
Definition: amorph.h:17
wibble::iterator
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
wibble::commandline::Bool
Definition: options.h:14
wibble::commandline::Bool::toString
static std::string toString(const value_type &val)
Definition: options.cpp:27
wibble::commandline::Bool::value_type
bool value_type
Definition: options.h:15
wibble::commandline::Bool::toBool
static bool toBool(const value_type &val)
Definition: options.cpp:25
wibble::commandline::Bool::init_val
static bool init_val
Definition: options.h:21
wibble::commandline::Bool::parse
static bool parse(const std::string &val)
Definition: options.cpp:17
wibble::commandline::Bool::toInt
static int toInt(const value_type &val)
Definition: options.cpp:26
wibble::commandline::ExistingFile
Definition: options.h:47
wibble::commandline::ExistingFile::value_type
std::string value_type
Definition: options.h:48
wibble::commandline::ExistingFile::parse
static std::string parse(const std::string &val)
wibble::commandline::ExistingFile::toString
static std::string toString(const value_type &val)
Definition: options.cpp:60
wibble::commandline::ExistingFile::init_val
static std::string init_val
Definition: options.h:51
wibble::commandline::Int
Definition: options.h:25
wibble::commandline::Int::toBool
static bool toBool(const value_type &val)
Definition: options.cpp:38
wibble::commandline::Int::parse
static int parse(const std::string &val)
Definition: options.cpp:30
wibble::commandline::Int::value_type
int value_type
Definition: options.h:26
wibble::commandline::Int::init_val
static int init_val
Definition: options.h:32
wibble::commandline::Int::toString
static std::string toString(const value_type &val)
Definition: options.cpp:40
wibble::commandline::Int::toInt
static int toInt(const value_type &val)
Definition: options.cpp:39
wibble::commandline::String
Definition: options.h:36
wibble::commandline::String::parse
static std::string parse(const std::string &val)
Definition: options.cpp:43
wibble::commandline::String::init_val
static std::string init_val
Definition: options.h:43
wibble::commandline::String::toString
static std::string toString(const value_type &val)
Definition: options.cpp:49
wibble::commandline::String::toBool
static bool toBool(const value_type &val)
Definition: options.cpp:47
wibble::commandline::String::value_type
std::string value_type
Definition: options.h:37
wibble::commandline::String::toInt
static int toInt(const value_type &val)
Definition: options.cpp:48