From 1a558d76bd97c26d2a454fcd3edb885034596728 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Wed, 29 Oct 2014 12:35:05 +1100 Subject: [PATCH] custom argument help --- src/cxxopts.hpp | 34 +++++++++++++++++++++++++--------- src/example.cpp | 4 ++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index c2f99bc..4f4fe7b 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -515,6 +515,7 @@ namespace cxxopts std::string l; String desc; bool has_arg; + std::string arg_help; }; struct HelpGroupDetails @@ -550,7 +551,8 @@ namespace cxxopts const std::string& s, const std::string& l, std::string desc, - std::shared_ptr value + std::shared_ptr value, + std::string arg_help ); int @@ -655,7 +657,8 @@ namespace cxxopts const std::string& opts, const std::string& desc, std::shared_ptr value - = ::cxxopts::value() + = ::cxxopts::value(), + std::string arg_help = "" ); private: @@ -685,7 +688,8 @@ namespace cxxopts ( const std::string& s, const std::string& l, - bool has_arg + bool has_arg, + const std::string& arg_help ) { String result = " "; @@ -706,7 +710,14 @@ namespace cxxopts if (has_arg) { - result += " arg"; + if (arg_help.size() != 0) + { + result += " " + toLocalString(arg_help); + } + else + { + result += " arg"; + } } return result; @@ -780,7 +791,8 @@ OptionAdder::operator() ( const std::string& opts, const std::string& desc, - std::shared_ptr value + std::shared_ptr value, + std::string arg_help ) { std::match_results result; @@ -794,7 +806,8 @@ OptionAdder::operator() const auto& s = result[2]; const auto& l = result[3]; - m_options.add_option(m_group, s.str(), l.str(), desc, value); + m_options.add_option(m_group, s.str(), l.str(), desc, value, + std::move(arg_help)); return *this; } @@ -987,7 +1000,8 @@ Options::add_option const std::string& s, const std::string& l, std::string desc, - std::shared_ptr value + std::shared_ptr value, + std::string arg_help ) { auto stringDesc = toLocalString(std::move(desc)); @@ -1006,7 +1020,9 @@ Options::add_option //add the help details auto& options = m_help[group]; options.options. - emplace_back(HelpOptionDetails{s, l, stringDesc, value->has_arg()}); + emplace_back(HelpOptionDetails{s, l, stringDesc, value->has_arg(), + std::move(arg_help)} + ); } void @@ -1048,7 +1064,7 @@ Options::help_one_group(const std::string& g) const for (const auto& o : group->second.options) { - auto s = format_option(o.s, o.l, o.has_arg); + auto s = format_option(o.s, o.l, o.has_arg, o.arg_help); longest = std::max(longest, stringLength(s)); format.push_back(std::make_pair(s, String())); } diff --git a/src/example.cpp b/src/example.cpp index c6500c6..d499282 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -37,14 +37,14 @@ int main(int argc, char* argv[]) options.add_options() ("a,apple", "an apple", cxxopts::value(apple)) ("b,bob", "Bob") - ("f,file", "File", cxxopts::value>()) + ("f,file", "File", cxxopts::value>(), "FILE") ("positional", "Positional arguments: these are the arguments that are entered " "without an option", cxxopts::value()) ("long-description", "thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace") ("help", "Print help") - ("int", "An integer", cxxopts::value()) + ("int", "An integer", cxxopts::value(), "N") ("option_that_is_too_long_for_the_help", "A very long option") #ifdef CXXOPTS_USE_UNICODE ("unicode", u8"A help option with non-ascii: à. Here the size of the"