From c33cb60eb6e89b6af30fa9d576457ccefff74ad6 Mon Sep 17 00:00:00 2001 From: whythat Date: Sun, 21 Jan 2018 17:28:32 +0200 Subject: [PATCH] common: implement dependent option descriptor --- src/common/command_line.h | 50 +++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/common/command_line.h b/src/common/command_line.h index 7b183d86b..ddbb136e9 100644 --- a/src/common/command_line.h +++ b/src/common/command_line.h @@ -46,7 +46,7 @@ namespace command_line //! \return True if `str` is `is_iequal("n" || "no" || `tr("no"))`. bool is_no(const std::string& str); - template + template struct arg_descriptor; template @@ -80,6 +80,22 @@ namespace command_line const char* description; }; + template + struct arg_descriptor + { + typedef T value_type; + + const char* name; + const char* description; + + const arg_descriptor& ref; + + T true_default_value; + T false_default_value; + + bool not_use_default; + }; + template boost::program_options::typed_value* make_semantic(const arg_descriptor& /*arg*/) { @@ -95,6 +111,23 @@ namespace command_line return semantic; } + template + boost::program_options::typed_value* make_semantic(const arg_descriptor& arg) + { + auto semantic = boost::program_options::value(); + if (!arg.not_use_default) { + if (arg.ref.default_value) + { + semantic->default_value(arg.true_default_value); + } + else + { + semantic->default_value(arg.false_default_value); + } + } + return semantic; + } + template boost::program_options::typed_value* make_semantic(const arg_descriptor& arg, const T& def) { @@ -112,8 +145,8 @@ namespace command_line return semantic; } - template - void add_arg(boost::program_options::options_description& description, const arg_descriptor& arg, bool unique = true) + template + void add_arg(boost::program_options::options_description& description, const arg_descriptor& arg, bool unique = true) { if (0 != description.find_nothrow(arg.name, false)) { @@ -189,12 +222,19 @@ namespace command_line return !value.empty(); } - template - bool is_arg_defaulted(const boost::program_options::variables_map& vm, const arg_descriptor& arg) + template + bool is_arg_defaulted(const boost::program_options::variables_map& vm, const arg_descriptor& arg) { return vm[arg.name].defaulted(); } + template + T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor& arg) + { + if (is_arg_defaulted(vm, arg) && !is_arg_defaulted(vm, arg.ref)) + return get_arg(vm, arg.ref) ? arg.true_default_value : arg.false_default_value; + return vm[arg.name].template as(); + } template T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor& arg)