X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fcore%2Fgetopt.h;h=8e0f49a3fb9d12c83d986289e6cf69d5fc10cee9;hb=d5dd704b2576f878809e87dbb8ff8591b9bdbce4;hp=94e8b1e4af35e598a6e1f99600f837aef916cfbb;hpb=ae48dac6a368dbf68b1ec976254a5f896c5b737b;p=libs%2Fcore.git diff --git a/source/core/getopt.h b/source/core/getopt.h index 94e8b1e..8e0f49a 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -1,9 +1,10 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ + #ifndef MSP_CORE_GETOPT_H_ #define MSP_CORE_GETOPT_H_ @@ -26,29 +27,31 @@ public: class OptBase { - public: - 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; } - void process(); - void process(const std::string &); - virtual ~OptBase() { } protected: - char shrt; + char shrt; std::string lng; - ArgType arg_type; - unsigned seen_count; + ArgType arg_type; + unsigned seen_count; std::string help; std::string metavar; OptBase(char, const std::string &, ArgType); - virtual void store()=0; - virtual void store(const std::string &)=0; + public: + virtual ~OptBase() { } + + 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; } + void process(); + void process(const std::string &); + protected: + virtual void store() = 0; + virtual void store(const std::string &) = 0; }; private: @@ -68,7 +71,7 @@ private: if(ss.fail()) throw UsageError("Invalid argument for --"+lng); - data=tmp; + data = tmp; } private: T &data; @@ -106,33 +109,47 @@ public: const std::vector &get_args() const { return args; } template - OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG) + 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) + 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 &); + +public: + /** Processes argc/argv style command line arguments. The contents of argv + will be unchanged; use get_args to access non-option arguments. */ + void operator()(unsigned, const char *const *); + +private: + /** Processes a long option. Returns the number of arguments eaten. */ unsigned process_long(const char *const *); + + /** Processes short options. Returns the number of arguments eaten. */ unsigned process_short(const char *const *); + +public: + /** Generates a single line that describes known options. */ + std::string generate_usage(const std::string &) const; + + /** Generates help for known options in tabular format, one option per + line. The returned string will have a linefeed at the end. */ + std::string generate_help() const; }; -template<> inline void GetOpt::Option::store() { data=true; } +template<> inline void GetOpt::Option::store() { data = true; } template<> inline void GetOpt::Option::store() { ++data; } template<> inline void GetOpt::Option::store(const std::string &a) -{ data=a; } +{ data = a; } template<> inline void GetOpt::ListOption >::store(const std::string &a) { data.push_back(a); }