diff options
Diffstat (limited to 'src/common/command_line.h')
-rw-r--r-- | src/common/command_line.h | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/src/common/command_line.h b/src/common/command_line.h index 7b183d86b..f67efcf86 100644 --- a/src/common/command_line.h +++ b/src/common/command_line.h @@ -30,7 +30,9 @@ #pragma once +#include <functional> #include <iostream> +#include <sstream> #include <type_traits> #include <boost/program_options/parsers.hpp> @@ -46,7 +48,7 @@ namespace command_line //! \return True if `str` is `is_iequal("n" || "no" || `tr("no"))`. bool is_no(const std::string& str); - template<typename T, bool required = false> + template<typename T, bool required = false, bool dependent = false> struct arg_descriptor; template<typename T> @@ -81,6 +83,22 @@ namespace command_line }; template<typename T> + struct arg_descriptor<T, false, true> + { + typedef T value_type; + + const char* name; + const char* description; + + T default_value; + + const arg_descriptor<bool, false>& ref; + std::function<T(bool, bool, T)> depf; + + bool not_use_default; + }; + + template<typename T> boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, true>& /*arg*/) { return boost::program_options::value<T>()->required(); @@ -96,6 +114,20 @@ namespace command_line } template<typename T> + boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false, true>& arg) + { + auto semantic = boost::program_options::value<T>(); + if (!arg.not_use_default) { + std::ostringstream format; + format << arg.depf(false, true, arg.default_value) << ", " + << arg.depf(true, true, arg.default_value) << " if '" + << arg.ref.name << "'"; + semantic->default_value(arg.depf(arg.ref.default_value, true, arg.default_value), format.str()); + } + return semantic; + } + + template<typename T> boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg, const T& def) { auto semantic = boost::program_options::value<T>(); @@ -112,8 +144,8 @@ namespace command_line return semantic; } - template<typename T, bool required> - void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, required>& arg, bool unique = true) + template<typename T, bool required, bool dependent> + void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, required, dependent>& arg, bool unique = true) { if (0 != description.find_nothrow(arg.name, false)) { @@ -189,12 +221,17 @@ namespace command_line return !value.empty(); } - template<typename T, bool required> - bool is_arg_defaulted(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg) + template<typename T, bool required, bool dependent> + bool is_arg_defaulted(const boost::program_options::variables_map& vm, const arg_descriptor<T, required, dependent>& arg) { return vm[arg.name].defaulted(); } + template<typename T, bool required> + T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required, true>& arg) + { + return arg.depf(get_arg(vm, arg.ref), is_arg_defaulted(vm, arg), vm[arg.name].template as<T>()); + } template<typename T, bool required> T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg) |