From e8c195b683dbc5b4d0f99fef55ac3379d5b58e4d Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 23 Mar 2017 09:59:02 -0400 Subject: [PATCH] Making it easier to add custom options --- include/CLI/App.hpp | 30 +++++++++++++++--------------- include/CLI/Option.hpp | 27 ++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index b0028d1d..6adcdee1 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -237,11 +237,11 @@ public: }; Option* opt = add_option(name, fun, description, defaulted); - opt->typeval_ = detail::type_name(); + opt->set_custom_option(detail::type_name()); if(defaulted) { std::stringstream out; out << variable; - opt->defaultval_ = out.str(); + opt->set_default_val(out.str()); } return opt; } @@ -266,11 +266,9 @@ public: }; Option* opt = add_option(name, fun, description, defaulted); - opt->allow_vector_ = true; - opt->expected_ = -1; - opt->typeval_ = detail::type_name(); + opt->set_custom_option(detail::type_name(), -1, true); if(defaulted) - opt->defaultval_ = "[" + detail::join(variable) + "]"; + opt->set_default_val("[" + detail::join(variable) + "]"); return opt; } @@ -286,7 +284,7 @@ public: Option* opt = add_option(name, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); - opt->expected_ = 0; + opt->set_custom_option("", 0); return opt; } @@ -308,7 +306,7 @@ public: Option* opt = add_option(name, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); - opt->expected_ = 0; + opt->set_custom_option("", 0); return opt; } @@ -330,7 +328,7 @@ public: Option* opt = add_option(name, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); - opt->expected_ = 0; + opt->set_custom_option("", 0); return opt; } @@ -356,12 +354,13 @@ public: }; Option* opt = add_option(name, fun, description, defaulted); - opt->typeval_ = detail::type_name(); - opt->typeval_ += " in {" + detail::join(options) + "}"; + std::string typeval = detail::type_name(); + typeval += " in {" + detail::join(options) + "}"; + opt->set_custom_option(typeval); if(defaulted) { std::stringstream out; out << member; - opt->defaultval_ = out.str(); + opt->set_default_val(out.str()); } return opt; } @@ -391,10 +390,11 @@ public: }; Option* opt = add_option(name, fun, description, defaulted); - opt->typeval_ = detail::type_name(); - opt->typeval_ += " in {" + detail::join(options) + "}"; + std::string typeval = detail::type_name(); + typeval += " in {" + detail::join(options) + "}"; + opt->set_custom_option(typeval); if(defaulted) { - opt->defaultval_ = member; + opt->set_default_val(member); } return opt; } diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 6cf47ab3..7c77081f 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -73,8 +73,8 @@ protected: /// The number of expected values, 0 for flag, -1 for unlimited vector int expected_ {1}; - /// A private setting to allow non-vector args to not be able to accept incorrect expected values - bool allow_vector_ {false}; + /// A private setting to allow args to not be able to accept incorrect expected values + bool changeable_ {false}; /// Ignore the case when matching (option, not value) bool ignore_case_ {false}; @@ -155,12 +155,12 @@ public: /// Set the number of expected arguments (Flags bypass this) Option* expected(int value) { - if(value == 0) + if(!changeable_) + throw IncorrectConstruction("You can only change the expected arguments for vectors"); + else if(value == 0) throw IncorrectConstruction("Cannot set 0 expected, use a flag instead"); else if(expected_ == 0) throw IncorrectConstruction("Cannot make a flag take arguments!"); - else if(!allow_vector_ && value != 1) - throw IncorrectConstruction("You can only change the Expected arguments for vectors"); expected_ = value; return this; } @@ -460,6 +460,23 @@ public: } ///@} + /// @name Custom options + ///@{ + + /// Set a custom option, typestring, expected, and changeable + void set_custom_option(std::string typeval, int expected=1, bool changeable = false) { + typeval_ = typeval; + expected_ = expected; + changeable_ = changeable; + } + + /// Set the default value string representation + void set_default_val(std::string val) { + defaultval_ = val; + } + + ///@} + protected: