From 24450d183a4f72675460293e5c87b812ad73e047 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Mon, 13 Nov 2017 18:47:12 +1100 Subject: [PATCH] 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 | 17 +++++++++++++++-- src/example.cpp | 4 +++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 81fb872..0ac630e 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -1118,6 +1118,7 @@ namespace cxxopts : m_program(std::move(program)) , m_help_string(toLocalString(std::move(help_string))) , m_positional_help("positional parameters") + , m_show_positional(false) , m_next_positional(m_positional.end()) { } @@ -1129,6 +1130,13 @@ namespace cxxopts return *this; } + Options& + show_positional_help() + { + m_show_positional = true; + return *this; + } + ParseResult parse(int& argc, char**& argv); @@ -1187,6 +1195,7 @@ namespace cxxopts std::string m_program; String m_help_string; std::string m_positional_help; + bool m_show_positional; std::unordered_map> m_options; std::vector m_positional; @@ -1788,7 +1797,9 @@ 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()) + if (o.is_container && + m_positional_set.find(o.l) != m_positional_set.end() && + !m_show_positional) { continue; } @@ -1806,7 +1817,9 @@ 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()) + if (o.is_container && + m_positional_set.find(o.l) != m_positional_set.end() && + !m_show_positional) { continue; } diff --git a/src/example.cpp b/src/example.cpp index 5678dd7..b541774 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -31,7 +31,9 @@ int main(int argc, char* argv[]) try { cxxopts::Options options(argv[0], " - example command line options"); - options.positional_help("[optional args]"); + options + .positional_help("[optional args]") + .show_positional_help(); bool apple = false;