1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-01 13:13:53 +00:00

Refactor group formatting a little

This commit is contained in:
Henry Fredrick Schreiner 2018-05-22 00:06:27 +02:00 committed by Henry Schreiner
parent 1994efd601
commit a323d7b444
2 changed files with 23 additions and 25 deletions

View File

@ -10,27 +10,26 @@
namespace CLI {
inline std::string Formatter::make_group(const App *app,
std::string group,
bool is_positional,
std::function<bool(const Option *)> filter) const {
inline std::string
Formatter::make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const {
std::stringstream out;
std::vector<const Option *> 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<const Option *> 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<const Option *> 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";

View File

@ -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<bool(const Option *)> filter) const;
virtual std::string make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
/// This prints out just the positionals "group"
virtual std::string make_positionals(const App *app) const;