options.hh
#ifndef __SDR_OPTIONS_HH__
#define __SDR_OPTIONS_HH__
#include <string>
#include <map>
namespace sdr {
class Options
{
public:
class Value {
protected:
typedef enum {
NONE,
INTEGER,
FLOAT,
STRING
} Type;
public:
Value();
Value(long value);
Value(double value);
Value(const std::string &value);
Value(const Value &other);
~Value();
const Value &operator=(const Value &other);
bool isNone() const;
bool isInteger() const;
bool isFloat() const;
bool isString() const;
long toInteger() const;
double toFloat() const;
std::string toString() const;
protected:
Type _type;
union {
long as_int;
double as_float;
char *as_string;
} _value;
};
typedef enum {
FLAG,
INTEGER,
FLOAT,
ANY
} ArgType;
typedef struct {
const char *name;
char short_name;
ArgType type;
const char *help;
} Definition;
static bool parse(const Definition defs[], int argc, char *argv[], Options &options);
static void print_help(std::ostream &stream, const Definition defs[]);
public:
Options();
bool has(const char *name);
const Value &get(const char *name);
protected:
std::map<std::string, Value> _options;
};
}
#endif // __SDR_OPTIONS_HH__
sdr::Options::_options
std::map< std::string, Value > _options
The table of option names and argument values.
Definition: options.hh:97
sdr::Options::Value::isInteger
bool isInteger() const
Returns true if the value is an integer.
Definition: options.cc:181
sdr::Options::Value::Value
Value()
Empty constructor.
Definition: options.cc:130
sdr::Options::Value::~Value
~Value()
Destructor.
Definition: options.cc:154
sdr
Definition: autocast.hh:8
sdr::Options::Definition
Argument definition.
Definition: options.hh:74
sdr::Options::Value::_value
union sdr::Options::Value::@0 _value
Values.
sdr::Options::Value::isNone
bool isNone() const
Returns true if the value is empty.
Definition: options.cc:176
sdr::Options::has
bool has(const char *name)
Returns true if the specified option was found (long name).
Definition: options.cc:22
sdr::Options::print_help
static void print_help(std::ostream &stream, const Definition defs[])
Serializes a help text derived from given the definitions into stream.
Definition: options.cc:99
sdr::Options::Value::NONE
Empty or invalid value.
Definition: options.hh:18
sdr::Options::Value::toInteger
long toInteger() const
Turns the value into an integer.
Definition: options.cc:196
sdr::Options::Value::FLOAT
A floating point number (double).
Definition: options.hh:20
sdr::Options::Value::isFloat
bool isFloat() const
Returns true if the value is a floating point number.
Definition: options.cc:186
sdr::Options::ANY
Any argument (string).
Definition: options.hh:70
sdr::Options::Value::toString
std::string toString() const
Turns the value into a string.
Definition: options.cc:206
sdr::Options::Value::toFloat
double toFloat() const
Turns the value into a floating point number.
Definition: options.cc:201
sdr::Options::ArgType
ArgType
Possible argument types.
Definition: options.hh:66
sdr::Options::FLOAT
Floating point argument.
Definition: options.hh:69
sdr::Options::Value::INTEGER
An integer (long int).
Definition: options.hh:19
sdr::Options::Value::operator=
const Value & operator=(const Value &other)
Assignment.
Definition: options.cc:165
sdr::Options::FLAG
No argument (flag).
Definition: options.hh:67
sdr::Options::Definition::short_name
char short_name
Argument name (short).
Definition: options.hh:76
sdr::Options::INTEGER
Integer argument.
Definition: options.hh:68
sdr::Options::Value::STRING
An ASCII string.
Definition: options.hh:21
sdr::Options::Definition::name
const char * name
Argument name (long).
Definition: options.hh:75
sdr::Options::parse
static bool parse(const Definition defs[], int argc, char *argv[], Options &options)
Parse the given arguments (argc, argv) using the definitions defs and stores the results into options...
Definition: options.cc:32
sdr::Options::Definition::help
const char * help
Help string.
Definition: options.hh:78
sdr::Options::Value
The argument value.
Definition: options.hh:14
sdr::Options
Convenience functions for command line arguments.
Definition: options.hh:10
sdr::Options::Options
Options()
Empty constructor.
Definition: options.cc:15
sdr::Options::Value::isString
bool isString() const
Returns true if the value is a string.
Definition: options.cc:191
sdr::Options::Definition::type
ArgType type
Argument type.
Definition: options.hh:77
sdr::Options::Value::Type
Type
Value type.
Definition: options.hh:17
sdr::Options::Value::_type
Type _type
The type of the value.
Definition: options.hh:56