From 32197f600c6651c304cb1574ecdd6f96fb6c3b5c Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 25 May 2017 19:01:52 +0200 Subject: [PATCH] remove iter() in groups --- ostd/argparse.hh | 82 ++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/ostd/argparse.hh b/ostd/argparse.hh index d0c9ffb..c763a6f 100644 --- a/ostd/argparse.hh +++ b/ostd/argparse.hh @@ -319,10 +319,6 @@ struct arg_mutually_exclusive_group: arg_description { return *p_opts.emplace_back(o); } - auto iter() const { - return ostd::citer(p_opts); - } - template bool for_each(F &&func, bool iter_ex) const { return for_each_impl(func, iter_ex); @@ -395,10 +391,6 @@ struct arg_group: arg_description { return *p_opts.emplace_back(o); } - auto iter() const { - return ostd::citer(p_opts); - } - template bool for_each(F &&func, bool iter_ex) const { return for_each_impl(func, iter_ex); @@ -628,9 +620,11 @@ public: return true; } std::vector names; - for (auto &mp: mgrp.iter()) { + bool cont = false; + mgrp.for_each([&names, &cont](auto const &marg) { string_range cn; - for (auto &n: mp->get_names()) { + auto const &mopt = static_cast(marg); + for (auto &n: mopt.get_names()) { if (n.size() > cn.size()) { cn = n; } @@ -638,14 +632,18 @@ public: if (!cn.empty()) { names.push_back(cn); } - if (mp->used()) { - return true; + if (mopt.used()) { + cont = true; + return false; } + return true; + }, true); + if (!cont) { + throw arg_error{format( + appender(), + "one of the arguments %('%s'%|, %) is required", names + ).get()}; } - throw arg_error{format( - appender(), - "one of the arguments %('%s'%|, %) is required", names - ).get()}; return true; } if (arg.type() != arg_type::POSITIONAL) { @@ -867,8 +865,8 @@ struct default_help_formatter { void format_options(OutputRange &out) { std::size_t opt_namel = 0, pos_namel = 0, grp_namel = 0; - std::vector allopt; - std::vector allpos; + std::vector allopt; + std::vector allpos; for (auto &p: p_parser.iter()) { auto cs = counting_sink(noop_sink()); @@ -888,21 +886,27 @@ struct default_help_formatter { break; } case arg_type::GROUP: - for (auto &sp: static_cast(*p).iter()) { - auto ccs = cs; - format_option(ccs, *sp); - grp_namel = std::max(grp_namel, ccs.get_written()); - } + static_cast(*p).for_each( + [&cs, &grp_namel, this](auto const &arg) { + auto ccs = cs; + this->format_option(ccs, arg); + grp_namel = std::max(grp_namel, ccs.get_written()); + return true; + }, true + ); break; case arg_type::MUTUALLY_EXCLUSIVE_GROUP: - for (auto &sp: static_cast< - arg_mutually_exclusive_group & - >(*p).iter()) { - auto ccs = cs; - format_option(ccs, *sp); - opt_namel = std::max(opt_namel, ccs.get_written()); - allopt.push_back(sp.get()); - } + static_cast(*p).for_each( + [&cs, &opt_namel, &allopt, this](auto const &arg) { + auto ccs = cs; + this->format_option(ccs, arg); + opt_namel = std::max(opt_namel, ccs.get_written()); + allopt.push_back(static_cast< + arg_optional const * + >(&arg)); + return true; + }, true + ); break; default: break; @@ -911,7 +915,7 @@ struct default_help_formatter { std::size_t maxpad = std::max({opt_namel, pos_namel, grp_namel}); auto write_help = [maxpad]( - auto &out, arg_argument &arg, std::size_t len + auto &out, arg_argument const &arg, std::size_t len ) { auto help = arg.get_help(); if (help.empty()) { @@ -955,13 +959,17 @@ struct default_help_formatter { } auto &garg = static_cast(*gp); format(out, "\n%s:\n", garg.get_name()); - for (auto &p: garg.iter()) { + garg.for_each([&write_help, &out, this](auto const &marg) { format(out, " "); auto cr = counting_sink(out); - format_option(cr, *p); + this->format_option(cr, marg); out = std::move(cr.get_range()); - write_help(out, *p, cr.get_written()); - } + write_help( + out, static_cast(marg), + cr.get_written() + ); + return true; + }, true); } } @@ -1026,7 +1034,7 @@ struct default_help_formatter { } template - void format_option(OutputRange &out, arg_argument const &arg) { + void format_option(OutputRange &out, arg_description const &arg) { switch (arg.type()) { case arg_type::OPTIONAL: format_option(out, static_cast(arg));