From a323d7b444f095d5269600e495c25aefed3f1d51 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 22 May 2018 00:06:27 +0200 Subject: [PATCH] Refactor group formatting a little --- include/CLI/Formatter.hpp | 42 ++++++++++++++++++------------------ include/CLI/FormatterFwd.hpp | 6 ++---- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/include/CLI/Formatter.hpp b/include/CLI/Formatter.hpp index 1f0110b1..82e641a3 100644 --- a/include/CLI/Formatter.hpp +++ b/include/CLI/Formatter.hpp @@ -10,27 +10,26 @@ namespace CLI { -inline std::string Formatter::make_group(const App *app, - std::string group, - bool is_positional, - std::function filter) const { +inline std::string +Formatter::make_group(std::string group, bool is_positional, std::vector opts) const { std::stringstream out; - std::vector opts = app->get_options(filter); - if(!opts.empty()) { - out << "\n" << group << ":\n"; - for(const Option *opt : opts) { - out << make_option(opt, is_positional); - } + out << "\n" << group << ":\n"; + for(const Option *opt : opts) { + out << make_option(opt, is_positional); } return out.str(); } inline std::string Formatter::make_positionals(const App *app) const { - return make_group(app, get_label("Positionals"), true, [](const Option *opt) { - return !opt->get_group().empty() && opt->get_positional(); - }); + std::vector opts = + app->get_options([](const Option *opt) { return !opt->get_group().empty() && opt->get_positional(); }); + + if(opts.empty()) + return std::string(); + else + return make_group(get_label("Positionals"), true, opts); } inline std::string Formatter::make_groups(const App *app, AppFormatMode mode) const { @@ -39,14 +38,15 @@ inline std::string Formatter::make_groups(const App *app, AppFormatMode mode) co // Options for(const std::string &group : groups) { - if(!group.empty()) { - out << make_group(app, group, false, [app, mode, &group](const Option *opt) { - return opt->get_group() == group // Must be in the right group - && opt->nonpositional() // Must not be a positional - && (mode != AppFormatMode::Sub // If mode is Sub, then - || (app->get_help_ptr() != opt // Ignore help pointer - && app->get_help_all_ptr() != opt)); // Ignore help all pointer - }); + std::vector opts = app->get_options([app, mode, &group](const Option *opt) { + return opt->get_group() == group // Must be in the right group + && opt->nonpositional() // Must not be a positional + && (mode != AppFormatMode::Sub // If mode is Sub, then + || (app->get_help_ptr() != opt // Ignore help pointer + && app->get_help_all_ptr() != opt)); // Ignore help all pointer + }); + if(!group.empty() && !opts.empty()) { + out << make_group(group, false, opts); if(group != groups.back()) out << "\n"; diff --git a/include/CLI/FormatterFwd.hpp b/include/CLI/FormatterFwd.hpp index 5e87dec4..92bfe651 100644 --- a/include/CLI/FormatterFwd.hpp +++ b/include/CLI/FormatterFwd.hpp @@ -106,11 +106,9 @@ class Formatter : public FormatterBase { /// @name Overridables ///@{ - /// This prints out a group of options + /// This prints out a group of options with title /// - /// Use the filter to pick out the items you want in your group - virtual std::string - make_group(const App *app, std::string group, bool is_positional, std::function filter) const; + virtual std::string make_group(std::string group, bool is_positional, std::vector opts) const; /// This prints out just the positionals "group" virtual std::string make_positionals(const App *app) const;