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(); }
26 Command line option processor. Both short and long options are supported, with
27 optional and required arguments. Automatic help text generation is also
30 Short options begin with a single dash and are identified by a single letter.
31 Multiple short options may be grouped if they take no arguments; for example,
32 the string "-abc" could be interpreted as having the options 'a', 'b' and 'c'.
33 If the option takes an argument and there are unused characters in the argv
34 element, then those characters are interpreted as the argument. Otherwise the
35 next element is taken as the argument. An optional argument must be given in
38 Long options begin with a double dash and are identified by an arbitrary
39 string. An argument can be specified either in the same argv element,
40 separated by an equals sign, or in the next element. As with short options,
41 an optional argument must be in the same element.
43 A single option may have both alternative forms, but must always have at least
44 a long form. This is to encourage self-documenting options; it's much easier
45 to remember words than letters.
47 A built-in --help option is provided and will output a list of options and
48 their associated help texts. An application may override this by providing
49 its own option with the same name.
68 /// Sets help text for the option.
69 virtual Option &set_help(const std::string &) = 0;
71 /** Sets help text for the option, with a placeholder metavariable. The
72 metavariable is used to denote the argument in the option list. */
73 virtual Option &set_help(const std::string &, const std::string &) = 0;
75 virtual Option &bind_seen_count(unsigned &) = 0;
77 /// Returns the number of times this option was seen on the command line.
78 virtual unsigned get_seen_count() const = 0;
89 virtual Store *clone() const = 0;
91 virtual void store() = 0;
92 virtual void store(const std::string &) = 0;
95 class OptionImpl: public Option
102 unsigned *ext_seen_count;
108 OptionImpl(char, const std::string &, const Store &, ArgType);
109 virtual ~OptionImpl();
111 virtual OptionImpl &set_help(const std::string &);
112 virtual OptionImpl &set_help(const std::string &, const std::string &);
113 virtual OptionImpl &bind_seen_count(unsigned &);
114 char get_short() const { return shrt; }
115 const std::string &get_long() const { return lng; }
116 ArgType get_arg_type() const { return arg_type; }
117 const std::string &get_help() const { return help; }
118 const std::string &get_metavar() const { return metavar; }
119 virtual unsigned get_seen_count() const { return seen_count; }
121 void process(const std::string &);
125 class SimpleStore: public Store
131 SimpleStore(T &d): data(d) { }
133 virtual SimpleStore *clone() const
134 { return new SimpleStore(data); }
136 virtual void store() { }
138 virtual void store(const std::string &a)
139 { data = lexical_cast<T>(a); }
143 class ListStore: public Store
149 ListStore(T &d): data(d) { }
151 virtual ListStore *clone() const
152 { return new ListStore(data); }
154 virtual void store() { }
156 virtual void store(const std::string &a)
157 { data.push_back(lexical_cast<typename T::value_type>(a)); }
160 typedef std::list<OptionImpl *> OptionList;
164 std::vector<std::string> args;
170 /// Returns any non-option arguments encountered during processing.
171 const std::vector<std::string> &get_args() const { return args; }
173 /** Adds an option with both short and long forms. Processing depends on
174 the type of the destination variable and whether an argument is taken or
175 not. With an argument, the value is lexical_cast to appropriate type and
176 stored in the destination. Without an argument, a bool will be set to true
177 and an unsigned will be incremented; any other type will be ignored. */
179 Option &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
180 { return add_option(s, l, SimpleStore<T>(d), a); }
182 /** Adds an option with both short and long forms. The option may be
183 specified multiple times, and the argument from each occurrence is stored in
184 the list. The argument type must be REQUIRED_ARG. */
186 Option &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
187 { return add_option(s, l, ListStore<std::list<T> >(d), a); }
189 /** Adds an option with only a long form. */
191 Option &add_option(const std::string &l, T &d, ArgType a)
192 { return add_option(0, l, d, a); }
195 OptionImpl &add_option(char, const std::string &, const Store &, ArgType);
197 OptionImpl &get_option(char);
198 OptionImpl &get_option(const std::string &);
201 /** Processes argc/argv style command line arguments. The contents of argv
202 will be unchanged; use get_args to access non-option arguments. */
203 void operator()(unsigned, const char *const *);
206 /** Processes a long option. Returns the number of arguments eaten. */
207 unsigned process_long(const char *const *);
209 /** Processes short options. Returns the number of arguments eaten. */
210 unsigned process_short(const char *const *);
213 /** Generates a single line that describes known options. */
214 std::string generate_usage(const std::string &) const;
216 /** Generates help for known options in tabular format, one option per
217 line. The returned string will have a linefeed at the end. */
218 std::string generate_help() const;
221 template<> inline void GetOpt::SimpleStore<bool>::store()
224 template<> inline void GetOpt::SimpleStore<unsigned>::store()
227 template<> inline void GetOpt::SimpleStore<std::string>::store(const std::string &a)
230 template<> inline void GetOpt::ListStore<std::list<std::string> >::store(const std::string &a)
231 { data.push_back(a); }