l_parser(argc,argv,style_);
19
20 variables_map vm;
21 BOOST_AUTO(pr, command_line_parser(argc,argv).options(opts).allow_unregistered().run());
22 vector ur_opts = collect_unrecognized(pr.options, include_positional);
23 BOOST_FOREACH(string str, ur_opts)
24 {
25 if(str.find(style_[1]) == std::string::npos)
26 std::cerr << "Unknown option: " << str << std::endl;
27 }
28 store(pr,vm);
29 notify(vm);
30
31 if(vm.size() == 0 && argc == 1)
32 {
33 std::cout << opts << std::endl;
34 return ;
35 }
36
37 for(vci it = data_.begin(); it != data_.end(); ++it)
38 {
39 const std::string& key = it->get<0>();
40 if(vm.count(key.c_str()))
41 it->get<3>()(key);
42 }
43
44 }
用户可以继承这个基类后,添加选项,描述,想对应的处理函数等等。
等等,你是不是发现啥问题了,对,按照我这样的写法,你是无法实现-L./xxx.so这样的功能的。我并没有提供一个参数后跟一个输入的形式,主要原因是我觉得这样的例子并不直观,我更倾向于L=xx.so这样的表达式,所以我提供了special_parser这个函数,你可以很方便的扩展成你想要的style。
当然现在的这个功能还是很单一的,比如因为我的handler是以此调用,所以假如你的选项之间有依赖关系的话,在添加时就得格外小心了。
暂时就这些,肯定还有很多理解不到的地方,请大家多多指教阿。
闲人草堂