Boost program_options with composing() and implicit_value() are not “composed”

夙愿已清 提交于 2019-12-05 20:15:52

Here's a workaround that doesn't require modifying boost sources. If you separate the parse and store tasks then you can modify the intermediate boost::program_options::parsed_options vector of options. Each element of the vector contains the std::string key and a std::vector<std::string> of values. The workaround relies on the fact that for implicit values, that vector of values is empty. If we scan parsed_options for implicit values and explicitly assign them a value then they don't clobber previous values of the same key. Here's working code:

#include <iostream>
#include <string>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

namespace std {
   // This overload is needed to use composed options.
   static std::ostream& operator<<(
      std::ostream& os,
      const std::vector<std::string>& v) {
      os << '{';
      BOOST_FOREACH(const std::string& s, v) {
         if (&s != &*v.begin())
            os << ", ";
         os << '"' << s << '"';
      }
      os << '}';
      return os;
   }
}

int main(int argc, char *argv[]) {
   po::options_description desc("Allowed options");
   desc.add_options()
      ("debug,D",
       po::value<std::vector<std::string> >()
       ->composing()
       ->implicit_value(std::vector<std::string>(1,"1")),
       "Set debug level.");

   // Just parse the options without storing them in the map.
   po::parsed_options parsed_options = po::command_line_parser(argc, argv)
      .options(desc)
      .run();

   // Implicit option values are empty, replace with default value.
   BOOST_FOREACH(po::option& o, parsed_options.options) {
      if (o.string_key == "debug" && o.value.empty())
         o.value.push_back("1"); // default value is "1"
   }

   // Now store and earlier values aren't clobbered.
   po::variables_map vm;
   po::store(parsed_options, vm);
   po::notify(vm);

   std::cout << "variables_map[\"debug\"] = "
             << (vm.count("debug") ?
                 vm["debug"].as<std::vector<std::string> >() :
                 std::vector<std::string>())
             << '\n';
   return 0;
}

And here's those same test cases:

$ ./a.out -D abc -D 255 -D xyz
variables_map["debug"] = {"abc", "255", "xyz"}

$ ./a.out -D -D 255 -D xyz
variables_map["debug"] = {"1", "255", "xyz"}

$ ./a.out -D abc -D -D xyz
variables_map["debug"] = {"abc", "1", "xyz"}

$ ./a.out -D abc -D 255 -D
variables_map["debug"] = {"abc", "255", "1"}

Well, I eventually figured out a solution that seems to work for me. It would be nice to have some independent verification from somebody fluent in boost::program_options if possible, but apparently nobody seems to know or care much about it.

Here is a patch to boost_1_49_0 that will allow an option to be both composing() and also have an 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);
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!