diff --git a/src/Arg.cpp b/src/Arg.cpp index 86cecd5..13bc0de 100644 --- a/src/Arg.cpp +++ b/src/Arg.cpp @@ -21,6 +21,7 @@ #include +#include using namespace std; @@ -35,24 +36,8 @@ const string Arg::flagStartString = "-"; const string Arg::nameStartString = "--"; const string Arg::ignoreNameString = "ignore_rest"; -Arg::Arg( const string& flag, - const string& name, - const string& desc, - bool req, - bool valreq, - Visitor* v) -: - _flag(flag), - _name(name), - _description(desc), - _required(req), - _requireLabel("required"), - _valueRequired(valreq), - _alreadySet(false), - _visitor( v ), - _ignoreable(true), - _xorSet(false) -{ +void Arg::init() +{ if ( _flag.length() > 1 ) throw(ArgException("Argument flag can only be one character long", toString() ) ); @@ -73,9 +58,62 @@ Arg::Arg( const string& flag, Arg::flagStartString + "' or '" + Arg::nameStartString + "' or space.", toString() ) ); + +} +Arg::Arg( const string& flag, + const string& name, + const string& desc, + bool req, + bool valreq, + Visitor* v) +: + _flag(flag), + _name(name), + _description(desc), + _required(req), + _requireLabel("required"), + _valueRequired(valreq), + _alreadySet(false), + _visitor( v ), + _ignoreable(true), + _xorSet(false) +{ + try { + init(); //< initialize the object + } catch (ArgException ae) { + throw(ae); //< and forward any exceptions + } }; +Arg::Arg(const std::string& flag, + const std::string& name, + const std::string& desc, + bool req, + bool valreq, + CmdLine &parser, + Visitor* v) : + _flag(flag), + _name(name), + _description(desc), + _required(req), + _requireLabel("required"), + _valueRequired(valreq), + _alreadySet(false), + _visitor( v ), + _ignoreable(true), + _xorSet(false) +{ + try { + init(); //< initialize the object + } catch (ArgException ae) { + throw(ae); //< and forward any exceptions + } + + //Add to the parser, what about exceptions + parser.add(*this); +} + Arg::Arg() : _flag(""), diff --git a/src/SwitchArg.cpp b/src/SwitchArg.cpp index 0171c4b..6eb3a41 100644 --- a/src/SwitchArg.cpp +++ b/src/SwitchArg.cpp @@ -21,6 +21,7 @@ #include +#include using namespace std; @@ -35,6 +36,18 @@ SwitchArg::SwitchArg(const string& flag, _value( _default ) { }; +SwitchArg::SwitchArg(const string& flag, + const string& name, + const string& desc, + bool _default, + CmdLine &parser, + Visitor* v ) +: Arg(flag, name, desc, false, false, v), + _value( _default ) +{ + parser.add(*this); +} + SwitchArg::~SwitchArg() { }; bool SwitchArg::getValue() { return _value; };