X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.h;h=5c21ccb97bb7142aeb2b968ebd0e8e23e1303676;hp=8d78a728169a74e2b5715fce27ee63bccd100cc0;hb=99b9121e2158603372c7313400283b622e6754d8;hpb=c3e242c2629cbc9645258b30aaf07b7285d4372b diff --git a/source/core/getopt.h b/source/core/getopt.h index 8d78a72..5c21ccb 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -17,7 +17,7 @@ private: public: usage_error(const std::string &w, const std::string &h = std::string()): std::runtime_error(w), help_(h) { } - ~usage_error() throw() { } + virtual ~usage_error() throw() = default; const char *help() const throw() { return help_.c_str(); } }; @@ -72,9 +72,9 @@ public: class Option { protected: - Option() { } + Option() = default; public: - virtual ~Option() { } + virtual ~Option() = default; /// Sets help text for the option. virtual Option &set_help(const std::string &) = 0; @@ -92,9 +92,9 @@ public: class Argument { protected: - Argument() { } + Argument() = default; public: - virtual ~Argument() { } + virtual ~Argument() = default; virtual Argument &set_help(const std::string &) = 0; }; @@ -103,9 +103,9 @@ private: class Store { protected: - Store() { } + Store() = default; public: - virtual ~Store() { } + virtual ~Store() = default; virtual Store *clone() const = 0; @@ -117,14 +117,14 @@ private: class OptionImpl: public Option { protected: - char shrt; + char shrt = 0; std::string lng; - ArgType arg_type; - unsigned seen_count; - unsigned *ext_seen_count; + ArgType arg_type = NO_ARG; + unsigned seen_count = 0; + unsigned *ext_seen_count = 0; std::string help; - std::string metavar; - Store *store; + std::string metavar = "ARG"; + Store *store = 0; public: OptionImpl(char, const std::string &, const Store &, ArgType); @@ -147,9 +147,9 @@ private: { private: std::string name; - ArgType type; + ArgType type = REQUIRED_ARG; std::string help; - Store *store; + Store *store = 0; public: ArgumentImpl(const std::string &, const Store &, ArgType); @@ -203,22 +203,15 @@ private: { data.push_back(lexical_cast(a)); } }; - typedef std::list OptionList; - typedef std::list ArgumentList; - - bool help; - OptionList opts; - ArgumentList args; + bool help = false; + std::vector opts; + std::vector args; std::vector args_raw; public: GetOpt(); ~GetOpt(); - /** Returns any non-option arguments encountered during processing. - Deprecated. */ - const std::vector &get_args() const { return args_raw; } - /** Adds an option with both short and long forms. Processing depends on the type of the destination variable and whether an argument is taken or not. With an argument, the value is lexical_cast to the appropriate type @@ -228,6 +221,13 @@ public: Option &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) { return add_option(s, l, SimpleStore(d), a); } + /** Adds an option with both short and long forms. The option may be + specified multiple times, and the argument from each occurrence is stored in + the list. The argument type must be REQUIRED_ARG. */ + template + Option &add_option(char s, const std::string &l, std::vector &d, ArgType a = REQUIRED_ARG) + { return add_option(s, l, ListStore >(d), a); } + /** Adds an option with both short and long forms. The option may be specified multiple times, and the argument from each occurrence is stored in the list. The argument type must be REQUIRED_ARG. */ @@ -246,6 +246,13 @@ public: Argument &add_argument(const std::string &n, T &d, ArgType a = REQUIRED_ARG) { return add_argument(n, SimpleStore(d), a); } + /** Adds a positional argument list. If the list is declared as required, + at least one element must be given; an optional list may be empty. Only one + list may be added, and optional fixed arguments can't be used with it. */ + template + Argument &add_argument(const std::string &n, std::vector &d, ArgType a = REQUIRED_ARG) + { return add_argument(n, ListStore >(d), a); } + /** Adds a positional argument list. If the list is declared as required, at least one element must be given; an optional list may be empty. Only one list may be added, and optional fixed arguments can't be used with it. */ @@ -292,6 +299,9 @@ template<> inline void GetOpt::SimpleStore::store() template<> inline void GetOpt::SimpleStore::store(const std::string &a) { data = a; } +template<> inline void GetOpt::ListStore >::store(const std::string &a) +{ data.push_back(a); } + template<> inline void GetOpt::ListStore >::store(const std::string &a) { data.push_back(a); }