Commit 24450d183a4f72675460293e5c87b812ad73e047

Authored by Jarryd Beck
1 parent 4ebfa259

Allow positional arguments to show in help

Fixes #72. This adds an option for positional arguments to be
shown in the help output.
include/cxxopts.hpp
@@ -1118,6 +1118,7 @@ namespace cxxopts @@ -1118,6 +1118,7 @@ namespace cxxopts
1118 : m_program(std::move(program)) 1118 : m_program(std::move(program))
1119 , m_help_string(toLocalString(std::move(help_string))) 1119 , m_help_string(toLocalString(std::move(help_string)))
1120 , m_positional_help("positional parameters") 1120 , m_positional_help("positional parameters")
  1121 + , m_show_positional(false)
1121 , m_next_positional(m_positional.end()) 1122 , m_next_positional(m_positional.end())
1122 { 1123 {
1123 } 1124 }
@@ -1129,6 +1130,13 @@ namespace cxxopts @@ -1129,6 +1130,13 @@ namespace cxxopts
1129 return *this; 1130 return *this;
1130 } 1131 }
1131 1132
  1133 + Options&
  1134 + show_positional_help()
  1135 + {
  1136 + m_show_positional = true;
  1137 + return *this;
  1138 + }
  1139 +
1132 ParseResult 1140 ParseResult
1133 parse(int& argc, char**& argv); 1141 parse(int& argc, char**& argv);
1134 1142
@@ -1187,6 +1195,7 @@ namespace cxxopts @@ -1187,6 +1195,7 @@ namespace cxxopts
1187 std::string m_program; 1195 std::string m_program;
1188 String m_help_string; 1196 String m_help_string;
1189 std::string m_positional_help; 1197 std::string m_positional_help;
  1198 + bool m_show_positional;
1190 1199
1191 std::unordered_map<std::string, std::shared_ptr<OptionDetails>> m_options; 1200 std::unordered_map<std::string, std::shared_ptr<OptionDetails>> m_options;
1192 std::vector<std::string> m_positional; 1201 std::vector<std::string> m_positional;
@@ -1788,7 +1797,9 @@ Options::help_one_group(const std::string&amp; g) const @@ -1788,7 +1797,9 @@ Options::help_one_group(const std::string&amp; g) const
1788 1797
1789 for (const auto& o : group->second.options) 1798 for (const auto& o : group->second.options)
1790 { 1799 {
1791 - if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end()) 1800 + if (o.is_container &&
  1801 + m_positional_set.find(o.l) != m_positional_set.end() &&
  1802 + !m_show_positional)
1792 { 1803 {
1793 continue; 1804 continue;
1794 } 1805 }
@@ -1806,7 +1817,9 @@ Options::help_one_group(const std::string&amp; g) const @@ -1806,7 +1817,9 @@ Options::help_one_group(const std::string&amp; g) const
1806 auto fiter = format.begin(); 1817 auto fiter = format.begin();
1807 for (const auto& o : group->second.options) 1818 for (const auto& o : group->second.options)
1808 { 1819 {
1809 - if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end()) 1820 + if (o.is_container &&
  1821 + m_positional_set.find(o.l) != m_positional_set.end() &&
  1822 + !m_show_positional)
1810 { 1823 {
1811 continue; 1824 continue;
1812 } 1825 }
src/example.cpp
@@ -31,7 +31,9 @@ int main(int argc, char* argv[]) @@ -31,7 +31,9 @@ int main(int argc, char* argv[])
31 try 31 try
32 { 32 {
33 cxxopts::Options options(argv[0], " - example command line options"); 33 cxxopts::Options options(argv[0], " - example command line options");
34 - options.positional_help("[optional args]"); 34 + options
  35 + .positional_help("[optional args]")
  36 + .show_positional_help();
35 37
36 bool apple = false; 38 bool apple = false;
37 39