diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index cc0c6f3..633a419 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -802,6 +802,14 @@ namespace cxxopts String help_one_group(const std::string& group) const; + inline + void + generate_group_help(String& result, const std::vector& groups) const; + + inline + void + generate_all_groups_help(String& result) const; + std::string m_program; String m_help_string; std::string m_positional_help; @@ -1372,6 +1380,35 @@ Options::help_one_group(const std::string& g) const return result; } +void +Options::generate_group_help(String& result, const std::vector& groups) const +{ + for (std::size_t i = 0; i < groups.size(); ++i) + { + String const& group_help = help_one_group(groups[i]); + if (empty(group_help)) continue; + result += group_help; + if (i < groups.size() - 1) + { + result += '\n'; + } + } +} + +void +Options::generate_all_groups_help(String& result) const +{ + std::vector groups; + groups.reserve(m_help.size()); + + for (auto& group : m_help) + { + groups.push_back(group.first); + } + + generate_group_help(result, groups); +} + std::string Options::help(const std::vector& groups) const { @@ -1384,15 +1421,13 @@ Options::help(const std::vector& groups) const result += "\n\n"; - for (std::size_t i = 0; i < groups.size(); ++i) + if (groups.size() == 0) { - String const& group_help = help_one_group(groups[i]); - if (empty(group_help)) continue; - result += group_help; - if (i < groups.size() - 1) - { - result += '\n'; - } + generate_all_groups_help(result); + } + else + { + generate_group_help(result, groups); } return toUTF8String(result);