From 77c7fa836f0f77049835f649d1f9a9ff8bc11cc9 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 17 Jan 2016 05:09:58 -0600 Subject: Add the cpp-optparse project to Externals. From https://github.com/weisslj/cpp-argparse --- Externals/cpp-optparse/OptionParser.h | 278 ++++++++++++++++++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 Externals/cpp-optparse/OptionParser.h (limited to 'Externals/cpp-optparse/OptionParser.h') diff --git a/Externals/cpp-optparse/OptionParser.h b/Externals/cpp-optparse/OptionParser.h new file mode 100644 index 0000000000..aab832c00c --- /dev/null +++ b/Externals/cpp-optparse/OptionParser.h @@ -0,0 +1,278 @@ +/** + * Copyright (C) 2010 Johannes Weißl + * License: MIT License + * URL: https://github.com/weisslj/cpp-optparse + */ + +#ifndef OPTIONPARSER_H_ +#define OPTIONPARSER_H_ + +#include +#include +#include +#include +#include +#include +#include +#include + +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 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 + 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 + Option& choices(InputIterator begin, InputIterator end) { + _choices.assign(begin, end); type("choice"); return *this; + } + Option& choices(std::initializer_list ilist) { + _choices.assign(ilist); type("choice"); return *this; + } + 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& 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 _short_opts; + std::set _long_opts; + + std::string _action; + std::string _type; + std::string _dest; + std::string _default; + size_t _nargs; + std::string _const; + std::list _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& opt); + + std::string format_option_help(unsigned int indent = 2) const; + + protected: + std::string _description; + + std::list