Commit 21591dc8e80d4b4ad5c9508758f6b8e8d5cc1996

Authored by Torben Jonas
Committed by jarro2783
1 parent 3c3d2003

Conditionally output all help groups on empty groups vector (#42)

* Conditionally output all help groups on empty groups vector

* Fix formatting
Showing 1 changed file with 43 additions and 8 deletions
include/cxxopts.hpp
@@ -802,6 +802,14 @@ namespace cxxopts @@ -802,6 +802,14 @@ namespace cxxopts
802 String 802 String
803 help_one_group(const std::string& group) const; 803 help_one_group(const std::string& group) const;
804 804
  805 + inline
  806 + void
  807 + generate_group_help(String& result, const std::vector<std::string>& groups) const;
  808 +
  809 + inline
  810 + void
  811 + generate_all_groups_help(String& result) const;
  812 +
805 std::string m_program; 813 std::string m_program;
806 String m_help_string; 814 String m_help_string;
807 std::string m_positional_help; 815 std::string m_positional_help;
@@ -1372,6 +1380,35 @@ Options::help_one_group(const std::string&amp; g) const @@ -1372,6 +1380,35 @@ Options::help_one_group(const std::string&amp; g) const
1372 return result; 1380 return result;
1373 } 1381 }
1374 1382
  1383 +void
  1384 +Options::generate_group_help(String& result, const std::vector<std::string>& groups) const
  1385 +{
  1386 + for (std::size_t i = 0; i < groups.size(); ++i)
  1387 + {
  1388 + String const& group_help = help_one_group(groups[i]);
  1389 + if (empty(group_help)) continue;
  1390 + result += group_help;
  1391 + if (i < groups.size() - 1)
  1392 + {
  1393 + result += '\n';
  1394 + }
  1395 + }
  1396 +}
  1397 +
  1398 +void
  1399 +Options::generate_all_groups_help(String& result) const
  1400 +{
  1401 + std::vector<std::string> groups;
  1402 + groups.reserve(m_help.size());
  1403 +
  1404 + for (auto& group : m_help)
  1405 + {
  1406 + groups.push_back(group.first);
  1407 + }
  1408 +
  1409 + generate_group_help(result, groups);
  1410 +}
  1411 +
1375 std::string 1412 std::string
1376 Options::help(const std::vector<std::string>& groups) const 1413 Options::help(const std::vector<std::string>& groups) const
1377 { 1414 {
@@ -1384,15 +1421,13 @@ Options::help(const std::vector&lt;std::string&gt;&amp; groups) const @@ -1384,15 +1421,13 @@ Options::help(const std::vector&lt;std::string&gt;&amp; groups) const
1384 1421
1385 result += "\n\n"; 1422 result += "\n\n";
1386 1423
1387 - for (std::size_t i = 0; i < groups.size(); ++i) 1424 + if (groups.size() == 0)
1388 { 1425 {
1389 - String const& group_help = help_one_group(groups[i]);  
1390 - if (empty(group_help)) continue;  
1391 - result += group_help;  
1392 - if (i < groups.size() - 1)  
1393 - {  
1394 - result += '\n';  
1395 - } 1426 + generate_all_groups_help(result);
  1427 + }
  1428 + else
  1429 + {
  1430 + generate_group_help(result, groups);
1396 } 1431 }
1397 1432
1398 return toUTF8String(result); 1433 return toUTF8String(result);