温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

boost中program_options库如何解析命令行参数以及读取配置文件

发布时间:2021-10-14 09:28:55 来源:亿速云 阅读:300 作者:柒染 栏目:编程语言

boost中program_options库如何解析命令行参数以及读取配置文件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

一、命令行解析

tprogram_options解析命令行参数示例代码:

#include <iostream> using namespace std; #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc, char*argv[]) {	//int level;	po::options_description desc("Allowed options");	desc.add_options()	("help", "produce help message")	//("help,h", "produce help message")	("compression", po::value<int>(), "set compression level");	//("compression", po::value<int>(&level)->default_value(1), "set compression level");	po::variables_map vm;	po::store(po::parse_command_line(argc, argv, desc), vm);	po::notify(vm);	if(vm.count("help"))	{	cout<<desc<<endl;	return 1;	}	if(vm.count("compression"))	{	cout<<"compression level was set to "<<vm["compression"].as<int>()<<"."<<endl;	//cout<<"compression level: "<<level<<endl;	}	else	{	cout<<"compression level was not set."<<endl;	}	return 0; }

运行结果:

输入参数:--help

boost中program_options库如何解析命令行参数以及读取配置文件

输入参数:--compression
10

boost中program_options库如何解析命令行参数以及读取配置文件

二、读取配置文件(Linux、Windows均可)

2.1 代码

#include <fstream> #include <map> using namespace std; #include <boost/program_options.hpp> using namespace boost; namespace po = boost::program_options; #ifdef WIN32  #include "C:\Users\gwy8868\Desktop\fast_dr302\fast_dr302\global\xtokens.h" #else #include "/opt/guowenyan/fast_dr302/global/xtokens.h" #endif std::pair<std::string, std::string> at_option_parser(std::string const& s) {	if ('@' == s[0])	{	return make_pair(std::string("config"), s.substr(1));	}	else	{	return std::pair<std::string, std::string>();	} } int main(int argc, char*argv[]) {	//	string host_ip;	short  host_port;	string server_ip;	short  server_port;	//	po::options_description hostoptions("host options");	hostoptions.add_options()	("host_ip,H", po::value<string>(&host_ip), "set db_host")	("host_port,P", po::value<short>(&host_port)->default_value(3306), "set db_port");	po::options_description general("general options");	general.add_options()	("help,h", "produce help message")	("server_ip,s", po::value<string>(&server_ip), "set the http_server's ip. e.g. '202.106.0.20'")	("server_port,p", po::value<short>(&server_port)->default_value(80), "set the http_server's port. default:80");	string config_file;	po::options_description config("config options");	config.add_options()	("config", po::value<string>(&config_file)->default_value("config.conf"),	"set config file, specified with '@name' too");	po::options_description all("All options");	all.add(hostoptions).add(general).add(config);	po::variables_map vm;	po::store(po::command_line_parser(argc, argv).options(all).extra_parser(::at_option_parser).run(), vm); 	if (vm.count("help"))	{	cout << hostoptions <<endl;	cout << general << endl;	cout << config << endl;	return false;	}	if (vm.count("config"))	{	string conf_name = vm["config"].as<string>();	ifstream ifs_config(conf_name.c_str());	if (! ifs_config)	{	cerr << "could not open the configure file" << endl;	return false;	}	stringstream ss_config;	ss_config << ifs_config.rdbuf();	global::strings_t args;	global::separate_tokens(ss_config.str(), args, " \r\n");	po::store(po::command_line_parser(args).options(all).run(), vm);	}	po::notify(vm);	//	cout<<"host_ip: "<<host_ip<<endl;	cout<<"host_port: "<<host_port<<endl;	cout<<"server_ip: "<<server_ip<<endl;	cout<<"server_port: "<<server_port<<endl;	return 0; }

2.2 配置文件

config.conf:

boost中program_options库如何解析命令行参数以及读取配置文件

config2.conf:
boost中program_options库如何解析命令行参数以及读取配置文件

2.3 输出结果

boost中program_options库如何解析命令行参数以及读取配置文件

看完上述内容,你们掌握boost中program_options库如何解析命令行参数以及读取配置文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI