好吧,我最终想出了一个似乎对我有用的解决方案。如果可能的话,从流利的人那里获得一些独立的验证会很好boost::program_options
,但显然没有人知道或关心它。
这是 boost_1_49_0 的一个补丁,它允许一个选项既是 composing() 也有一个 implicit_value()。
diff -Naur old/boost/program_options/detail/value_semantic.hpp new/boost/program_options/detail/value_semantic.hpp
--- old/boost/program_options/detail/value_semantic.hpp 2010-07-12 03:14:14.000000000 -0400
+++ new/boost/program_options/detail/value_semantic.hpp 2012-08-17 16:31:03.000000000 -0400
@@ -154,6 +154,28 @@
}
}
+ // Helper function to copy a non-vector implicit value into the
+ // tokens vector.
+ template<class T, class charT>
+ void get_implicit_tokens(std::vector<std::basic_string<charT> >& vs,
+ const boost::any& a,
+ T*, long) {
+ const T va = boost::any_cast<const T>(a);
+ vs.push_back(boost::lexical_cast<std::basic_string<charT> >(va));
+ }
+
+ // Helper function to copy a vector implicit value into the
+ // tokens vector.
+ template<class T, class charT>
+ void get_implicit_tokens(std::vector<std::basic_string<charT> >& vs,
+ const boost::any& a,
+ std::vector<T>*, int) {
+ const std::vector<T> va = boost::any_cast<const std::vector<T> >(a);
+ for (unsigned i = 0; i < va.size(); i++) {
+ vs.push_back(boost::lexical_cast<std::basic_string<charT> >(va[i]));
+ }
+ }
+
template<class T, class charT>
void
typed_value<T, charT>::
@@ -164,7 +186,14 @@
// value, then assign the implicit value as the stored value;
// otherwise, validate the user-provided token(s).
if (new_tokens.empty() && !m_implicit_value.empty())
- value_store = m_implicit_value;
+ if (m_composing) {
+ // Attempt to append the implicit value.
+ std::vector<std::basic_string<charT> > vs;
+ get_implicit_tokens(vs, m_implicit_value, (T*)0, 0);
+ validate(value_store, vs, (T*)0, 0);
+ } else {
+ value_store = m_implicit_value;
+ }
else
validate(value_store, new_tokens, (T*)0, 0);
}