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
69 Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { }
71 virtual void store() { }
73 virtual void store(const std::string &a)
77 data = lexical_cast<T>(a);
79 catch(const lexical_error &e)
81 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
89 class ListOption: public OptBase
92 ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d)
93 { if(arg_type!=REQUIRED_ARG) throw std::invalid_argument("ListOption arg_type!=REQUIRED"); }
95 virtual void store() { }
97 virtual void store(const std::string &a)
101 data.push_back(lexical_cast<typename T::value_type>(a));
103 catch(const lexical_error &e)
105 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
113 std::list<OptBase *> opts;
114 std::vector<std::string> args;
120 const std::vector<std::string> &get_args() const { return args; }
123 OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
124 { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
127 OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
128 { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
131 OptBase &add_option(const std::string &l, T &d, ArgType a)
132 { return add_option(0, l, d, a); }
135 OptBase &get_option(char);
136 OptBase &get_option(const std::string &);
139 /** Processes argc/argv style command line arguments. The contents of argv
140 will be unchanged; use get_args to access non-option arguments. */
141 void operator()(unsigned, const char *const *);
144 /** Processes a long option. Returns the number of arguments eaten. */
145 unsigned process_long(const char *const *);
147 /** Processes short options. Returns the number of arguments eaten. */
148 unsigned process_short(const char *const *);
151 /** Generates a single line that describes known options. */
152 std::string generate_usage(const std::string &) const;
154 /** Generates help for known options in tabular format, one option per
155 line. The returned string will have a linefeed at the end. */
156 std::string generate_help() const;
159 template<> inline void GetOpt::Option<bool>::store() { data = true; }
160 template<> inline void GetOpt::Option<unsigned>::store() { ++data; }
162 template<> inline void GetOpt::Option<std::string>::store(const std::string &a)
165 template<> inline void GetOpt::ListOption<std::list<std::string> >::store(const std::string &a)
166 { data.push_back(a); }