X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fgetopt.h;h=f6c48597c5220f73998f1ced0ccc6faf5411a445;hb=c76bd823b81d723d8cd4531631a4b18544f1981a;hp=2824a0b7c7f3b79378477853fa8874300a98eb75;hpb=521cf1db00f8ce2d9f9494dca503d6c17d89ac2f;p=libs%2Fcore.git diff --git a/source/core/getopt.h b/source/core/getopt.h index 2824a0b..f6c4859 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -7,10 +7,10 @@ Distributed under the LGPL #ifndef MSP_CORE_GETOPT_H_ #define MSP_CORE_GETOPT_H_ -#include #include #include -#include "error.h" +#include +#include "except.h" namespace Msp { @@ -27,11 +27,13 @@ public: class OptBase { public: - OptBase &set_help(const std::string &h) { help=h; return *this; } + OptBase &set_help(const std::string &); + OptBase &set_help(const std::string &, const std::string &); char get_short() const { return shrt; } const std::string &get_long() const { return lng; } ArgType get_arg_type() const { return arg_type; } const std::string &get_help() const { return help; } + const std::string &get_metavar() const { return metavar; } unsigned get_seen_count() const { return seen_count; } virtual void process()=0; virtual void process(const std::string &)=0; @@ -42,29 +44,11 @@ public: ArgType arg_type; unsigned seen_count; std::string help; + std::string metavar; - OptBase(char s, const std::string &l, ArgType a): shrt(s), lng(l), arg_type(a), seen_count(0) { } + OptBase(char, const std::string &, ArgType); }; - const std::list &get_args() const { return args; } - - template - OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG) - { opts.push_back(new Option(s, l, d, a)); return *opts.back(); } - - template - OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a=REQUIRED_ARG) - { opts.push_back(new ListOption >(s, l, d, a)); return *opts.back(); } - - template - OptBase &add_option(const std::string &l, T &d, ArgType a) - { return add_option(0, l, d, a); } - - std::string generate_usage(const std::string &) const; - std::string generate_help() const; - void operator()(unsigned, const char *const *); - - ~GetOpt(); private: template class Option: public OptBase @@ -127,8 +111,30 @@ private: T &data; }; - std::list opts; - std::list args; + std::list opts; + std::vector args; + +public: + ~GetOpt(); + + const std::vector &get_args() const { return args; } + + template + OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG) + { opts.push_back(new Option(s, l, d, a)); return *opts.back(); } + + template + OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a=REQUIRED_ARG) + { opts.push_back(new ListOption >(s, l, d, a)); return *opts.back(); } + + template + OptBase &add_option(const std::string &l, T &d, ArgType a) + { return add_option(0, l, d, a); } + + std::string generate_usage(const std::string &) const; + std::string generate_help() const; + void operator()(unsigned, const char *const *); +private: OptBase &get_option(char); OptBase &get_option(const std::string &);