From 1d9ae57a92c367ec6e92c982e42f7edb573084f6 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Tue, 14 Oct 2014 16:59:54 +1100 Subject: [PATCH] parse groups --- src/cxxopts.cpp | 32 +++++++++++++++++++++++++------- src/cxxopts.hpp | 16 +++++++++++++--- src/example.cpp | 15 ++++++++++----- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/cxxopts.cpp b/src/cxxopts.cpp index 97ddc56..7f5ac29 100644 --- a/src/cxxopts.cpp +++ b/src/cxxopts.cpp @@ -363,7 +363,7 @@ Options::add_option //add the help details auto& options = m_help[group]; - options.push_back(HelpDetails{s, l, desc, value->has_arg()}); + options.options.push_back(HelpOptionDetails{s, l, desc, value->has_arg()}); } void @@ -382,11 +382,11 @@ Options::add_one_option } std::string -Options::help() const +Options::help_one_group(const std::string& g) const { typedef std::vector> OptionHelp; - auto group = m_help.find(""); + auto group = m_help.find(g); if (group == m_help.end()) { return ""; @@ -396,10 +396,14 @@ Options::help() const size_t longest = 0; - std::string result = "Usage:\n " + m_program + " [OPTION...] " - + m_help_string + "\n\n"; + std::string result; + + if (!g.empty()) + { + result += " " + g + " options:\n\n"; + } - for (const auto& o : group->second) + for (const auto& o : group->second.options) { auto s = format_option(o.s, o.l, o.has_arg); longest = std::max(longest, s.size()); @@ -412,7 +416,7 @@ Options::help() const int allowed = 76 - longest - OPTION_DESC_GAP; auto fiter = format.begin(); - for (const auto& o : group->second) + for (const auto& o : group->second.options) { auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed); @@ -436,4 +440,18 @@ Options::help() const return result; } +std::string +Options::help(const std::vector& groups) const +{ + std::string result = "Usage:\n " + m_program + " [OPTION...] " + + m_help_string + "\n\n"; + + for (const auto& g : groups) + { + result += help_one_group(g); + } + + return result; +} + } diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index ffa687a..bfbe7b7 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -326,7 +326,7 @@ namespace cxxopts int m_count; }; - struct HelpDetails + struct HelpOptionDetails { std::string s; std::string l; @@ -334,6 +334,13 @@ namespace cxxopts bool has_arg; }; + struct HelpGroupDetails + { + std::string name; + std::string description; + std::vector options; + }; + class Options { public: @@ -390,7 +397,7 @@ namespace cxxopts parse_positional(std::string option); std::string - help() const; + help(const std::vector& groups = {""}) const; private: @@ -425,6 +432,9 @@ namespace cxxopts const std::string& name ); + std::string + help_one_group(const std::string& group) const; + std::string m_program; std::string m_help_string; @@ -432,7 +442,7 @@ namespace cxxopts std::string m_positional; //mapping from groups to help options - std::map> m_help; + std::map m_help; }; class OptionAdder diff --git a/src/example.cpp b/src/example.cpp index 5180c86..a1d237c 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -48,10 +48,20 @@ int main(int argc, char* argv[]) ("option_that_is_too_long_for_the_help", "A very long option") ; + options.add_options("Group") + ("c,compile", "compile") + ("d,drop", "drop", cxxopts::value>()); + options.parse_positional("positional"); options.parse(argc, argv); + if (options.count("help")) + { + std::cout << options.help({"", "Group"}) << std::endl; + exit(0); + } + if (apple) { std::cout << "Saw option ‘a’" << std::endl; @@ -72,11 +82,6 @@ int main(int argc, char* argv[]) } } - if (options.count("help")) - { - std::cout << options.help() << std::endl; - } - if (options.count("positional")) { std::cout << "Positional = " << options["positional"].as()