Ticket #1131: program_options_implicit.patch

File program_options_implicit.patch, 4.3 KB (added by Bryan Green <bgreen0@…>, 15 years ago)
  • boost/program_options/value_semantic.hpp

    RCS file: /cvsroot/boost/boost/boost/program_options/value_semantic.hpp,v
    retrieving revision 1.13
    diff -u -r1.13 value_semantic.hpp
     
    204204            return this;
    205205        }
    206206
     207        typed_value* implicit_value(const T &v)
     208        {
     209            m_implicit_value = boost::any(v);
     210            m_implicit_value_as_text =
     211                boost::lexical_cast<std::string>(v);
     212            return this;
     213        }
     214
     215        typed_value* implicit_value(const T &v, const std::string& textual)
     216        {
     217            m_implicit_value = boost::any(v);
     218            m_implicit_value_as_text = textual;
     219            return this;
     220        }
     221
    207222        /** Specifies a function to be called when the final value
    208223            is determined. */
    209224        typed_value* notifier(function1<void, const T&> f)
     
    243258
    244259        unsigned min_tokens() const
    245260        {
    246             if (m_zero_tokens) {
     261            if (m_zero_tokens || !m_implicit_value.empty()) {
    247262                return 0;
    248263            } else {
    249264                return 1;
     
    301316        // as boost::optional to avoid unnecessary instantiations.
    302317        boost::any m_default_value;
    303318        std::string m_default_value_as_text;
     319        boost::any m_implicit_value;
     320        std::string m_implicit_value_as_text;
    304321        bool m_composing, m_implicit, m_multitoken, m_zero_tokens;
    305322        boost::function1<void, const T&> m_notifier;
    306323    };
  • boost/program_options/detail/value_semantic.hpp

    RCS file: /cvsroot/boost/boost/boost/program_options/detail/value_semantic.hpp,v
    retrieving revision 1.15
    diff -u -r1.15 value_semantic.hpp
     
    1616    std::string
    1717    typed_value<T, charT>::name() const
    1818    {
    19         if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
     19        if (!m_implicit_value.empty() && !m_implicit_value_as_text.empty()) {
     20            return "[=arg] (=" + m_implicit_value_as_text + ")";
     21        }
     22        else if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
    2023            return arg + " (=" + m_default_value_as_text + ")";
    21         } else {
     24        }
     25        else {
    2226            return arg;
    2327        }
    2428    }
     
    2731    void
    2832    typed_value<T, charT>::notify(const boost::any& value_store) const
    2933    {
    30         const T* value = boost::any_cast<const T>(&value_store);
     34        const T* value;
     35        if (value_store.empty())
     36            value = boost::any_cast<const T>(&m_implicit_value);
     37        else
     38            value = boost::any_cast<const T>(&value_store);
    3139        if (m_store_to) {
    3240            *m_store_to = *value;
    3341        }
     
    148156    xparse(boost::any& value_store,
    149157           const std::vector<std::basic_string<charT> >& new_tokens) const
    150158    {
    151         validate(value_store, new_tokens, (T*)0, 0);
     159        if (!new_tokens.empty() || m_implicit_value.empty())
     160            validate(value_store, new_tokens, (T*)0, 0);
    152161    }
    153162
    154163    template<class T>
  • libs/program_options/src/cmdline.cpp

    RCS file: /cvsroot/boost/boost/libs/program_options/src/cmdline.cpp,v
    retrieving revision 1.23
    diff -u -r1.23 cmdline.cpp
     
    329329           
    330330            max_tokens -= opt.value.size();
    331331
     332            // A value is optional if min_tokens == 0, but max_tokens > 0.
     333            // If a value is optional, it must appear in opt.value (because
     334            // it was 'adjacent'.  Otherwise, remove the expectation of a
     335            // non-adjacent value.
     336            if (min_tokens == 0 && max_tokens > 0 && opt.value.empty())
     337                --max_tokens;
     338
    332339            // Everything's OK, move the values to the result.           
    333340            for(;!other_tokens.empty() && max_tokens--; ) {
    334341                opt.value.push_back(other_tokens[0]);