From c45bcc385870b8707ef5475cdd7c14b1ce7fc4a1 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Mon, 29 Aug 2016 18:37:15 +1000 Subject: [PATCH] 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. --- src/cxxopts.hpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index f1999d4..d7b0f68 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -38,6 +38,7 @@ THE SOFTWARE. #include #include #include +#include #include //when we ask cxxopts to use Unicode, help strings are processed using ICU, @@ -650,6 +651,7 @@ namespace cxxopts bool has_implicit; std::string implicit_value; std::string arg_help; + bool is_container; }; struct HelpGroupDetails @@ -784,6 +786,7 @@ namespace cxxopts std::map> m_options; std::vector m_positional; std::vector::iterator m_next_positional; + std::unordered_set m_positional_set; //mapping from groups to help options std::map m_help; @@ -1073,6 +1076,8 @@ Options::parse_positional(std::vector options) { m_positional = std::move(options); m_next_positional = m_positional.begin(); + + m_positional_set.insert(m_positional.begin(), m_positional.end()); } void @@ -1255,7 +1260,8 @@ Options::add_option value->has_arg(), value->has_default(), value->get_default_value(), value->has_implicit(), value->get_implicit_value(), - std::move(arg_help)}); + std::move(arg_help), + value->is_container()}); } void @@ -1297,6 +1303,11 @@ Options::help_one_group(const std::string& g) const for (const auto& o : group->second.options) { + if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end()) + { + continue; + } + auto s = format_option(o); longest = std::max(longest, stringLength(s)); format.push_back(std::make_pair(s, String())); @@ -1310,6 +1321,11 @@ Options::help_one_group(const std::string& g) const auto fiter = format.begin(); for (const auto& o : group->second.options) { + if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end()) + { + continue; + } + auto d = format_description(o, longest + OPTION_DESC_GAP, allowed); result += fiter->first; @@ -1337,7 +1353,13 @@ std::string Options::help(const std::vector& groups) const { String result = m_help_string + "\nUsage:\n " + - toLocalString(m_program) + " [OPTION...]\n\n"; + toLocalString(m_program) + " [OPTION...]"; + + if (m_positional.size() > 0) { + result += " positional parameters"; + } + + result += "\n\n"; for (std::size_t i = 0; i < groups.size(); ++i) {