Commit c45bcc385870b8707ef5475cdd7c14b1ce7fc4a1

Authored by Jarryd Beck
1 parent 9e3f3115

Hide positional parameters that are a container.

Closes #26. Don't show positional parameters that are a container,
because they are designed for chewing up any extra parameters.
Showing 1 changed file with 24 additions and 2 deletions
src/cxxopts.hpp
... ... @@ -38,6 +38,7 @@ THE SOFTWARE.
38 38 #include <regex>
39 39 #include <sstream>
40 40 #include <string>
  41 +#include <unordered_set>
41 42 #include <vector>
42 43  
43 44 //when we ask cxxopts to use Unicode, help strings are processed using ICU,
... ... @@ -650,6 +651,7 @@ namespace cxxopts
650 651 bool has_implicit;
651 652 std::string implicit_value;
652 653 std::string arg_help;
  654 + bool is_container;
653 655 };
654 656  
655 657 struct HelpGroupDetails
... ... @@ -784,6 +786,7 @@ namespace cxxopts
784 786 std::map<std::string, std::shared_ptr<OptionDetails>> m_options;
785 787 std::vector<std::string> m_positional;
786 788 std::vector<std::string>::iterator m_next_positional;
  789 + std::unordered_set<std::string> m_positional_set;
787 790  
788 791 //mapping from groups to help options
789 792 std::map<std::string, HelpGroupDetails> m_help;
... ... @@ -1073,6 +1076,8 @@ Options::parse_positional(std::vector&lt;std::string&gt; options)
1073 1076 {
1074 1077 m_positional = std::move(options);
1075 1078 m_next_positional = m_positional.begin();
  1079 +
  1080 + m_positional_set.insert(m_positional.begin(), m_positional.end());
1076 1081 }
1077 1082  
1078 1083 void
... ... @@ -1255,7 +1260,8 @@ Options::add_option
1255 1260 value->has_arg(),
1256 1261 value->has_default(), value->get_default_value(),
1257 1262 value->has_implicit(), value->get_implicit_value(),
1258   - std::move(arg_help)});
  1263 + std::move(arg_help),
  1264 + value->is_container()});
1259 1265 }
1260 1266  
1261 1267 void
... ... @@ -1297,6 +1303,11 @@ Options::help_one_group(const std::string&amp; g) const
1297 1303  
1298 1304 for (const auto& o : group->second.options)
1299 1305 {
  1306 + if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end())
  1307 + {
  1308 + continue;
  1309 + }
  1310 +
1300 1311 auto s = format_option(o);
1301 1312 longest = std::max(longest, stringLength(s));
1302 1313 format.push_back(std::make_pair(s, String()));
... ... @@ -1310,6 +1321,11 @@ Options::help_one_group(const std::string&amp; g) const
1310 1321 auto fiter = format.begin();
1311 1322 for (const auto& o : group->second.options)
1312 1323 {
  1324 + if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end())
  1325 + {
  1326 + continue;
  1327 + }
  1328 +
1313 1329 auto d = format_description(o, longest + OPTION_DESC_GAP, allowed);
1314 1330  
1315 1331 result += fiter->first;
... ... @@ -1337,7 +1353,13 @@ std::string
1337 1353 Options::help(const std::vector<std::string>& groups) const
1338 1354 {
1339 1355 String result = m_help_string + "\nUsage:\n " +
1340   - toLocalString(m_program) + " [OPTION...]\n\n";
  1356 + toLocalString(m_program) + " [OPTION...]";
  1357 +
  1358 + if (m_positional.size() > 0) {
  1359 + result += " positional parameters";
  1360 + }
  1361 +
  1362 + result += "\n\n";
1341 1363  
1342 1364 for (std::size_t i = 0; i < groups.size(); ++i)
1343 1365 {
... ...