diff options
author | Evan Lojewski <github@meklort.com> | 2019-02-12 20:01:37 -0700 |
---|---|---|
committer | Evan Lojewski <github@meklort.com> | 2019-02-12 20:01:37 -0700 |
commit | fc9aa66abcccd78877cd7ec05a1bedb167b35418 (patch) | |
tree | 7a1f4711e497fdcee125432babc1f88da6df733f /libs | |
parent | f3cd76c858634452f6309c774079266c1958c083 (diff) | |
download | bcm5719-ortega-fc9aa66abcccd78877cd7ec05a1bedb167b35418.tar.gz bcm5719-ortega-fc9aa66abcccd78877cd7ec05a1bedb167b35418.zip |
Import option parsing library from https://github.com/weisslj/cpp-optparse
Diffstat (limited to 'libs')
-rw-r--r-- | libs/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libs/OptParse/LICENSE | 21 | ||||
-rw-r--r-- | libs/OptParse/Makefile | 30 | ||||
-rw-r--r-- | libs/OptParse/OptionParser.cpp | 590 | ||||
-rw-r--r-- | libs/OptParse/OptionParser.h | 279 | ||||
-rw-r--r-- | libs/OptParse/README.md | 53 | ||||
-rwxr-xr-x | libs/OptParse/t/testprog | 153 | ||||
-rwxr-xr-x | libs/OptParse/test.sh | 87 | ||||
-rw-r--r-- | libs/OptParse/testprog.cpp | 183 |
9 files changed, 1398 insertions, 0 deletions
diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt index 2352d41..1093020 100644 --- a/libs/CMakeLists.txt +++ b/libs/CMakeLists.txt @@ -47,4 +47,6 @@ add_subdirectory(NVRam) add_subdirectory(MII) add_subdirectory(VPD) +add_subdirectory(OptParse) + add_subdirectory(bcm5719) diff --git a/libs/OptParse/LICENSE b/libs/OptParse/LICENSE new file mode 100644 index 0000000..995e407 --- /dev/null +++ b/libs/OptParse/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2010 Johannes Weißl + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/libs/OptParse/Makefile b/libs/OptParse/Makefile new file mode 100644 index 0000000..19cab9d --- /dev/null +++ b/libs/OptParse/Makefile @@ -0,0 +1,30 @@ +ifeq ($(WARN),1) +ifeq ($(CXX),g++) +WARN_FLAGS = -O3 -g -Wall -Wextra -Werror # -Weffc++ +else ifeq ($(CXX),clang++) +WARN_FLAGS = -O3 -g -Wall -Wextra -Werror +else ifeq ($(CXX),icpc) +WARN_FLAGS = -O3 -ipo -g -Wall -wd981 -wd383 -wd2259 -Werror # -Weffc++ +endif +endif + +ifeq ($(CPP11),1) +STD_FLAGS = -std=c++0x +endif + +BIN = testprog +OBJECTS = OptionParser.o testprog.o + +$(BIN): $(OBJECTS) + $(CXX) -o $@ $(OBJECTS) $(WARN_FLAGS) $(STD_FLAGS) $(LINKFLAGS) + +%.o: %.cpp OptionParser.h + $(CXX) $(WARN_FLAGS) $(STD_FLAGS) $(CXXFLAGS) -c $< -o $@ + +.PHONY: clean test + +test: testprog + ./test.sh + +clean: + rm -f *.o $(BIN) diff --git a/libs/OptParse/OptionParser.cpp b/libs/OptParse/OptionParser.cpp new file mode 100644 index 0000000..d850052 --- /dev/null +++ b/libs/OptParse/OptionParser.cpp @@ -0,0 +1,590 @@ +/** + * Copyright (C) 2010 Johannes Weißl <jargon@molb.org> + * License: MIT License + * URL: https://github.com/weisslj/cpp-optparse + */ + +#include "OptionParser.h" + +#include <cstdlib> +#include <algorithm> +#include <complex> +#include <ciso646> + +#if defined(ENABLE_NLS) && ENABLE_NLS +# include <libintl.h> +# define _(s) gettext(s) +#else +# define _(s) ((const char *) (s)) +#endif + +using namespace std; + +namespace optparse { + +////////// auxiliary (string) functions { ////////// +class str_wrap { +public: + str_wrap(const string& l, const string& r) : lwrap(l), rwrap(r) {} + str_wrap(const string& w) : lwrap(w), rwrap(w) {} + string operator() (const string& s) { return lwrap + s + rwrap; } + const string lwrap, rwrap; +}; +template<typename InputIterator, typename UnaryOperator> +static string str_join_trans(const string& sep, InputIterator begin, InputIterator end, UnaryOperator op) { + string buf; + for (InputIterator it = begin; it != end; ++it) { + if (it != begin) + buf += sep; + buf += op(*it); + } + return buf; +} +template<class InputIterator> +static string str_join(const string& sep, InputIterator begin, InputIterator end) { + return str_join_trans(sep, begin, end, str_wrap("")); +} +static string& str_replace(string& s, const string& patt, const string& repl) { + size_t pos = 0, n = patt.length(); + while (true) { + pos = s.find(patt, pos); + if (pos == string::npos) + break; + s.replace(pos, n, repl); + pos += repl.size(); + } + return s; +} +static string str_replace(const string& s, const string& patt, const string& repl) { + string tmp = s; + str_replace(tmp, patt, repl); + return tmp; +} +static string str_format(const string& str, size_t pre, size_t len, bool running_text = true, bool indent_first = true) { + string s = str; + stringstream ss; + string p; + len -= 2; // Python seems to not use full length + if (running_text) + replace(s.begin(), s.end(), '\n', ' '); + if (indent_first) + p = string(pre, ' '); + + size_t pos = 0, linestart = 0; + size_t line = 0; + while (true) { + bool wrap = false; + + size_t new_pos = s.find_first_of(" \n\t", pos); + if (new_pos == string::npos) + break; + if (s[new_pos] == '\n') { + pos = new_pos + 1; + wrap = true; + } + if (line == 1) + p = string(pre, ' '); + if (wrap || new_pos + pre > linestart + len) { + ss << p << s.substr(linestart, pos - linestart - 1) << endl; + linestart = pos; + line++; + } + pos = new_pos + 1; + } + ss << p << s.substr(linestart) << endl; + return ss.str(); +} +static string str_inc(const string& s) { + stringstream ss; + string v = (s != "") ? s : "0"; + long i; + istringstream(v) >> i; + ss << i+1; + return ss.str(); +} +static unsigned int cols() { + unsigned int n = 80; +#ifndef _WIN32 + const char *s = getenv("COLUMNS"); + if (s) + istringstream(s) >> n; +#endif + return n; +} +static string basename(const string& s) { + string b = s; + size_t i = b.find_last_not_of('/'); + if (i == string::npos) { + if (b[0] == '/') + b.erase(1); + return b; + } + b.erase(i+1, b.length()-i-1); + i = b.find_last_of("/"); + if (i != string::npos) + b.erase(0, i+1); + return b; +} +////////// } auxiliary (string) functions ////////// + + +////////// class OptionContainer { ////////// +Option& OptionContainer::add_option(const string& opt) { + const string tmp[1] = { opt }; + return add_option(vector<string>(&tmp[0], &tmp[1])); +} +Option& OptionContainer::add_option(const string& opt1, const string& opt2) { + const string tmp[2] = { opt1, opt2 }; + return add_option(vector<string>(&tmp[0], &tmp[2])); +} +Option& OptionContainer::add_option(const string& opt1, const string& opt2, const string& opt3) { + const string tmp[3] = { opt1, opt2, opt3 }; + return add_option(vector<string>(&tmp[0], &tmp[3])); +} +Option& OptionContainer::add_option(const vector<string>& v) { + _opts.resize(_opts.size()+1, Option(get_parser())); + Option& option = _opts.back(); + string dest_fallback; + for (vector<string>::const_iterator it = v.begin(); it != v.end(); ++it) { + if (it->substr(0,2) == "--") { + const string s = it->substr(2); + if (option.dest() == "") + option.dest(str_replace(s, "-", "_")); + option._long_opts.insert(s); + _optmap_l[s] = &option; + } else { + const string s = it->substr(1,1); + if (dest_fallback == "") + dest_fallback = s; + option._short_opts.insert(s); + _optmap_s[s] = &option; + } + } + if (option.dest() == "") + option.dest(dest_fallback); + return option; +} +string OptionContainer::format_option_help(unsigned int indent /* = 2 */) const { + stringstream ss; + + if (_opts.empty()) + return ss.str(); + + for (list<Option>::const_iterator it = _opts.begin(); it != _opts.end(); ++it) { + if (it->help() != SUPPRESS_HELP) + ss << it->format_help(indent); + } + + return ss.str(); +} +////////// } class OptionContainer ////////// + +////////// class OptionParser { ////////// +OptionParser::OptionParser() : + OptionContainer(), + _usage(_("%prog [options]")), + _add_help_option(true), + _add_version_option(true), + _interspersed_args(true) {} + +OptionParser& OptionParser::add_option_group(const OptionGroup& group) { + for (list<Option>::const_iterator oit = group._opts.begin(); oit != group._opts.end(); ++oit) { + const Option& option = *oit; + for (set<string>::const_iterator it = option._short_opts.begin(); it != option._short_opts.end(); ++it) + _optmap_s[*it] = &option; + for (set<string>::const_iterator it = option._long_opts.begin(); it != option._long_opts.end(); ++it) + _optmap_l[*it] = &option; + } + _groups.push_back(&group); + return *this; +} + +const Option& OptionParser::lookup_short_opt(const string& opt) const { + optMap::const_iterator it = _optmap_s.find(opt); + if (it == _optmap_s.end()) + error(_("no such option") + string(": -") + opt); + return *it->second; +} + +void OptionParser::handle_short_opt(const string& opt, const string& arg) { + + _remaining.pop_front(); + string value; + + const Option& option = lookup_short_opt(opt); + if (option._nargs == 1) { + value = arg.substr(2); + if (value == "") { + if (_remaining.empty()) + error("-" + opt + " " + _("option requires an argument")); + value = _remaining.front(); + _remaining.pop_front(); + } + } else { + if (arg.length() > 2) + _remaining.push_front(string("-") + arg.substr(2)); + } + + process_opt(option, string("-") + opt, value); +} + +const Option& OptionParser::lookup_long_opt(const string& opt) const { + + list<string> matching; + for (optMap::const_iterator it = _optmap_l.begin(); it != _optmap_l.end(); ++it) { + if (it->first.compare(0, opt.length(), opt) == 0) { + matching.push_back(it->first); + if (it->first.length() == opt.length()) + break; + } + } + if (matching.size() > 1) { + string x = str_join_trans(", ", matching.begin(), matching.end(), str_wrap("--", "")); + error(_("ambiguous option") + string(": --") + opt + " (" + x + "?)"); + } + if (matching.size() == 0) + error(_("no such option") + string(": --") + opt); + + return *_optmap_l.find(matching.front())->second; +} + +void OptionParser::handle_long_opt(const string& optstr) { + + _remaining.pop_front(); + string opt, value; + + size_t delim = optstr.find("="); + if (delim != string::npos) { + opt = optstr.substr(0, delim); + value = optstr.substr(delim+1); + } else + opt = optstr; + + const Option& option = lookup_long_opt(opt); + if (option._nargs == 1 and delim == string::npos) { + if (not _remaining.empty()) { + value = _remaining.front(); + _remaining.pop_front(); + } + } + + if (option._nargs == 1 and value == "") + error("--" + opt + " " + _("option requires an argument")); + + process_opt(option, string("--") + opt, value); +} + +Values& OptionParser::parse_args(const int argc, char const* const* const argv) { + if (prog() == "") + prog(basename(argv[0])); + return parse_args(&argv[1], &argv[argc]); +} +Values& OptionParser::parse_args(const vector<string>& v) { + + _remaining.assign(v.begin(), v.end()); + + if (add_help_option()) { + add_option("-h", "--help") .action("help") .help(_("show this help message and exit")); + _opts.splice(_opts.begin(), _opts, --(_opts.end())); + } + if (add_version_option() and version() != "") { + add_option("--version") .action("version") .help(_("show program's version number and exit")); + _opts.splice(_opts.begin(), _opts, --(_opts.end())); + } + + while (not _remaining.empty()) { + const string arg = _remaining.front(); + + if (arg == "--") { + _remaining.pop_front(); + break; + } + + if (arg.substr(0,2) == "--") { + handle_long_opt(arg.substr(2)); + } else if (arg.substr(0,1) == "-" and arg.length() > 1) { + handle_short_opt(arg.substr(1,1), arg); + } else { + _remaining.pop_front(); + _leftover.push_back(arg); + if (not interspersed_args()) + break; + } + } + while (not _remaining.empty()) { + const string arg = _remaining.front(); + _remaining.pop_front(); + _leftover.push_back(arg); + } + + for (list<Option>::const_iterator it = _opts.begin(); it != _opts.end(); ++it) { + if (it->get_default() != "" and not _values.is_set(it->dest())) + _values[it->dest()] = it->get_default(); + } + + for (list<OptionGroup const*>::iterator group_it = _groups.begin(); group_it != _groups.end(); ++group_it) { + for (list<Option>::const_iterator it = (*group_it)->_opts.begin(); it != (*group_it)->_opts.end(); ++it) { + if (it->get_default() != "" and not _values.is_set(it->dest())) + _values[it->dest()] = it->get_default(); + } + } + + return _values; +} + +void OptionParser::process_opt(const Option& o, const string& opt, const string& value) { + if (o.action() == "store") { + string err = o.check_type(opt, value); + if (err != "") + error(err); + _values[o.dest()] = value; + _values.is_set_by_user(o.dest(), true); + } + else if (o.action() == "store_const") { + _values[o.dest()] = o.get_const(); + _values.is_set_by_user(o.dest(), true); + } + else if (o.action() == "store_true") { + _values[o.dest()] = "1"; + _values.is_set_by_user(o.dest(), true); + } + else if (o.action() == "store_false") { + _values[o.dest()] = "0"; + _values.is_set_by_user(o.dest(), true); + } + else if (o.action() == "append") { + string err = o.check_type(opt, value); + if (err != "") + error(err); + _values[o.dest()] = value; + _values.all(o.dest()).push_back(value); + _values.is_set_by_user(o.dest(), true); + } + else if (o.action() == "append_const") { + _values[o.dest()] = o.get_const(); + _values.all(o.dest()).push_back(o.get_const()); + _values.is_set_by_user(o.dest(), true); + } + else if (o.action() == "count") { + _values[o.dest()] = str_inc(_values[o.dest()]); + _values.is_set_by_user(o.dest(), true); + } + else if (o.action() == "help") { + print_help(); + std::exit(0); + } + else if (o.action() == "version") { + print_version(); + std::exit(0); + } + else if (o.action() == "callback" && o.callback()) { + string err = o.check_type(opt, value); + if (err != "") + error(err); + (*o.callback())(o, opt, value, *this); + } +} + +string OptionParser::format_help() const { + stringstream ss; + + if (usage() != SUPPRESS_USAGE) + ss << get_usage() << endl; + + if (description() != "") + ss << str_format(description(), 0, cols()) << endl; + + ss << _("Options") << ":" << endl; + ss << format_option_help(); + + for (list<OptionGroup const*>::const_iterator it = _groups.begin(); it != _groups.end(); ++it) { + const OptionGroup& group = **it; + ss << endl << " " << group.title() << ":" << endl; + if (group.description() != "") { + unsigned int malus = 4; // Python seems to not use full length + ss << str_format(group.description(), 4, cols() - malus) << endl; + } + ss << group.format_option_help(4); + } + + if (epilog() != "") + ss << endl << str_format(epilog(), 0, cols()); + + return ss.str(); +} +void OptionParser::print_help() const { + cout << format_help(); +} + +void OptionParser::set_usage(const string& u) { + string lower = u; + transform(lower.begin(), lower.end(), lower.begin(), ::tolower); + if (lower.compare(0, 7, "usage: ") == 0) + _usage = u.substr(7); + else + _usage = u; +} +string OptionParser::format_usage(const string& u) const { + stringstream ss; + ss << _("Usage") << ": " << u << endl; + return ss.str(); +} +string OptionParser::get_usage() const { + if (usage() == SUPPRESS_USAGE) + return string(""); + return format_usage(str_replace(usage(), "%prog", prog())); +} +void OptionParser::print_usage(ostream& out) const { + string u = get_usage(); + if (u != "") + out << u << endl; +} +void OptionParser::print_usage() const { + print_usage(cout); +} + +string OptionParser::get_version() const { + return str_replace(_version, "%prog", prog()); +} +void OptionParser::print_version(ostream& out) const { + out << get_version() << endl; +} +void OptionParser::print_version() const { + print_version(cout); +} + +void OptionParser::exit() const { + throw 2; +} +void OptionParser::error(const string& msg) const { + print_usage(cerr); + cerr << prog() << ": " << _("error") << ": " << msg << endl; + exit(); +} +////////// } class OptionParser ////////// + +////////// class Values { ////////// +const string& Values::operator[] (const string& d) const { + strMap::const_iterator it = _map.find(d); + static const string empty = ""; + return (it != _map.end()) ? it->second : empty; +} +void Values::is_set_by_user(const string& d, bool yes) { + if (yes) + _userSet.insert(d); + else + _userSet.erase(d); +} +////////// } class Values ////////// + +////////// class Option { ////////// +string Option::check_type(const string& opt, const string& val) const { + istringstream ss(val); + stringstream err; + + if (type() == "int" || type() == "long") { + long t; + if (not (ss >> t)) + err << _("option") << " " << opt << ": " << _("invalid integer value") << ": '" << val << "'"; + } + else if (type() == "float" || type() == "double") { + double t; + if (not (ss >> t)) + err << _("option") << " " << opt << ": " << _("invalid floating-point value") << ": '" << val << "'"; + } + else if (type() == "choice") { + if (find(choices().begin(), choices().end(), val) == choices().end()) { + list<string> tmp = choices(); + transform(tmp.begin(), tmp.end(), tmp.begin(), str_wrap("'")); + err << _("option") << " " << opt << ": " << _("invalid choice") << ": '" << val << "'" + << " (" << _("choose from") << " " << str_join(", ", tmp.begin(), tmp.end()) << ")"; + } + } + else if (type() == "complex") { + complex<double> t; + if (not (ss >> t)) + err << _("option") << " " << opt << ": " << _("invalid complex value") << ": '" << val << "'"; + } + + return err.str(); +} + +string Option::format_option_help(unsigned int indent /* = 2 */) const { + + string mvar_short, mvar_long; + if (nargs() == 1) { + string mvar = metavar(); + if (mvar == "") { + mvar = dest(); + transform(mvar.begin(), mvar.end(), mvar.begin(), ::toupper); + } + mvar_short = " " + mvar; + mvar_long = "=" + mvar; + } + + stringstream ss; + ss << string(indent, ' '); + + if (not _short_opts.empty()) { + ss << str_join_trans(", ", _short_opts.begin(), _short_opts.end(), str_wrap("-", mvar_short)); + if (not _long_opts.empty()) + ss << ", "; + } + if (not _long_opts.empty()) + ss << str_join_trans(", ", _long_opts.begin(), _long_opts.end(), str_wrap("--", mvar_long)); + + return ss.str(); +} + +string Option::format_help(unsigned int indent /* = 2 */) const { + stringstream ss; + string h = format_option_help(indent); + unsigned int width = cols(); + unsigned int opt_width = min(width*3/10, 36u); + bool indent_first = false; + ss << h; + // if the option list is too long, start a new paragraph + if (h.length() >= (opt_width-1)) { + ss << endl; + indent_first = true; + } else { + ss << string(opt_width - h.length(), ' '); + if (help() == "") + ss << endl; + } + if (help() != "") { + string help_str = (get_default() != "") ? str_replace(help(), "%default", get_default()) : help(); + ss << str_format(help_str, opt_width, width, false, indent_first); + } + return ss.str(); +} + +Option& Option::action(const string& a) { + _action = a; + if (a == "store_const" || a == "store_true" || a == "store_false" || + a == "append_const" || a == "count" || a == "help" || a == "version") { + nargs(0); + } else if (a == "callback") { + nargs(0); + type(""); + } + return *this; +} + + +Option& Option::type(const std::string& t) { + _type = t; + nargs((t == "") ? 0 : 1); + return *this; +} + +const std::string& Option::get_default() const { + strMap::const_iterator it = _parser._defaults.find(dest()); + if (it != _parser._defaults.end()) + return it->second; + else + return _default; +} +////////// } class Option ////////// + +} diff --git a/libs/OptParse/OptionParser.h b/libs/OptParse/OptionParser.h new file mode 100644 index 0000000..1556091 --- /dev/null +++ b/libs/OptParse/OptionParser.h @@ -0,0 +1,279 @@ +/** + * Copyright (C) 2010 Johannes Weißl <jargon@molb.org> + * License: MIT License + * URL: https://github.com/weisslj/cpp-optparse + */ + +#ifndef OPTIONPARSER_H_ +#define OPTIONPARSER_H_ + +#include <string> +#include <vector> +#include <list> +#include <map> +#include <set> +#include <iostream> +#include <sstream> + +namespace optparse { + +class OptionParser; +class OptionGroup; +class Option; +class Values; +class Value; +class Callback; + +typedef std::map<std::string,std::string> strMap; +typedef std::map<std::string,std::list<std::string> > lstMap; +typedef std::map<std::string,Option const*> 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<std::string>::iterator iterator; + typedef std::list<std::string>::const_iterator const_iterator; + std::list<std::string>& all(const std::string& d) { return _appendMap[d]; } + const std::list<std::string>& all(const std::string& d) const { return _appendMap.find(d)->second; } + + private: + strMap _map; + lstMap _appendMap; + std::set<std::string> _userSet; +}; + +class Option { + public: + Option(const OptionParser& p) : + _parser(p), _action("store"), _type("string"), _nargs(1), _callback(0) {} + virtual ~Option() {} + + Option& action(const std::string& a); + Option& type(const std::string& t); + Option& dest(const std::string& d) { _dest = d; return *this; } + Option& set_default(const std::string& d) { _default = d; return *this; } + template<typename T> + Option& set_default(T t) { std::ostringstream ss; ss << t; _default = ss.str(); return *this; } + Option& nargs(size_t n) { _nargs = n; return *this; } + Option& set_const(const std::string& c) { _const = c; return *this; } + template<typename InputIterator> + Option& choices(InputIterator begin, InputIterator end) { + _choices.assign(begin, end); type("choice"); return *this; + } +#if __cplusplus >= 201103L + Option& choices(std::initializer_list<std::string> ilist) { + _choices.assign(ilist); type("choice"); return *this; + } +#endif + Option& help(const std::string& h) { _help = h; return *this; } + Option& metavar(const std::string& m) { _metavar = m; return *this; } + Option& callback(Callback& c) { _callback = &c; return *this; } + + const std::string& action() const { return _action; } + const std::string& type() const { return _type; } + const std::string& dest() const { return _dest; } + const std::string& get_default() const; + size_t nargs() const { return _nargs; } + const std::string& get_const() const { return _const; } + const std::list<std::string>& choices() const { return _choices; } + const std::string& help() const { return _help; } + const std::string& metavar() const { return _metavar; } + Callback* callback() const { return _callback; } + + private: + std::string check_type(const std::string& opt, const std::string& val) const; + std::string format_option_help(unsigned int indent = 2) const; + std::string format_help(unsigned int indent = 2) const; + + const OptionParser& _parser; + + std::set<std::string> _short_opts; + std::set<std::string> _long_opts; + + std::string _action; + std::string _type; + std::string _dest; + std::string _default; + size_t _nargs; + std::string _const; + std::list<std::string> _choices; + std::string _help; + std::string _metavar; + Callback* _callback; + + friend class OptionContainer; + friend class OptionParser; +}; + +class OptionContainer { + public: + OptionContainer(const std::string& d = "") : _description(d) {} + virtual ~OptionContainer() {} + + virtual OptionContainer& description(const std::string& d) { _description = d; return *this; } + virtual const std::string& description() const { return _description; } + + 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<std::string>& opt); + + std::string format_option_help(unsigned int indent = 2) const; + + protected: + std::string _description; + + std::list<Option> _opts; + optMap _optmap_s; + optMap _optmap_l; + + private: + virtual const OptionParser& get_parser() = 0; +}; + +class OptionParser : public OptionContainer { + 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; + } + template<typename T> + OptionParser& set_defaults(const std::string& dest, T t) { std::ostringstream ss; ss << t; _defaults[dest] = ss.str(); 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; } + + Values& parse_args(int argc, char const* const* argv); + Values& parse_args(const std::vector<std::string>& args); + template<typename InputIterator> + Values& parse_args(InputIterator begin, InputIterator end) { + return parse_args(std::vector<std::string>(begin, end)); + } + + const std::list<std::string>& args() const { return _leftover; } + std::vector<std::string> args() { + return std::vector<std::string>(_leftover.begin(), _leftover.end()); + } + + std::string format_help() 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 OptionParser& get_parser() { return *this; } + 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; + bool _add_help_option; + bool _add_version_option; + std::string _prog; + std::string _epilog; + bool _interspersed_args; + + Values _values; + + strMap _defaults; + std::list<OptionGroup const*> _groups; + + std::list<std::string> _remaining; + std::list<std::string> _leftover; + + friend class Option; +}; + +class OptionGroup : public OptionContainer { + public: + OptionGroup(const OptionParser& p, const std::string& t, const std::string& d = "") : + OptionContainer(d), _parser(p), _title(t) {} + virtual ~OptionGroup() {} + + OptionGroup& title(const std::string& t) { _title = t; return *this; } + const std::string& title() const { return _title; } + + private: + const OptionParser& get_parser() { return _parser; } + + const OptionParser& _parser; + std::string _title; + + friend class OptionParser; +}; + +class Callback { +public: + virtual void operator() (const Option& option, const std::string& opt, const std::string& val, const OptionParser& parser) = 0; + virtual ~Callback() {} +}; + +} + +#endif diff --git a/libs/OptParse/README.md b/libs/OptParse/README.md new file mode 100644 index 0000000..d9452fc --- /dev/null +++ b/libs/OptParse/README.md @@ -0,0 +1,53 @@ +# cpp-optparse + +This is yet another option parser for C++. It is modelled after the excellent +Python optparse API. Although incomplete, anyone familiar to +[optparse](http://docs.python.org/library/optparse.html) should feel at home. + +- Copyright (c) 2010 Johannes Weißl +- License: MIT License + +## Design decisions + +- Elegant and easy usage more important than speed / flexibility +- Small size more important than feature completeness, e.g.: + * No unicode + * No checking for user programming errors + * No conflict handlers + * No adding of new actions + +## FAQ + +- 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 for many projects: + 322 *.h (44750 lines) + 7 *.cpp (2078 lines) +- Why not use tclap/Opag/Options/CmdLine/Anyoption/Argument_helper/...? + * Similarity to Python desired for faster learning curve + +## Future work + +- Support nargs > 1? + +## Example + +```cpp +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<string> args = parser.args(); + +if (options.get("verbose")) + cout << options["filename"] << endl; +``` diff --git a/libs/OptParse/t/testprog b/libs/OptParse/t/testprog new file mode 100755 index 0000000..aeae0de --- /dev/null +++ b/libs/OptParse/t/testprog @@ -0,0 +1,153 @@ +#!/usr/bin/env python +# -*- mode: python; coding: utf-8; indent-tabs-mode: nil -*- +# vim: set filetype=python fileencoding=utf-8 expandtab sw=4 sts=4: + +import sys +import os +from optparse import OptionParser, OptionGroup, SUPPRESS_HELP, SUPPRESS_USAGE + +class MyCallback(object): + def __init__(self): + self.counter = 0 + def __call__(self, option, opt, val, parser): + self.counter += 1 + print "--- MyCallback --- " + str(self.counter) + ". time called" + print "--- MyCallback --- option.action(): " + option.action + print "--- MyCallback --- option.type(): " + (option.type if option.type else "") + print "--- MyCallback --- opt: " + opt + print "--- MyCallback --- val: " + (val if val else "") + print "--- MyCallback --- parser.usage(): " + parser.usage + print + +def main(): + usage = \ + "usage: %prog [OPTION]... DIR [FILE]..." \ + if "DISABLE_USAGE" not in os.environ else \ + SUPPRESS_USAGE + version = ( + "%prog 1.0\nCopyright (C) 2010 Johannes Weißl\n" + "License GPLv3+: GNU GPL version 3 or later " + "<http://gnu.org/licenses/gpl.html>.\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law." + ) + desc = ( + "Lorem ipsum dolor sit amet, consectetur adipisicing" + " elit, sed do eiusmod tempor incididunt ut labore et dolore magna" + " aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco" + " laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor" + " in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla" + " pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa" + " qui officia deserunt mollit anim id est laborum." + ) + epilog = ( + "Sed ut perspiciatis unde omnis iste natus error sit" + " voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque" + " ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae" + " dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit" + " aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos" + " qui ratione voluptatem sequi nesciunt.\nNeque porro quisquam est, qui" + " dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia" + " non numquam eius modi tempora incidunt ut labore et dolore magnam" + " aliquam quaerat voluptatem." + ) + + parser = OptionParser( + usage=usage, + version=version, + description=desc, + epilog=epilog + ) + if "DISABLE_INTERSPERSED_ARGS" in os.environ: + parser.disable_interspersed_args() + + parser.set_defaults(verbosity="50") + parser.set_defaults(no_clear=False) + + parser.add_option("--clear", action="store_false", dest="no_clear", help="clear (default)") + parser.add_option("--no-clear", action="store_true", help="not clear") + parser.add_option("--string", + help="This is a really long text... very long indeed! It must be wrapped on normal terminals.") + parser.add_option("-x", "--clause", "--sentence", metavar="SENTENCE", default="I'm a sentence", + help="This is a really long text... very long indeed! It must be wrapped on normal terminals. " + "Also it should appear not on the same line as the option.") + parser.add_option("-k", action="count", help="how many times?") + parser.add_option("--verbose", action="store_const", const="100", dest="verbosity", help="be verbose!") + parser.add_option("-s", "--silent", action="store_const", const="0", dest="verbosity", help="be silent!") + parser.add_option("-n", "--number", type="int", default=1, metavar="NUM", help="number of files (default: %default)") + parser.add_option("-H", action="help", help="alternative help") + parser.add_option("-V", action="version", help="alternative version") + parser.add_option("-i", "--int", action="store", type="int", default=3, help="default: %default") + parser.add_option("-f", "--float", action="store", type="float", default=5.3, help="default: %default") + parser.add_option("-c", "--complex", action="store", type="complex") + choices = ["foo", "bar", "baz"] + parser.add_option("-C", "--choices", choices=choices) + choices_list = ["item1", "item2", "item3"] + parser.add_option("--choices-list", choices=choices_list) + parser.add_option("-m", "--more", action="append") + parser.add_option("--more-milk", action="append_const", const="milk") + parser.add_option("--hidden", help=SUPPRESS_HELP) + + # test for 325cb47 + parser.add_option("--option1", action="store", type="int", default=1) + parser.add_option("--option2", action="store", type="int", default="1") + parser.set_defaults(option1="640") + parser.set_defaults(option2=640) # now works + + mc = MyCallback() + parser.add_option("-K", "--callback", action="callback", callback=mc, help="callback test") + parser.add_option("--string-callback", action="callback", callback=mc, type="string", help="callback test") + + group1 = OptionGroup(parser, "Dangerous Options", + "Caution: use these options at your own risk. " + "It is believed that some of them\nbite.") + group1.add_option("-g", action="store_true", help="Group option.", default=False) + parser.add_option_group(group1) + + group2 = OptionGroup(parser, "Size Options", "Image Size Options.") + group2.add_option("-w", "--width", action="store", type="int", default=640, help="default: %default") + group2.add_option("--height", action="store", type="int", help="default: %default") + parser.set_defaults(height=480) + parser.add_option_group(group2) + + options, args = parser.parse_args() + + print "clear:", ("false" if options.no_clear else "true") + print "string:", options.string if options.string else "" + print "clause:", options.clause + print "k:", options.k if options.k else "" + print "verbosity:", options.verbosity + print "number:", options.number + print "int:", options.int + print "float: %g" % (options.float,) + c = complex(0) + if options.complex is not None: + c = options.complex + print "complex: (%g,%g)" % (c.real, c.imag) + print "choices:", options.choices if options.choices else "" + print "choices-list:", options.choices_list if options.choices_list else "" + print "more:", + print ", ".join(options.more if options.more else []) + + print "more_milk:" + for opt in (options.more_milk if options.more_milk else []): + print "-", opt + + print "hidden:", options.hidden if options.hidden else "" + print "group:", ("true" if options.g else "false") + + print "option1:", options.option1 + print "option2:", options.option2 + + print "width:", options.width + print "height:", options.height + + print + print "leftover arguments: " + for arg in args: + print "arg: " + arg + + return 0 + +if __name__ == "__main__": + main() diff --git a/libs/OptParse/test.sh b/libs/OptParse/test.sh new file mode 100755 index 0000000..a0de60c --- /dev/null +++ b/libs/OptParse/test.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# -*- mode: sh; coding: utf-8; indent-tabs-mode: nil -*- +# vim: set filetype=sh fileencoding=utf-8 expandtab sw=4 sts=4: + +export COLUMNS=80 + +c () { + echo "$(printf "%q " ./testprog "$@")" + local t_stdout_cpp=$(mktemp -t cpp-stdout-optparse.XXXXXXXXXX) + local t_stderr_cpp=$(mktemp -t cpp-stderr-optparse.XXXXXXXXXX) + local t_stdout_pyt=$(mktemp -t pyt-stdout-optparse.XXXXXXXXXX) + local t_stderr_pyt=$(mktemp -t pyt-stderr-optparse.XXXXXXXXXX) + ./testprog "$@" >"$t_stdout_cpp" 2>"$t_stderr_cpp" + status_cpp=$? + ./t/testprog "$@" >"$t_stdout_pyt" 2>"$t_stderr_pyt" + status_pyt=$? + if ! cmp -s "$t_stderr_cpp" "$t_stderr_pyt" ; then + diff -au "$t_stderr_cpp" "$t_stderr_pyt" + exit 1 + fi + rm -f "$t_stderr_cpp" "$t_stderr_pyt" + if ! cmp -s "$t_stdout_cpp" "$t_stdout_pyt" ; then + diff -au "$t_stdout_cpp" "$t_stdout_pyt" + exit 1 + fi + rm -f "$t_stdout_cpp" "$t_stdout_pyt" + if [[ $status_cpp -ne $status_pyt ]] ; then + echo >&2 "status $status_pyt expected, got $status_cpp" + exit 1 + fi +} + +c +c --str # ambiguous option +c -Z # unknown argument +c --argument-does-not-exist +c --version +c -h +c --help +c "foo bar" baz "" +c --clear +c --no-clear +c --clear --no-clear +c --clear --no-clear --clear +c --string "foo bar" +c --string # requires argument +c -x foo +c --clause foo +c --sentence foo +c -k -k -k -k -k +c --verbose +c -s +c --silent +c -v -s +c -n-10 +c -n 300 +c --number=0 +c -H +c -V +c -i-10 +c -i 300 +c --int=0 +# c -i 2.3 # TODO: ignores suffix +c -i no-number +c -f-2.3 +c -f 300 +c --float=0 +c -f no-number +c -c-2.3 +c -c 300 +c --complex=0 +c -c no-number +c -C foo +c --choices baz +c -C wrong-choice +c --choices-list item2 +c --choices-list wrong-item +c -m a -m b +c -m a --more b -m c +c --more-milk --more-milk +c --hidden foo +c -K -K -K +c --string-callback x +c --no-clear foo bar -k -k z -v -n3 "x y" -i 8 -f 3.2 -c 2 +DISABLE_INTERSPERSED_ARGS=1 c -k a -k b +DISABLE_USAGE=1 c --argument-does-not-exist +DISABLE_USAGE=1 c --help diff --git a/libs/OptParse/testprog.cpp b/libs/OptParse/testprog.cpp new file mode 100644 index 0000000..1eb0330 --- /dev/null +++ b/libs/OptParse/testprog.cpp @@ -0,0 +1,183 @@ +#include "OptionParser.h" + +#include <iostream> +#include <cstdlib> +#include <sstream> +#include <string> +#include <complex> +#include <algorithm> + +using namespace std; + +using namespace optparse; + +class Output { +public: + Output(stringstream& ss, const string& d) : stream(ss), delim(d), first(true) {} + void operator() (const string& s) { + if (first) + first = false; + else + stream << delim; + stream << s; + } + stringstream& stream; + const string& delim; + bool first; +}; + +class MyCallback : public optparse::Callback { +public: + MyCallback() : counter(0) {} + void operator() (const Option& option, const string& opt, const string& val, const OptionParser& parser) { + counter++; + cout << "--- MyCallback --- " << counter << ". time called" << endl; + cout << "--- MyCallback --- option.action(): " << option.action() << endl; + cout << "--- MyCallback --- option.type(): " << option.type() << endl; + cout << "--- MyCallback --- opt: " << opt << endl; + cout << "--- MyCallback --- val: " << val << endl; + cout << "--- MyCallback --- parser.usage(): " << parser.usage() << endl; + cout << endl; + } + int counter; +}; + +int main(int argc, char *argv[]) +{ + const string usage = + (!getenv("DISABLE_USAGE")) ? + "usage: %prog [OPTION]... DIR [FILE]..." : SUPPRESS_USAGE; + const string version = "%prog 1.0\nCopyright (C) 2010 Johannes Weißl\n" + "License GPLv3+: GNU GPL version 3 or later " + "<http://gnu.org/licenses/gpl.html>.\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law."; + const string desc = "Lorem ipsum dolor sit amet, consectetur adipisicing" + " elit, sed do eiusmod tempor incididunt ut labore et dolore magna" + " aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco" + " laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor" + " in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla" + " pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa" + " qui officia deserunt mollit anim id est laborum."; + const string epilog = "Sed ut perspiciatis unde omnis iste natus error sit" + " voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque" + " ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae" + " dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit" + " aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos" + " qui ratione voluptatem sequi nesciunt.\nNeque porro quisquam est, qui" + " dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia" + " non numquam eius modi tempora incidunt ut labore et dolore magnam" + " aliquam quaerat voluptatem."; + + OptionParser parser = OptionParser() + .usage(usage) + .version(version) + .description(desc) + .epilog(epilog) + ; + if (getenv("DISABLE_INTERSPERSED_ARGS")) + parser.disable_interspersed_args(); + + parser.set_defaults("verbosity", "50"); + parser.set_defaults("no_clear", "0"); + + // test all actions + parser.add_option("--clear") .action("store_false") .dest("no_clear") .help("clear (default)"); + parser.add_option("--no-clear") .action("store_true") .help("not clear"); + parser.add_option("--string") + .help("This is a really long text... very long indeed! It must be wrapped on normal terminals."); + parser.add_option("-x", "--clause", "--sentence") .metavar("SENTENCE") .set_default("I'm a sentence") + .help("This is a really long text... very long indeed! It must be wrapped on normal terminals. " + "Also it should appear not on the same line as the option."); + parser.add_option("-k") .action("count") .help("how many times?"); + parser.add_option("--verbose") .action("store_const") .set_const("100") .dest("verbosity") .help("be verbose!"); + parser.add_option("-s", "--silent") .action("store_const") .set_const("0") .dest("verbosity") .help("be silent!"); + parser.add_option("-n", "--number") .type("int") .set_default("1") .metavar("NUM") .help("number of files (default: %default)"); + parser.add_option("-H") .action("help") .help("alternative help"); + parser.add_option("-V") .action("version") .help("alternative version"); + parser.add_option("-i", "--int") .action("store") .type("int") .set_default(3) .help("default: %default"); + parser.add_option("-f", "--float") .action("store") .type("float") .set_default(5.3) .help("default: %default"); + parser.add_option("-c", "--complex") .action("store") .type("complex"); + char const* const choices[] = { "foo", "bar", "baz" }; + parser.add_option("-C", "--choices") .choices(&choices[0], &choices[3]); +#if __cplusplus >= 201103L + parser.add_option("--choices-list") .choices({"item1", "item2", "item3"}); +#else + char const* const choices_list[] = { "item1", "item2", "item3" }; + parser.add_option("--choices-list") .choices(&choices_list[0], &choices_list[3]); +#endif + parser.add_option("-m", "--more") .action("append"); + parser.add_option("--more-milk") .action("append_const") .set_const("milk"); + parser.add_option("--hidden") .help(SUPPRESS_HELP); + + // test for 325cb47 + parser.add_option("--option1") .action("store") .type("int") .set_default(1); + parser.add_option("--option2") .action("store") .type("int") .set_default("1"); + parser.set_defaults("option1", "640"); + parser.set_defaults("option2", 640); // now works + + MyCallback mc; + parser.add_option("-K", "--callback") .action("callback") .callback(mc) .help("callback test"); + parser.add_option("--string-callback") .action("callback") .callback(mc) .type("string") .help("callback test"); + + OptionGroup group1 = OptionGroup(parser, "Dangerous Options", + "Caution: use these options at your own risk. " + "It is believed that some of them\nbite."); + group1.add_option("-g") .action("store_true") .help("Group option.") .set_default("0"); + parser.add_option_group(group1); + + OptionGroup group2 = OptionGroup(parser, "Size Options", "Image Size Options."); + group2.add_option("-w", "--width") .action("store") .type("int") .set_default(640) .help("default: %default"); + group2.add_option("--height") .action("store") .type("int") .help("default: %default"); + parser.set_defaults("height", 480); + parser.add_option_group(group2); + + try { + Values& options = parser.parse_args(argc, argv); + vector<string> args = parser.args(); + + cout << "clear: " << (options.get("no_clear") ? "false" : "true") << endl; + cout << "string: " << options["string"] << endl; + cout << "clause: " << options["clause"] << endl; + cout << "k: " << options["k"] << endl; + cout << "verbosity: " << options["verbosity"] << endl; + cout << "number: " << (int) options.get("number") << endl; + cout << "int: " << (int) options.get("int") << endl; + cout << "float: " << (float) options.get("float") << endl; + complex<double> c = 0; + if (options.is_set("complex")) { + stringstream ss; + ss << options["complex"]; + ss >> c; + } + cout << "complex: " << c << endl; + cout << "choices: " << (const char*) options.get("choices") << endl; + cout << "choices-list: " << (const char*) options.get("choices_list") << endl; + { + stringstream ss; + for_each(options.all("more").begin(), options.all("more").end(), Output(ss, ", ")); + cout << "more: " << ss.str() << endl; + } + cout << "more_milk:" << endl; + for (Values::iterator it = options.all("more_milk").begin(); it != options.all("more_milk").end(); ++it) + cout << "- " << *it << endl; + cout << "hidden: " << options["hidden"] << endl; + cout << "group: " << (options.get("g") ? "true" : "false") << endl; + + cout << "option1: " << (int) options.get("option1") << std::endl; + cout << "option2: " << (int) options.get("option2") << std::endl; + + cout << "width: " << (int) options.get("width") << std::endl; + cout << "height: " << (int) options.get("height") << std::endl; + + cout << endl << "leftover arguments: " << endl; + for (vector<string>::const_iterator it = args.begin(); it != args.end(); ++it) { + cout << "arg: " << *it << endl; + } + } + catch(int ex) { + return ex; + } + + return 0; +} |