X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.h;h=405864bb27bfdda1668537a35630550465963482;hp=407d3459984fce67c42ffd92f1c9ffe7faae4b23;hb=44da9fc9afb6b7e49c1558c5572213a1e6f401e8;hpb=9be92503cda27dffd8c3219ec4cfadaee37b6369 diff --git a/source/core/getopt.h b/source/core/getopt.h index 407d345..405864b 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -6,6 +6,7 @@ #include #include #include +#include "noncopyable.h" namespace Msp { @@ -58,7 +59,7 @@ A built-in --help option is provided and will output a list of options, arguments and their associated help texts. An application may override this by providing its own option with the same name. */ -class GetOpt +class GetOpt: private NonCopyable { public: enum ArgType @@ -202,22 +203,15 @@ private: { data.push_back(lexical_cast(a)); } }; - typedef std::list OptionList; - typedef std::list ArgumentList; - bool help; - OptionList opts; - ArgumentList args; + 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 @@ -227,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. */ @@ -245,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. */ @@ -291,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); }