Allow to specify how option's value is named in help message.

Fixes #4781.


[SVN r77931]
This commit is contained in:
Vladimir Prus
2012-04-12 08:37:34 +00:00
parent fd7b310993
commit 5cbfa80841
3 changed files with 28 additions and 3 deletions

View File

@@ -16,16 +16,17 @@ namespace boost { namespace program_options {
std::string
typed_value<T, charT>::name() const
{
std::string const& var = (m_value_name.empty() ? arg : m_value_name);
if (!m_implicit_value.empty() && !m_implicit_value_as_text.empty()) {
std::string msg = "[=arg(=" + m_implicit_value_as_text + ")]";
std::string msg = "[=" + var + "(=" + m_implicit_value_as_text + ")]";
if (!m_default_value.empty() && !m_default_value_as_text.empty())
msg += " (=" + m_default_value_as_text + ")";
return msg;
}
else if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
return arg + " (=" + m_default_value_as_text + ")";
return var + " (=" + m_default_value_as_text + ")";
} else {
return arg;
return var;
}
}

View File

@@ -227,6 +227,13 @@ namespace boost { namespace program_options {
return this;
}
/** Specifies the name used to to the value in help message. */
typed_value* value_name(const std::string& name)
{
m_value_name = name;
return this;
}
/** Specifies an implicit value, which will be used
if the option is given, but without an adjacent value.
Using this implies that an explicit value is optional, but if
@@ -354,6 +361,7 @@ namespace boost { namespace program_options {
// Default value is stored as boost::any and not
// as boost::optional to avoid unnecessary instantiations.
std::string m_value_name;
boost::any m_default_value;
std::string m_default_value_as_text;
boost::any m_implicit_value;

View File

@@ -228,6 +228,21 @@ void test_default_values()
);
}
void test_value_name()
{
options_description desc("Supported options");
desc.add_options()
("include", value<string>()->value_name("directory"), "Search for headers in 'directory'.")
;
stringstream ss;
ss << desc;
BOOST_CHECK_EQUAL(ss.str(),
"Supported options:\n"
" --include directory Search for headers in 'directory'.\n"
);
}
int main(int, char* [])
{
@@ -238,6 +253,7 @@ int main(int, char* [])
test_long_default_value();
test_word_wrapping();
test_default_values();
test_value_name();
return 0;
}