Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customization point for to-string conversion #121

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions include/boost/program_options/detail/value_semantic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// This file defines template functions that are declared in
// ../value_semantic.hpp.

#include <boost/lexical_cast.hpp>
#include <boost/throw_exception.hpp>

// forward declaration
Expand Down Expand Up @@ -46,6 +47,39 @@ namespace boost { namespace program_options {
}
}

template<class T>
std::string make_textual(const T& v, value_semantic*, long)
{
return boost::lexical_cast<std::string>(v);
}

template<class T>
std::string make_textual(const std::vector<T>& 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<class T, class charT>
typed_value<T, charT>*
typed_value<T, charT>::default_value(const T& v)
{
return default_value(v, make_textual(v, this, 0));
}

template<class T, class charT>
typed_value<T, charT>*
typed_value<T, charT>::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()
Expand Down
26 changes: 10 additions & 16 deletions include/boost/program_options/value_semantic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <boost/any.hpp>
#include <boost/function/function1.hpp>
#include <boost/lexical_cast.hpp>

#include <string>
#include <vector>
Expand Down Expand Up @@ -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<std::string>(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,
Expand All @@ -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<std::string>(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)
Expand Down