diff --git a/include/boost/program_options/detail/value_semantic.hpp b/include/boost/program_options/detail/value_semantic.hpp index e4b15d7..814a3db 100644 --- a/include/boost/program_options/detail/value_semantic.hpp +++ b/include/boost/program_options/detail/value_semantic.hpp @@ -16,16 +16,17 @@ namespace boost { namespace program_options { std::string typed_value::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; } } diff --git a/include/boost/program_options/value_semantic.hpp b/include/boost/program_options/value_semantic.hpp index 7b34bb2..081e997 100644 --- a/include/boost/program_options/value_semantic.hpp +++ b/include/boost/program_options/value_semantic.hpp @@ -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; diff --git a/test/options_description_test.cpp b/test/options_description_test.cpp index 59f6bac..d443a7b 100644 --- a/test/options_description_test.cpp +++ b/test/options_description_test.cpp @@ -228,6 +228,21 @@ void test_default_values() ); } +void test_value_name() +{ + options_description desc("Supported options"); + desc.add_options() + ("include", value()->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; }