diff options
author | Jarryd Beck <jarro.2783@gmail.com> | 2016-08-29 11:37:15 +0300 |
---|---|---|
committer | Jarryd Beck <jarro.2783@gmail.com> | 2016-08-29 11:37:15 +0300 |
commit | c45bcc385870b8707ef5475cdd7c14b1ce7fc4a1 (patch) | |
tree | 274464e3201148a569f159e435f6bdc316277192 /src | |
parent | 9e3f3115d2249dc2689b39bdc51a412a23c04204 (diff) |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/cxxopts.hpp | 26 |
1 files 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 <regex> #include <sstream> #include <string> +#include <unordered_set> #include <vector> //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<std::string, std::shared_ptr<OptionDetails>> m_options; std::vector<std::string> m_positional; std::vector<std::string>::iterator m_next_positional; + std::unordered_set<std::string> m_positional_set; //mapping from groups to help options std::map<std::string, HelpGroupDetails> m_help; @@ -1073,6 +1076,8 @@ Options::parse_positional(std::vector<std::string> 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<std::string>& 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) { |