From 923b426464653257d5383c8de8ec2cc3b27ba07c Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Fri, 4 Nov 2011 13:13:00 -0400 Subject: [PATCH 1/2] Add support for a metavar in typed_value This changes the text for the placeholder argument in the help output. It can be used to indicated that a directory path is preferred rather than a file path or similar. --- boost/program_options/detail/value_semantic.hpp | 7 ++++--- boost/program_options/value_semantic.hpp | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/boost/program_options/detail/value_semantic.hpp b/boost/program_options/detail/value_semantic.hpp index e4b15d7..ba00924 100644 --- a/boost/program_options/detail/value_semantic.hpp +++ b/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_metavar.empty() ? arg : m_metavar); 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/boost/program_options/value_semantic.hpp b/boost/program_options/value_semantic.hpp index 033009e..0c9da1c 100644 --- a/boost/program_options/value_semantic.hpp +++ b/boost/program_options/value_semantic.hpp @@ -227,6 +227,15 @@ namespace boost { namespace program_options { return this; } + /** Specifies a metavar for the argument. This is the text used for + the argument placeholder in the help output. + */ + typed_value* metavar(const std::string& m) + { + m_metavar = m; + 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 @@ -346,6 +355,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_metavar; boost::any m_default_value; std::string m_default_value_as_text; boost::any m_implicit_value; -- 1.7.6.4