Complete the help

This commit is contained in:
Baptiste Wicht 2014-10-26 21:18:54 +01:00
parent 22ab9836d4
commit 7e8f2e0b26

View File

@ -51,6 +51,9 @@ namespace cxxopts
virtual bool virtual bool
has_default() const = 0; has_default() const = 0;
virtual std::string
get_default_value() const = 0;
}; };
class OptionException : public std::exception class OptionException : public std::exception
@ -261,6 +264,12 @@ namespace cxxopts
return false; return false;
} }
std::string
get_default_value() const
{
return "";
}
const T& const T&
get() const get() const
{ {
@ -314,6 +323,12 @@ namespace cxxopts
return true; return true;
} }
std::string
get_default_value() const
{
return m_default;
}
private: private:
T m_default; T m_default;
}; };
@ -412,6 +427,8 @@ namespace cxxopts
std::string l; std::string l;
std::string desc; std::string desc;
bool has_arg; bool has_arg;
bool has_default;
std::string default_value;
}; };
struct HelpGroupDetails struct HelpGroupDetails
@ -580,11 +597,12 @@ namespace cxxopts
std::string std::string
format_option format_option
( (
const std::string& s, const HelpOptionDetails& o
const std::string& l,
bool has_arg
) )
{ {
auto& s = o.s;
auto& l = o.l;
std::string result = " "; std::string result = " ";
if (s.size() > 0) if (s.size() > 0)
@ -601,9 +619,14 @@ namespace cxxopts
result += " --" + l; result += " --" + l;
} }
if (has_arg) if (o.has_arg)
{ {
result += " arg"; result += " arg";
if (o.has_default)
{
result += " [" + o.default_value + "]";
}
} }
return result; return result;
@ -911,7 +934,8 @@ Options::add_option
//add the help details //add the help details
auto& options = m_help[group]; auto& options = m_help[group];
options.options.emplace_back(HelpOptionDetails{s, l, desc, value->has_arg()}); options.options.emplace_back(HelpOptionDetails{s, l, desc,
value->has_arg(), value->has_default(), value->get_default_value()});
} }
void void
@ -953,7 +977,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);
longest = std::max(longest, s.size()); longest = std::max(longest, s.size());
format.push_back(std::make_pair(s, std::string())); format.push_back(std::make_pair(s, std::string()));
} }