From 061d988ef218e05787e2b82e5a252614e3f001eb Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 16 May 2017 00:06:16 +0200 Subject: [PATCH] expand argparse storage options, add defaults, add outside storage --- ostd/argparse.hh | 71 +++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/ostd/argparse.hh b/ostd/argparse.hh index 99e727b..6c4198f 100644 --- a/ostd/argparse.hh +++ b/ostd/argparse.hh @@ -112,16 +112,45 @@ struct arg_optional: arg_argument { return p_valreq; } + template + arg_optional &value(T &&val) { + p_value = std::forward(val); + } + template arg_optional &action(F func) { - p_action = [func = std::move(func)]( + p_action = [this, func = std::move(func)]( iterator_range vals - ) mutable -> std::any { - return func(vals); + ) mutable { + p_value = func(vals); }; return *this; } + template + arg_optional &action(F func, T &vref) { + p_action = [this, &vref, func = std::move(func)]( + iterator_range vals + ) mutable { + vref = func(vals); + p_value = true; + }; + return *this; + } + + template + arg_optional &action(std::pair p) { + p_value = std::move(p.second); + return action(std::move(p.first)); + } + + template + arg_optional &action(std::pair p, U &vref) { + vref = std::move(p.second); + p_value = false; + return action(std::move(p.first), vref); + } + arg_optional &help(string_range str) { arg_argument::help(str); return *this; @@ -141,7 +170,7 @@ protected: void set_values(iterator_range vals) { if (p_action) { - p_value = p_action(vals); + p_action(vals); } else { switch (vals.size()) { case 0: @@ -164,7 +193,7 @@ protected: private: std::any p_value; - std::function)> p_action; + std::function)> p_action; std::string p_lname; char p_sname; }; @@ -544,20 +573,12 @@ private: template auto arg_print_help(OutputRange o, arg_parser &p) { - struct cb { - cb(OutputRange orange, arg_parser &par): - p(par), out(orange) - {} - - bool operator()(iterator_range) { - p.print_help(out); - return true; - } - private: - arg_parser &p; - OutputRange out; + return [o = std::move(o), &p](iterator_range) + mutable + { + p.print_help(o); + return true; }; - return cb{o, p}; }; auto arg_print_help(arg_parser &p) { @@ -566,23 +587,17 @@ auto arg_print_help(arg_parser &p) { template auto arg_store_const(T &&val) { - struct cb { - cb(T &&cval): value(std::forward(cval)) {} - std::decay_t operator()(iterator_range) { - return std::move(value); - } - private: - std::decay_t value; + return [val](iterator_range) mutable { + return std::move(val); }; - return cb{std::forward(val)}; } auto arg_store_true() { - return arg_store_const(true); + return std::make_pair(arg_store_const(true), false); } auto arg_store_false() { - return arg_store_const(false); + return std::make_pair(arg_store_const(false), true); } /** @} */