options: whitespace

This commit is contained in:
Danny Robson 2015-06-09 15:41:08 +10:00
parent 9db4dd5b08
commit 1d65a7ed77
2 changed files with 163 additions and 161 deletions

View File

@ -307,23 +307,23 @@ bytesoption::execute (const std::string& data)
class helpoption : public option class helpoption : public option
{ {
protected: protected:
static const char HELP_CHARACTER; static const char HELP_CHARACTER;
static const char *HELP_NAME; static const char *HELP_NAME;
static const char *HELP_DESCRIPTION; static const char *HELP_DESCRIPTION;
processor * m_processor; processor * m_processor;
public: public:
helpoption (processor * _processor): helpoption (processor * _processor):
option (HELP_CHARACTER, HELP_NAME, HELP_DESCRIPTION, false), option (HELP_CHARACTER, HELP_NAME, HELP_DESCRIPTION, false),
m_processor (_processor) m_processor (_processor)
{ ; } { ; }
virtual void execute (void); virtual void execute (void);
virtual void execute (const std::string& data) virtual void execute (const std::string& data)
{ option::execute (data); } { option::execute (data); }
}; };
@ -531,7 +531,8 @@ processor::parse_args (int argc, const char ** argv)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void void
processor::add_option (std::unique_ptr<option> opt) { processor::add_option (std::unique_ptr<option> opt)
{
if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ()) if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ())
throw std::logic_error ("Short option already exists"); throw std::logic_error ("Short option already exists");
if (m_longopt.find (opt->longopt ()) != m_longopt.end ()) if (m_longopt.find (opt->longopt ()) != m_longopt.end ())
@ -546,7 +547,8 @@ processor::add_option (std::unique_ptr<option> opt) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::unique_ptr<option> std::unique_ptr<option>
processor::remove_option (char letter) { processor::remove_option (char letter)
{
// Locate the option by short name // Locate the option by short name
const auto s_candidate = m_shortopt.find (letter); const auto s_candidate = m_shortopt.find (letter);
if (s_candidate == m_shortopt.end ()) if (s_candidate == m_shortopt.end ())
@ -575,7 +577,8 @@ processor::remove_option (char letter) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::unique_ptr<option> std::unique_ptr<option>
processor::remove_option (const char *name) { processor::remove_option (const char *name)
{
// Locate the option by long name // Locate the option by long name
const auto l_candidate = m_longopt.find (name); const auto l_candidate = m_longopt.find (name);
if (l_candidate == m_longopt.end ()) if (l_candidate == m_longopt.end ())

View File

@ -44,37 +44,37 @@ namespace util {
* some form of abort action. * some form of abort action.
*/ */
class option { class option {
protected: protected:
char m_shortopt; char m_shortopt;
const std::string m_longopt; const std::string m_longopt;
const std::string m_description; const std::string m_description;
bool m_required; bool m_required;
bool m_found; bool m_found;
public: public:
option (char _letter, option (char _letter,
const char *_name, const char *_name,
const char *_desc, const char *_desc,
bool _required); bool _required);
virtual ~option() { ; } virtual ~option() { ; }
virtual void execute (void) = 0; virtual void execute (void) = 0;
virtual void execute (const std::string& _data) = 0; virtual void execute (const std::string& _data) = 0;
virtual void finish (void); virtual void finish (void);
virtual bool is_required (void) const virtual bool is_required (void) const
{ return m_required; } { return m_required; }
virtual void reset(void) virtual void reset(void)
{ m_found = false; } { m_found = false; }
virtual char shortopt(void) const virtual char shortopt(void) const
{ return m_shortopt; } { return m_shortopt; }
virtual const std::string& longopt(void) const virtual const std::string& longopt(void) const
{ return m_longopt; } { return m_longopt; }
virtual const std::string& description(void) const virtual const std::string& description(void) const
{ return m_description; } { return m_description; }
}; };
@ -86,16 +86,16 @@ namespace util {
* printed. * printed.
*/ */
class nulloption : public option { class nulloption : public option {
public: public:
nulloption(char _letter, nulloption(char _letter,
const char *_name, const char *_name,
const char *_desc, const char *_desc,
bool _required = false); bool _required = false);
virtual void execute(void) virtual void execute(void)
{ m_found = true; } { m_found = true; }
virtual void execute(const std::string&) virtual void execute(const std::string&)
{ m_found = true; } { m_found = true; }
}; };
@ -109,22 +109,22 @@ namespace util {
* Throws an exception if a value is specified for the option. * Throws an exception if a value is specified for the option.
*/ */
class presentoption : public option { class presentoption : public option {
protected: protected:
bool *m_data; bool *m_data;
public: public:
presentoption(char _letter, presentoption(char _letter,
const char *_name, const char *_name,
const char *_desc, const char *_desc,
bool *_data, bool *_data,
bool _required = false); bool _required = false);
virtual void execute(void); virtual void execute(void);
virtual void execute(const std::string& data) virtual void execute(const std::string& data)
{ option::execute(data); } { option::execute(data); }
virtual void reset(void) virtual void reset(void)
{ *m_data = false; } { *m_data = false; }
}; };
@ -142,43 +142,42 @@ namespace util {
*/ */
template<typename T> template<typename T>
class valueoption : public option { class valueoption : public option {
protected: protected:
T *m_data; T *m_data;
public: public:
valueoption(char _letter, valueoption(char _letter,
const char *_name, const char *_name,
const char *_desc, const char *_desc,
T *_data, T *_data,
bool _required = false): bool _required = false):
option (_letter, _name, _desc, _required), option (_letter, _name, _desc, _required),
m_data (_data) m_data (_data)
{ ; } { ; }
virtual void execute(void) virtual void execute(void)
{ option::execute(); } { option::execute(); }
virtual void execute(const std::string& data) { virtual void execute(const std::string& data) {
get_arg(data, m_data); get_arg(data, m_data);
m_found = true; m_found = true;
}
protected:
// Retrieve string to value conversion
static
T& get_arg(const std::string &arg,
T *val);
static
T& get_arg(const std::string &_arg,
T *val,
const T &defaultval) {
try {
return get_arg(_arg, val);
} catch(...) {
return *val = defaultval;
} }
}
protected:
// Retrieve string to value conversion
static
T& get_arg(const std::string &arg,
T *val);
static
T& get_arg(const std::string &_arg,
T *val,
const T &defaultval) {
try {
return get_arg(_arg, val);
} catch(...) {
return *val = defaultval;
}
}
}; };
@ -192,54 +191,54 @@ namespace util {
* Recognises various postfixes and modifiers to a numeric value such as MiB. * Recognises various postfixes and modifiers to a numeric value such as MiB.
*/ */
class bytesoption : public valueoption<size_t> { class bytesoption : public valueoption<size_t> {
public: public:
/* Description of types available for parsing /* Description of types available for parsing
*/ */
enum bytestype { enum bytestype {
BYTES_SINGLE = 0, BYTES_SINGLE = 0,
BYTES_KILO = 1, BYTES_KILO = 1,
BYTES_MEGA = 2, BYTES_MEGA = 2,
BYTES_GIGA = 3, BYTES_GIGA = 3,
BYTES_TERA = 4, BYTES_TERA = 4,
BYTES_PETA = 5, BYTES_PETA = 5,
BYTES_EXA = 6, BYTES_EXA = 6,
// Currently does not support yota or zeta as there can be // Currently does not support yota or zeta as there can be
// trouble converting them without loss into 64bit quantities. // trouble converting them without loss into 64bit quantities.
// That and they're fricking huge... // That and they're fricking huge...
// //
// BYTES_ZETA, // BYTES_ZETA,
// BYTES_YOTA, // BYTES_YOTA,
NUM_BYTESTYPE NUM_BYTESTYPE
}; };
enum bytesmodifier { enum bytesmodifier {
BYTES_BASE2, BYTES_BASE2,
BYTES_BASE10, BYTES_BASE10,
NUM_BYTESMODIFIER NUM_BYTESMODIFIER
}; };
protected: protected:
bytestype m_type; bytestype m_type;
bytesmodifier m_modifier; bytesmodifier m_modifier;
static bytestype type_from_character(char c); static bytestype type_from_character(char c);
public: public:
/* Constructors and methods /* Constructors and methods
*/ */
bytesoption(char _letter, bytesoption(char _letter,
const char *_name, const char *_name,
const char *_desc, const char *_desc,
size_t *_data, size_t *_data,
bytestype _type = BYTES_SINGLE, bytestype _type = BYTES_SINGLE,
bytesmodifier _modifier = BYTES_BASE2, bytesmodifier _modifier = BYTES_BASE2,
bool _required = false); bool _required = false);
virtual void execute(const std::string &); virtual void execute(const std::string &);
}; };
@ -256,40 +255,40 @@ namespace util {
* performed within this class, merely dispatch and tokenisation. * performed within this class, merely dispatch and tokenisation.
*/ */
class processor { class processor {
protected: protected:
std::map<char, option *> m_shortopt; std::map<char, option *> m_shortopt;
std::map<std::string, option *> m_longopt; std::map<std::string, option *> m_longopt;
std::list<std::unique_ptr<option>> m_options; std::list<std::unique_ptr<option>> m_options;
// The command to execute the application // The command to execute the application
std::string m_command; std::string m_command;
protected: protected:
unsigned int parse_short(int pos, int argc, const char ** argv); unsigned int parse_short(int pos, int argc, const char ** argv);
unsigned int parse_long(int pos, int argc, const char ** argv); unsigned int parse_long(int pos, int argc, const char ** argv);
public: public:
processor(); processor();
~processor(); ~processor();
void print_usage(void); void print_usage(void);
// TODO: Use function overloading here... // TODO: Use function overloading here...
void parse_args(int argc, const char ** argv); void parse_args(int argc, const char ** argv);
void parse_args(int argc, char ** argv) void parse_args(int argc, char ** argv)
{ parse_args(argc, const_cast<const char**>(argv)); } { parse_args(argc, const_cast<const char**>(argv)); }
void parse_stream(std::istream & is); void parse_stream(std::istream & is);
void add_option(std::unique_ptr<option>); void add_option(std::unique_ptr<option>);
std::unique_ptr<option> remove_option(char letter); std::unique_ptr<option> remove_option(char letter);
std::unique_ptr<option> remove_option(const char * name); std::unique_ptr<option> remove_option(const char * name);
std::unique_ptr<option> remove_option(const std::string& name) std::unique_ptr<option> remove_option(const std::string& name)
{ return remove_option (name.c_str()); } { return remove_option (name.c_str()); }
std::unique_ptr<option> remove_option(const option *opt) std::unique_ptr<option> remove_option(const option *opt)
{ return remove_option (opt->longopt ()); } { return remove_option (opt->longopt ()); }
}; };
} }