From 22ab9836d46477a825a83142a1bfa194f352d1b9 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sun, 26 Oct 2014 21:01:18 +0100 Subject: [PATCH] Add basic support for default options --- src/cxxopts.hpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++- src/example.cpp | 7 ++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index f3908c6..194e384 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -43,8 +43,14 @@ namespace cxxopts virtual void parse(const std::string& text) const = 0; + virtual void + parse() const = 0; + virtual bool has_arg() const = 0; + + virtual bool + has_default() const = 0; }; class OptionException : public std::exception @@ -237,12 +243,24 @@ namespace cxxopts parse_value(text, *m_store); } + void + parse() const + { + parse_value("", *m_store); + } + bool has_arg() const { return value_has_arg::value; } + bool + has_default() const + { + return false; + } + const T& get() const { @@ -256,11 +274,50 @@ namespace cxxopts } } - private: + protected: std::shared_ptr m_result; T* m_store; }; + template + class default_value_default : public default_value + { + using base_type = default_value; + + public: + default_value_default(T value) + : base_type(), m_default(value) + { + } + + void + parse(const std::string& text) const + { + parse_value(text, *base_type::m_store); + } + + void + parse() const + { + *base_type::m_store = m_default; + } + + bool + has_arg() const + { + return value_has_arg::value; + } + + bool + has_default() const + { + return true; + } + + private: + T m_default; + }; + } template @@ -277,6 +334,13 @@ namespace cxxopts return std::make_shared>(&t); } + template + std::shared_ptr + value_default(const T& t) + { + return std::make_shared>(t); + } + class OptionAdder; class OptionDetails @@ -312,12 +376,23 @@ namespace cxxopts ++m_count; } + void + parse_default() + { + m_value->parse(); + ++m_count; + } + int count() const { return m_count; } + const Value& value() const { + return *m_value; + } + template const T& as() const @@ -799,6 +874,16 @@ Options::parse(int& argc, char**& argv) ++current; } + for (auto& opt : m_options) + { + auto& detail = opt.second; + auto& value = detail->value(); + + if(!detail->count() && value.has_default()){ + detail->parse_default(); + } + } + argc = nextKeep; } diff --git a/src/example.cpp b/src/example.cpp index 989d40c..59e9983 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -38,6 +38,7 @@ int main(int argc, char* argv[]) ("a,apple", "an apple", cxxopts::value(apple)) ("b,bob", "Bob") ("f,file", "File", cxxopts::value>()) + ("o,output", "Output file", cxxopts::value_default("a.out")) ("positional", "Positional arguments: these are the arguments that are entered " "without an option", cxxopts::value()) @@ -88,6 +89,12 @@ int main(int argc, char* argv[]) << std::endl; } + if (options.count("output")) + { + std::cout << "Output = " << options["output"].as() + << std::endl; + } + if (options.count("int")) { std::cout << "int = " << options["int"].as() << std::endl;