提供强大的命令行参数处理功能,不仅能够分析命令行,也能够从配置文件和环境变量中获取参数,实现了分出完善的程序配置选项处理功能。
program_options 库的解析程序选项功能由三个基本组件构成:
- 选项描述器:定义选项和选项的值
- 分析器:依据选项描述器的定义解析命令行参数或者数据文件
- 存储器:把分析器的结果保存起来共程序使用
boost::options 库的使用实例:
#include <boost/program_options.hpp>
using namespace boost;
//using namespace boost::program_options;
#include <string>
using std::string;
int main(int argc,char **argv)
{
program_options::options_description opts("demo options"); //核心类
opts.add_options() //增加程序选项
("help", "just a help info")
("filename", program_options::value<std::string>(), "to find a file");
program_options::variables_map vm; //选项存储map容器
program_options::store(program_options::parse_command_line(argc, argv, opts), vm); //解析存储
if (vm.count("help")){
cout << opts << endl;
//return 0;
}
if (vm.count("filename")){
cout << "find" << vm["filename"].as<string>() << endl;
}
if (vm.size() == 0){
cout << "no options" << endl;
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31