custom argument help

This commit is contained in:
Jarryd Beck 2014-10-29 12:35:05 +11:00
parent 8d7c4ea43e
commit 1a558d76bd
2 changed files with 27 additions and 11 deletions

View File

@ -515,6 +515,7 @@ namespace cxxopts
std::string l; std::string l;
String desc; String desc;
bool has_arg; bool has_arg;
std::string arg_help;
}; };
struct HelpGroupDetails struct HelpGroupDetails
@ -550,7 +551,8 @@ namespace cxxopts
const std::string& s, const std::string& s,
const std::string& l, const std::string& l,
std::string desc, std::string desc,
std::shared_ptr<const Value> value std::shared_ptr<const Value> value,
std::string arg_help
); );
int int
@ -655,7 +657,8 @@ namespace cxxopts
const std::string& opts, const std::string& opts,
const std::string& desc, const std::string& desc,
std::shared_ptr<const Value> value std::shared_ptr<const Value> value
= ::cxxopts::value<bool>() = ::cxxopts::value<bool>(),
std::string arg_help = ""
); );
private: private:
@ -685,7 +688,8 @@ namespace cxxopts
( (
const std::string& s, const std::string& s,
const std::string& l, const std::string& l,
bool has_arg bool has_arg,
const std::string& arg_help
) )
{ {
String result = " "; String result = " ";
@ -706,7 +710,14 @@ namespace cxxopts
if (has_arg) if (has_arg)
{ {
result += " arg"; if (arg_help.size() != 0)
{
result += " " + toLocalString(arg_help);
}
else
{
result += " arg";
}
} }
return result; return result;
@ -780,7 +791,8 @@ OptionAdder::operator()
( (
const std::string& opts, const std::string& opts,
const std::string& desc, const std::string& desc,
std::shared_ptr<const Value> value std::shared_ptr<const Value> value,
std::string arg_help
) )
{ {
std::match_results<const char*> result; std::match_results<const char*> result;
@ -794,7 +806,8 @@ OptionAdder::operator()
const auto& s = result[2]; const auto& s = result[2];
const auto& l = result[3]; 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; return *this;
} }
@ -987,7 +1000,8 @@ Options::add_option
const std::string& s, const std::string& s,
const std::string& l, const std::string& l,
std::string desc, std::string desc,
std::shared_ptr<const Value> value std::shared_ptr<const Value> value,
std::string arg_help
) )
{ {
auto stringDesc = toLocalString(std::move(desc)); auto stringDesc = toLocalString(std::move(desc));
@ -1006,7 +1020,9 @@ Options::add_option
//add the help details //add the help details
auto& options = m_help[group]; auto& options = m_help[group];
options.options. 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 void
@ -1048,7 +1064,7 @@ Options::help_one_group(const std::string& g) const
for (const auto& o : group->second.options) 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)); longest = std::max(longest, stringLength(s));
format.push_back(std::make_pair(s, String())); format.push_back(std::make_pair(s, String()));
} }

View File

@ -37,14 +37,14 @@ int main(int argc, char* argv[])
options.add_options() options.add_options()
("a,apple", "an apple", cxxopts::value<bool>(apple)) ("a,apple", "an apple", cxxopts::value<bool>(apple))
("b,bob", "Bob") ("b,bob", "Bob")
("f,file", "File", cxxopts::value<std::vector<std::string>>()) ("f,file", "File", cxxopts::value<std::vector<std::string>>(), "FILE")
("positional", ("positional",
"Positional arguments: these are the arguments that are entered " "Positional arguments: these are the arguments that are entered "
"without an option", cxxopts::value<std::string>()) "without an option", cxxopts::value<std::string>())
("long-description", ("long-description",
"thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace") "thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace")
("help", "Print help") ("help", "Print help")
("int", "An integer", cxxopts::value<int>()) ("int", "An integer", cxxopts::value<int>(), "N")
("option_that_is_too_long_for_the_help", "A very long option") ("option_that_is_too_long_for_the_help", "A very long option")
#ifdef CXXOPTS_USE_UNICODE #ifdef CXXOPTS_USE_UNICODE
("unicode", u8"A help option with non-ascii: à. Here the size of the" ("unicode", u8"A help option with non-ascii: à. Here the size of the"