Commit a323d7b444f095d5269600e495c25aefed3f1d51
Committed by
Henry Schreiner
1 parent
1994efd6
Refactor group formatting a little
Showing
2 changed files
with
23 additions
and
25 deletions
include/CLI/Formatter.hpp
| @@ -10,27 +10,26 @@ | @@ -10,27 +10,26 @@ | ||
| 10 | 10 | ||
| 11 | namespace CLI { | 11 | namespace CLI { |
| 12 | 12 | ||
| 13 | -inline std::string Formatter::make_group(const App *app, | ||
| 14 | - std::string group, | ||
| 15 | - bool is_positional, | ||
| 16 | - std::function<bool(const Option *)> filter) const { | 13 | +inline std::string |
| 14 | +Formatter::make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const { | ||
| 17 | std::stringstream out; | 15 | std::stringstream out; |
| 18 | - std::vector<const Option *> opts = app->get_options(filter); | ||
| 19 | 16 | ||
| 20 | - if(!opts.empty()) { | ||
| 21 | - out << "\n" << group << ":\n"; | ||
| 22 | - for(const Option *opt : opts) { | ||
| 23 | - out << make_option(opt, is_positional); | ||
| 24 | - } | 17 | + out << "\n" << group << ":\n"; |
| 18 | + for(const Option *opt : opts) { | ||
| 19 | + out << make_option(opt, is_positional); | ||
| 25 | } | 20 | } |
| 26 | 21 | ||
| 27 | return out.str(); | 22 | return out.str(); |
| 28 | } | 23 | } |
| 29 | 24 | ||
| 30 | inline std::string Formatter::make_positionals(const App *app) const { | 25 | inline std::string Formatter::make_positionals(const App *app) const { |
| 31 | - return make_group(app, get_label("Positionals"), true, [](const Option *opt) { | ||
| 32 | - return !opt->get_group().empty() && opt->get_positional(); | ||
| 33 | - }); | 26 | + std::vector<const Option *> opts = |
| 27 | + app->get_options([](const Option *opt) { return !opt->get_group().empty() && opt->get_positional(); }); | ||
| 28 | + | ||
| 29 | + if(opts.empty()) | ||
| 30 | + return std::string(); | ||
| 31 | + else | ||
| 32 | + return make_group(get_label("Positionals"), true, opts); | ||
| 34 | } | 33 | } |
| 35 | 34 | ||
| 36 | inline std::string Formatter::make_groups(const App *app, AppFormatMode mode) const { | 35 | 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 | @@ -39,14 +38,15 @@ inline std::string Formatter::make_groups(const App *app, AppFormatMode mode) co | ||
| 39 | 38 | ||
| 40 | // Options | 39 | // Options |
| 41 | for(const std::string &group : groups) { | 40 | for(const std::string &group : groups) { |
| 42 | - if(!group.empty()) { | ||
| 43 | - out << make_group(app, group, false, [app, mode, &group](const Option *opt) { | ||
| 44 | - return opt->get_group() == group // Must be in the right group | ||
| 45 | - && opt->nonpositional() // Must not be a positional | ||
| 46 | - && (mode != AppFormatMode::Sub // If mode is Sub, then | ||
| 47 | - || (app->get_help_ptr() != opt // Ignore help pointer | ||
| 48 | - && app->get_help_all_ptr() != opt)); // Ignore help all pointer | ||
| 49 | - }); | 41 | + std::vector<const Option *> opts = app->get_options([app, mode, &group](const Option *opt) { |
| 42 | + return opt->get_group() == group // Must be in the right group | ||
| 43 | + && opt->nonpositional() // Must not be a positional | ||
| 44 | + && (mode != AppFormatMode::Sub // If mode is Sub, then | ||
| 45 | + || (app->get_help_ptr() != opt // Ignore help pointer | ||
| 46 | + && app->get_help_all_ptr() != opt)); // Ignore help all pointer | ||
| 47 | + }); | ||
| 48 | + if(!group.empty() && !opts.empty()) { | ||
| 49 | + out << make_group(group, false, opts); | ||
| 50 | 50 | ||
| 51 | if(group != groups.back()) | 51 | if(group != groups.back()) |
| 52 | out << "\n"; | 52 | out << "\n"; |
include/CLI/FormatterFwd.hpp
| @@ -106,11 +106,9 @@ class Formatter : public FormatterBase { | @@ -106,11 +106,9 @@ class Formatter : public FormatterBase { | ||
| 106 | /// @name Overridables | 106 | /// @name Overridables |
| 107 | ///@{ | 107 | ///@{ |
| 108 | 108 | ||
| 109 | - /// This prints out a group of options | 109 | + /// This prints out a group of options with title |
| 110 | /// | 110 | /// |
| 111 | - /// Use the filter to pick out the items you want in your group | ||
| 112 | - virtual std::string | ||
| 113 | - make_group(const App *app, std::string group, bool is_positional, std::function<bool(const Option *)> filter) const; | 111 | + virtual std::string make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const; |
| 114 | 112 | ||
| 115 | /// This prints out just the positionals "group" | 113 | /// This prints out just the positionals "group" |
| 116 | virtual std::string make_positionals(const App *app) const; | 114 | virtual std::string make_positionals(const App *app) const; |