2012-08-02 38 views
2

我有在一个选项的情况下,升压program_options(v1_49)()中的问题定义为构成,也隐式的()。我的意图是实现类似于perl的-D选项,以便您可以执行-D或-Dname并多次使用它。我options_description是:与构成()和implicit_value升压program_options()不是“组成的”

( "debug,D", 
    bpo::value<vector<string> >() 
     ->composing() 
     ->implicit_value(vector<string>(1,"1")), 
    "Set debug level." 
), 

这似乎在大多数情况下工作正常,但每当-D没有值出现在命令行中,所有早期值都被清除,例如:

$ ./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"] = {"1", "xyz"} 

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

我想我明白为什么会发生这种情况,隐式值{“1”}会替换现有的矢量而不是添加到它。有什么我可以做得到这个工作,或者是boost :: program_options的限制吗?

+0

您可以尝试只具有'的std :: string'内含价值,而不是一个'的std :: VECTOR'之一。 – Xeo 2012-08-07 18:42:33

+0

不确定你的意思,隐式值的类型必须与值的类型匹配。如果不是,则该语句不会编译。 – user9645 2012-08-10 12:00:20

+0

那么我在'boost/program_options/detail/value_semantic.hpp:xparse()'中发现了破损的代码:在'value_store = m_implicit_value;'的情况下,没有'm_composing == true'的检查。任何人都可以帮助建议一些方法来追加'value_store'而不是分配给它? – user9645 2012-08-17 19:42:07

回答

2

这是一个解决方法,不需要修改助推源。如果将解析和存储任务分开,则可以修改中间选项的向量boost::program_options::parsed_options。矢量的每个元素包含std::string键和值的std::vector<std::string>。解决方法依赖于这样的事实:对于隐式值,该值的向量为空。如果我们扫描parsed_options隐式的价值观和明确地为它们分配一个值,那么他们就不会破坏相同键的以前的值。这里的工作代码:

#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; 
} 

而这里的那些相同的测试用例:

$ ./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"} 
+0

非常感谢,这是比修改boost代码更好的方法,特别是当我真的不知道我在那里做什么时...... – user9645 2013-05-28 18:18:55

0

好了,我终于想通了,似乎为我工作的解决方案。这将是很好,如果可能有从别人流利的一些独立核查boost::program_options,但显然没人似乎知道或在意它。

下面是boost_1_49_0,将允许一个选项是既构成(),也有一个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); 
    }