diff --git a/include/boost/program_options/detail/value_semantic.hpp b/include/boost/program_options/detail/value_semantic.hpp index 9531339a34..cb357ab010 100644 --- a/include/boost/program_options/detail/value_semantic.hpp +++ b/include/boost/program_options/detail/value_semantic.hpp @@ -6,6 +6,7 @@ // This file defines template functions that are declared in // ../value_semantic.hpp. +#include #include // forward declaration @@ -46,6 +47,39 @@ namespace boost { namespace program_options { } } + template + std::string make_textual(const T& v, value_semantic*, long) + { + return boost::lexical_cast(v); + } + + template + std::string make_textual(const std::vector& v, value_semantic*, long) + { + std::string textual; + for (unsigned i = 0; i < v.size(); ++i) + { + if (i != 0) + textual += ' '; + textual += make_textual(v[i], (value_semantic*)0, 0); + } + return textual; + } + + template + typed_value* + typed_value::default_value(const T& v) + { + return default_value(v, make_textual(v, this, 0)); + } + + template + typed_value* + typed_value::implicit_value(const T &v) + { + return implicit_value(v, make_textual(v, this, 0)); + } + namespace validators { /* If v.size() > 1, throw validation_error. If v.size() == 1, return v.front() diff --git a/include/boost/program_options/value_semantic.hpp b/include/boost/program_options/value_semantic.hpp index ac9dbc663b..7794ecbfe6 100644 --- a/include/boost/program_options/value_semantic.hpp +++ b/include/boost/program_options/value_semantic.hpp @@ -11,7 +11,6 @@ #include #include -#include #include #include @@ -194,14 +193,10 @@ namespace boost { namespace program_options { /** Specifies default value, which will be used if none is explicitly specified. The type 'T' should - provide operator<< for ostream. + provide operator<< for ostream, or the 'make_textual' + function should be specialized. */ - typed_value* default_value(const T& v) - { - m_default_value = boost::any(v); - m_default_value_as_text = boost::lexical_cast(v); - return this; - } + typed_value* default_value(const T& v); /** Specifies default value, which will be used if none is explicitly specified. Unlike the above overload, @@ -218,15 +213,14 @@ namespace boost { namespace program_options { /** 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, + Using this implies that an explicit value is optional, but if + given, must be strictly adjacent to the option, i.e.: '-ovalue' + or '--option=value'. Giving '-o' or '--option' will cause the + implicit value to be applied. + The type 'T' should provide operator<< for ostream, or the + 'make_textual' function should be specialized. */ - typed_value* implicit_value(const T &v) - { - m_implicit_value = boost::any(v); - m_implicit_value_as_text = - boost::lexical_cast(v); - return this; - } + typed_value* implicit_value(const T &v); /** Specifies the name used to to the value in help message. */ typed_value* value_name(const std::string& name)