1 #ifndef MSP_CORE_GETOPT_H_
2 #define MSP_CORE_GETOPT_H_
8 #include <msp/strings/lexicalcast.h>
12 class usage_error: public std::runtime_error
18 usage_error(const std::string &w, const std::string &h = std::string()): std::runtime_error(w), help_(h) { }
19 ~usage_error() throw() { }
21 const char *help() const throw() { return help_.c_str(); }
45 OptBase(char, const std::string &, ArgType);
47 virtual ~OptBase() { }
49 OptBase &set_help(const std::string &);
50 OptBase &set_help(const std::string &, const std::string &);
51 char get_short() const { return shrt; }
52 const std::string &get_long() const { return lng; }
53 ArgType get_arg_type() const { return arg_type; }
54 const std::string &get_help() const { return help; }
55 const std::string &get_metavar() const { return metavar; }
56 unsigned get_seen_count() const { return seen_count; }
58 void process(const std::string &);
60 virtual void store() = 0;
61 virtual void store(const std::string &) = 0;
66 class Option: public OptBase
72 Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { }
74 virtual void store() { }
76 virtual void store(const std::string &a)
80 data = lexical_cast<T>(a);
82 catch(const lexical_error &e)
84 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
90 class ListOption: public OptBase
96 ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d)
97 { if(arg_type!=REQUIRED_ARG) throw std::invalid_argument("ListOption arg_type!=REQUIRED"); }
99 virtual void store() { }
101 virtual void store(const std::string &a)
105 data.push_back(lexical_cast<typename T::value_type>(a));
107 catch(const lexical_error &e)
109 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
115 std::list<OptBase *> opts;
116 std::vector<std::string> args;
122 const std::vector<std::string> &get_args() const { return args; }
125 OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
126 { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
129 OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
130 { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
133 OptBase &add_option(const std::string &l, T &d, ArgType a)
134 { return add_option(0, l, d, a); }
137 OptBase &get_option(char);
138 OptBase &get_option(const std::string &);
141 /** Processes argc/argv style command line arguments. The contents of argv
142 will be unchanged; use get_args to access non-option arguments. */
143 void operator()(unsigned, const char *const *);
146 /** Processes a long option. Returns the number of arguments eaten. */
147 unsigned process_long(const char *const *);
149 /** Processes short options. Returns the number of arguments eaten. */
150 unsigned process_short(const char *const *);
153 /** Generates a single line that describes known options. */
154 std::string generate_usage(const std::string &) const;
156 /** Generates help for known options in tabular format, one option per
157 line. The returned string will have a linefeed at the end. */
158 std::string generate_help() const;
161 template<> inline void GetOpt::Option<bool>::store() { data = true; }
162 template<> inline void GetOpt::Option<unsigned>::store() { ++data; }
164 template<> inline void GetOpt::Option<std::string>::store(const std::string &a)
167 template<> inline void GetOpt::ListOption<std::list<std::string> >::store(const std::string &a)
168 { data.push_back(a); }