From d094fdfab01834b43a668456348bb05b8bbdce90 Mon Sep 17 00:00:00 2001 From: RonxBulld <526677628@qq.com> Date: Mon, 26 Apr 2021 15:39:49 +0800 Subject: [PATCH] Embed verification logic in applicator::apply. --- proposals-demo/objectification/main.cpp | 45 +++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/proposals-demo/objectification/main.cpp b/proposals-demo/objectification/main.cpp index c317e65..82fcebb 100644 --- a/proposals-demo/objectification/main.cpp +++ b/proposals-demo/objectification/main.cpp @@ -31,6 +31,7 @@ void ApplyAction(Obj &O, const Action & action, const Actions & ... actions) } //=========================================================================== +// Option struct Opt : public cxxopts::Option { @@ -49,6 +50,9 @@ struct Opt : public cxxopts::Option }; +//=========================================================================== +// Parser + struct Parser : public cxxopts::Options { template @@ -66,22 +70,43 @@ struct Parser : public cxxopts::Options } }; + +//=========================================================================== +// Exception + +class option_multiset_error : public cxxopts::OptionSpecException +{ +public: + explicit option_multiset_error(const std::string& option) + : OptionSpecException("Option " + + cxxopts::LQUOTE + option + cxxopts::RQUOTE + + " is set multiple times.") + { + } +}; + } namespace yaco /// Applicators - the instance properties implicit modifier. { - template struct applicator - { - static void apply(const char *str, Parser &Os) - { - Os.program_name(std::string(str)); - } - static void apply(const char *str, Opt &O) +template struct applicator +{ + static void apply(const char *str, Parser &Os) + { + Os.program_name(std::string(str)); + } + + static void apply(const char *str, Opt &O) + { + if (!O.opts_.empty()) { - O.opts_ = std::string(str); + cxxopts::throw_or_mimic + (std::string(str) + " (previous: " + O.opts_ + ")"); } - }; + O.opts_ = std::string(str); + } +}; } /// yaco namespace yaco /// The instance properties explicit modifier. @@ -117,7 +142,7 @@ namespace yaco /// The instance properties explicit modifier. yaco::Parser options("objectification-demo"); yaco::Opt help("h,help", yaco::desc("display help messages."), yaco::inject(options)); -yaco::Opt job("j,job", "jobs", yaco::type(), yaco::inject(options)); +yaco::Opt job("j", yaco::desc("jobs"), yaco::type(), yaco::inject(options)); int main(int argc, char **argv) { auto result = options.parse(argc, argv);