3 This file is part of libmspcore
4 Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_CORE_GETOPT_H_
9 #define MSP_CORE_GETOPT_H_
38 OptBase(char, const std::string &, ArgType);
40 virtual ~OptBase() { }
42 OptBase &set_help(const std::string &);
43 OptBase &set_help(const std::string &, const std::string &);
44 char get_short() const { return shrt; }
45 const std::string &get_long() const { return lng; }
46 ArgType get_arg_type() const { return arg_type; }
47 const std::string &get_help() const { return help; }
48 const std::string &get_metavar() const { return metavar; }
49 unsigned get_seen_count() const { return seen_count; }
51 void process(const std::string &);
53 virtual void store() = 0;
54 virtual void store(const std::string &) = 0;
59 class Option: public OptBase
62 Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { }
64 virtual void store() { }
66 virtual void store(const std::string &a)
69 std::istringstream ss(a);
72 throw UsageError("Invalid argument for --"+lng);
81 class ListOption: public OptBase
84 ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d)
85 { if(arg_type!=REQUIRED_ARG) throw Exception("ListOption with arg_type!=REQUIRED makes no sense"); }
87 virtual void store() { }
89 virtual void store(const std::string &a)
91 typename T::value_type tmp;
92 std::istringstream ss(a);
95 throw UsageError("Invalid argument for --"+lng);
103 std::list<OptBase *> opts;
104 std::vector<std::string> args;
109 const std::vector<std::string> &get_args() const { return args; }
112 OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
113 { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
116 OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
117 { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
120 OptBase &add_option(const std::string &l, T &d, ArgType a)
121 { return add_option(0, l, d, a); }
124 OptBase &get_option(char);
125 OptBase &get_option(const std::string &);
128 /** Processes argc/argv style command line arguments. The contents of argv
129 will be unchanged; use get_args to access non-option arguments. */
130 void operator()(unsigned, const char *const *);
133 /** Processes a long option. Returns the number of arguments eaten. */
134 unsigned process_long(const char *const *);
136 /** Processes short options. Returns the number of arguments eaten. */
137 unsigned process_short(const char *const *);
140 /** Generates a single line that describes known options. */
141 std::string generate_usage(const std::string &) const;
143 /** Generates help for known options in tabular format, one option per
144 line. The returned string will have a linefeed at the end. */
145 std::string generate_help() const;
148 template<> inline void GetOpt::Option<bool>::store() { data = true; }
149 template<> inline void GetOpt::Option<unsigned>::store() { ++data; }
151 template<> inline void GetOpt::Option<std::string>::store(const std::string &a)
154 template<> inline void GetOpt::ListOption<std::list<std::string> >::store(const std::string &a)
155 { data.push_back(a); }