Skip to content

Commit 43e5366

Browse files
committed
Merge: Allow to specify how option's value is named in help message.
Fixes #4781. [SVN r77932]
1 parent cddd2c5 commit 43e5366

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

include/boost/program_options/detail/value_semantic.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ namespace boost { namespace program_options {
1616
std::string
1717
typed_value<T, charT>::name() const
1818
{
19+
std::string const& var = (m_value_name.empty() ? arg : m_value_name);
1920
if (!m_implicit_value.empty() && !m_implicit_value_as_text.empty()) {
20-
std::string msg = "[=arg(=" + m_implicit_value_as_text + ")]";
21+
std::string msg = "[=" + var + "(=" + m_implicit_value_as_text + ")]";
2122
if (!m_default_value.empty() && !m_default_value_as_text.empty())
2223
msg += " (=" + m_default_value_as_text + ")";
2324
return msg;
2425
}
2526
else if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
26-
return arg + " (=" + m_default_value_as_text + ")";
27+
return var + " (=" + m_default_value_as_text + ")";
2728
} else {
28-
return arg;
29+
return var;
2930
}
3031
}
3132

include/boost/program_options/value_semantic.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ namespace boost { namespace program_options {
227227
return this;
228228
}
229229

230+
/** Specifies the name used to to the value in help message. */
231+
typed_value* value_name(const std::string& name)
232+
{
233+
m_value_name = name;
234+
return this;
235+
}
236+
230237
/** Specifies an implicit value, which will be used
231238
if the option is given, but without an adjacent value.
232239
Using this implies that an explicit value is optional, but if
@@ -354,6 +361,7 @@ namespace boost { namespace program_options {
354361

355362
// Default value is stored as boost::any and not
356363
// as boost::optional to avoid unnecessary instantiations.
364+
std::string m_value_name;
357365
boost::any m_default_value;
358366
std::string m_default_value_as_text;
359367
boost::any m_implicit_value;

test/options_description_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,21 @@ void test_default_values()
228228
);
229229
}
230230

231+
void test_value_name()
232+
{
233+
options_description desc("Supported options");
234+
desc.add_options()
235+
("include", value<string>()->value_name("directory"), "Search for headers in 'directory'.")
236+
;
237+
238+
stringstream ss;
239+
ss << desc;
240+
BOOST_CHECK_EQUAL(ss.str(),
241+
"Supported options:\n"
242+
" --include directory Search for headers in 'directory'.\n"
243+
);
244+
}
245+
231246

232247
int main(int, char* [])
233248
{
@@ -238,6 +253,7 @@ int main(int, char* [])
238253
test_long_default_value();
239254
test_word_wrapping();
240255
test_default_values();
256+
test_value_name();
241257
return 0;
242258
}
243259

0 commit comments

Comments
 (0)