/** * Copyright (C) 2010 Johannes Weißl * License: your favourite BSD-style license * * git clone http://github.com/weisslj/cpp-optparse.git * * This is yet another option parser for C++. It is modelled after the * excellent Python optparse API. Although incomplete, anyone familiar to * optparse should feel at home: * http://docs.python.org/library/optparse.html * * Design decisions: * - elegant and easy usage more important than speed / flexibility * - shortness more important than feature completeness * * no unicode * * no checking for user programming errors * * Why not use getopt/getopt_long? * - not C++ / not completely POSIX * - too cumbersome to use, would need lot of additional code * * Why not use Boost.Program_options? * - boost not installed on all target platforms (esp. cluster, HPC, ...) * - too big to include just for option handling: * 322 *.h (44750 lines) + 7 *.cpp (2078 lines) * * Why not use tclap/Opag/Options/CmdLine/Anyoption/Argument_helper/...? * - no reason, writing one is faster than code inspection :-) * - similarity to Python desired for faster learning curve * * Future work: * - nargs > 1? * - comments? * * Python only features: * - conflict handlers * - adding new actions * * * Example: * * using optparse::OptionParser; * * OptionParser parser = OptionParser() .description("just an example"); * * parser.add_option("-f", "--file") .dest("filename") * .help("write report to FILE") .metavar("FILE"); * parser.add_option("-q", "--quiet") * .action("store_false") .dest("verbose") .set_default("1") * .help("don't print status messages to stdout"); * * optparse::Values options = parser.parse_args(argc, argv); * vector args = parser.args(); * * if (options.get("verbose")) * cout << options["filename"] << endl; * */ #ifndef OPTIONPARSER_H_ #define OPTIONPARSER_H_ #include #include #include #include #include #include #include #ifdef _MSC_VER # pragma warning(push) # pragma warning(disable: 4512) #endif namespace optparse { class OptionParser; class OptionGroup; class Option; class Values; class Value; class Callback; typedef std::map strMap; typedef std::map > lstMap; typedef std::map optMap; const char* const SUPPRESS_HELP = "SUPPRESS" "HELP"; const char* const SUPPRESS_USAGE = "SUPPRESS" "USAGE"; //! Class for automatic conversion from string -> anytype class Value { public: Value() : str(), valid(false) {} Value(const std::string& v) : str(v), valid(true) {} operator const char*() { return str.c_str(); } operator bool() { bool t; return (valid && (std::istringstream(str) >> t)) ? t : false; } operator short() { short t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator unsigned short() { unsigned short t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator int() { int t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator unsigned int() { unsigned int t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator long() { long t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator unsigned long() { unsigned long t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator float() { float t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator double() { double t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } operator long double() { long double t; return (valid && (std::istringstream(str) >> t)) ? t : 0; } private: const std::string str; bool valid; }; class Values { public: Values() : _map() {} const std::string& operator[] (const std::string& d) const; std::string& operator[] (const std::string& d) { return _map[d]; } bool is_set(const std::string& d) const { return _map.find(d) != _map.end(); } bool is_set_by_user(const std::string& d) const { return _userSet.find(d) != _userSet.end(); } void is_set_by_user(const std::string& d, bool yes); Value get(const std::string& d) const { return (is_set(d)) ? Value((*this)[d]) : Value(); } typedef std::list::iterator iterator; typedef std::list::const_iterator const_iterator; std::list& all(const std::string& d) { return _appendMap[d]; } const std::list& all(const std::string& d) const { return _appendMap.find(d)->second; } private: strMap _map; lstMap _appendMap; std::set _userSet; }; class OptionParser { public: OptionParser(); virtual ~OptionParser() {} OptionParser& usage(const std::string& u) { set_usage(u); return *this; } OptionParser& version(const std::string& v) { _version = v; return *this; } OptionParser& description(const std::string& d) { _description = d; return *this; } OptionParser& add_help_option(bool h) { _add_help_option = h; return *this; } OptionParser& add_version_option(bool v) { _add_version_option = v; return *this; } OptionParser& prog(const std::string& p) { _prog = p; return *this; } OptionParser& epilog(const std::string& e) { _epilog = e; return *this; } OptionParser& set_defaults(const std::string& dest, const std::string& val) { _defaults[dest] = val; return *this; } OptionParser& enable_interspersed_args() { _interspersed_args = true; return *this; } OptionParser& disable_interspersed_args() { _interspersed_args = false; return *this; } OptionParser& add_option_group(const OptionGroup& group); const std::string& usage() const { return _usage; } const std::string& version() const { return _version; } const std::string& description() const { return _description; } bool add_help_option() const { return _add_help_option; } bool add_version_option() const { return _add_version_option; } const std::string& prog() const { return _prog; } const std::string& epilog() const { return _epilog; } bool interspersed_args() const { return _interspersed_args; } Option& add_option(const std::string& opt); Option& add_option(const std::string& opt1, const std::string& opt2); Option& add_option(const std::string& opt1, const std::string& opt2, const std::string& opt3); Option& add_option(const std::vector& opt); Values& parse_args(int argc, char const* const* argv); Values& parse_args(const std::vector& args); template Values& parse_args(InputIterator begin, InputIterator end) { return parse_args(std::vector(begin, end)); } const std::list& args() const { return _leftover; } std::vector args() { return std::vector(_leftover.begin(), _leftover.end()); } std::string format_help() const; std::string format_option_help(unsigned int indent = 2) const; void print_help() const; void set_usage(const std::string& u); std::string get_usage() const; void print_usage(std::ostream& out) const; void print_usage() const; std::string get_version() const; void print_version(std::ostream& out) const; void print_version() const; void error(const std::string& msg) const; void exit() const; private: const Option& lookup_short_opt(const std::string& opt) const; const Option& lookup_long_opt(const std::string& opt) const; void handle_short_opt(const std::string& opt, const std::string& arg); void handle_long_opt(const std::string& optstr); void process_opt(const Option& option, const std::string& opt, const std::string& value); std::string format_usage(const std::string& u) const; std::string _usage; std::string _version; std::string _description; bool _add_help_option; bool _add_version_option; std::string _prog; std::string _epilog; bool _interspersed_args; Values _values; std::list