boost之program_options库,解析命令行参数、读取配置文件

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 一、命令行解析 tprogram_options解析命令行参数示例代码:   [cpp] view plaincopy   #include    using namespace std;      #include    namespace po = boos...

一、命令行解析

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

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #include <boost/program_options.hpp>  
  5. namespace po = boost::program_options;  
  6.   
  7. int main(int argc, char*argv[])  
  8. {  
  9.     //int level;  
  10.     po::options_description desc("Allowed options");  
  11.     desc.add_options()  
  12.         ("help", "produce help message")  
  13.         //("help,h", "produce help message")  
  14.         ("compression", po::value<int>(), "set compression level");  
  15.         //("compression", po::value<int>(&level)->default_value(1), "set compression level");  
  16.   
  17.     po::variables_map vm;  
  18.     po::store(po::parse_command_line(argc, argv, desc), vm);  
  19.     po::notify(vm);  
  20.   
  21.     if(vm.count("help"))  
  22.     {  
  23.         cout<<desc<<endl;  
  24.         return 1;  
  25.     }  
  26.   
  27.     if(vm.count("compression"))  
  28.     {  
  29.         cout<<"compression level was set to "<<vm["compression"].as<int>()<<"."<<endl;  
  30.         //cout<<"compression level: "<<level<<endl;  
  31.     }  
  32.     else  
  33.     {  
  34.         cout<<"compression level was not set."<<endl;  
  35.     }  
  36.   
  37.     return 0;  
  38. }  



 

运行结果:

 

输入参数:--help

 

输入参数:--compression 10

 

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

2.1 代码

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #include <fstream>  
  2. #include <map>  
  3. using namespace std;  
  4.   
  5. #include <boost/program_options.hpp>  
  6. using namespace boost;  
  7. namespace po = boost::program_options;  
  8.   
  9. #ifdef WIN32   
  10. #include "C:\Users\gwy8868\Desktop\fast_dr302\fast_dr302\global\xtokens.h"  
  11. #else  
  12. #include "/opt/guowenyan/fast_dr302/global/xtokens.h"  
  13. #endif  
  14.   
  15.   
  16. std::pair<std::string, std::string> at_option_parser(std::string const& s)  
  17. {  
  18.     if ('@' == s[0])  
  19.     {  
  20.         return make_pair(std::string("config"), s.substr(1));  
  21.     }  
  22.     else  
  23.     {  
  24.         return std::pair<std::string, std::string>();  
  25.     }  
  26. }  
  27.   
  28. int main(int argc, char*argv[])  
  29. {  
  30.     //  
  31.     string host_ip;  
  32.     short  host_port;  
  33.   
  34.     string server_ip;  
  35.     short  server_port;  
  36.   
  37.     //  
  38.     po::options_description hostoptions("host options");  
  39.     hostoptions.add_options()  
  40.         ("host_ip,H", po::value<string>(&host_ip), "set db_host")  
  41.         ("host_port,P", po::value<short>(&host_port)->default_value(3306), "set db_port");  
  42.   
  43.     po::options_description general("general options");  
  44.     general.add_options()  
  45.         ("help,h", "produce help message")  
  46.         ("server_ip,s", po::value<string>(&server_ip), "set the http_server's ip. e.g. '202.106.0.20'")  
  47.         ("server_port,p", po::value<short>(&server_port)->default_value(80), "set the http_server's port. default:80");  
  48.   
  49.     string config_file;  
  50.     po::options_description config("config options");  
  51.     config.add_options()  
  52.         ("config", po::value<string>(&config_file)->default_value("config.conf"),  
  53.         "set config file, specified with '@name' too");  
  54.   
  55.     po::options_description all("All options");  
  56.     all.add(hostoptions).add(general).add(config);  
  57.   
  58.     po::variables_map vm;  
  59.     po::store(po::command_line_parser(argc, argv).options(all).extra_parser(::at_option_parser).run(), vm);   
  60.   
  61.     if (vm.count("help"))  
  62.     {  
  63.         cout << hostoptions <<endl;  
  64.         cout << general << endl;  
  65.         cout << config << endl;  
  66.         return false;  
  67.     }  
  68.   
  69.     if (vm.count("config"))  
  70.     {  
  71.         string conf_name = vm["config"].as<string>();  
  72.         ifstream ifs_config(conf_name.c_str());  
  73.   
  74.         if (! ifs_config)  
  75.         {  
  76.             cerr << "could not open the configure file" << endl;  
  77.             return false;  
  78.         }  
  79.   
  80.         stringstream ss_config;  
  81.         ss_config << ifs_config.rdbuf();  
  82.   
  83.         global::strings_t args;  
  84.         global::separate_tokens(ss_config.str(), args, " \r\n");  
  85.         po::store(po::command_line_parser(args).options(all).run(), vm);  
  86.     }  
  87.     po::notify(vm);  
  88.   
  89.   
  90.     //  
  91.     cout<<"host_ip: "<<host_ip<<endl;  
  92.     cout<<"host_port: "<<host_port<<endl;  
  93.   
  94.     cout<<"server_ip: "<<server_ip<<endl;  
  95.     cout<<"server_port: "<<server_port<<endl;  
  96.   
  97.     return 0;  
  98. }  

2.2 配置文件

 

config.conf:

config2.conf:

2.3 输出结果

 

目录
相关文章
|
29天前
|
JSON 小程序 UED
微信小程序 app.json 配置文件解析与应用
本文介绍了微信小程序中 `app.json` 配置文件的详细
132 12
|
2月前
|
JSON 自然语言处理 Java
OpenAI API深度解析:参数、Token、计费与多种调用方式
随着人工智能技术的飞速发展,OpenAI API已成为许多开发者和企业的得力助手。本文将深入探讨OpenAI API的参数、Token、计费方式,以及如何通过Rest API(以Postman为例)、Java API调用、工具调用等方式实现与OpenAI的交互,并特别关注调用具有视觉功能的GPT-4o使用本地图片的功能。此外,本文还将介绍JSON模式、可重现输出的seed机制、使用代码统计Token数量、开发控制台循环聊天,以及基于最大Token数量的消息列表限制和会话长度管理的控制台循环聊天。
800 7
|
3月前
|
数据采集 JavaScript API
网页解析库:BeautifulSoup与Cheerio的选择
网页解析库:BeautifulSoup与Cheerio的选择
|
3月前
|
JSON PHP 数据格式
PHP解析配置文件的常用方法
INI文件是最常见的配置文件格式之一。
67 12
|
3月前
|
存储 Go PHP
Go语言中的加解密利器:go-crypto库全解析
在软件开发中,数据安全和隐私保护至关重要。`go-crypto` 是一个专为 Golang 设计的加密解密工具库,支持 AES 和 RSA 等加密算法,帮助开发者轻松实现数据的加密和解密,保障数据传输和存储的安全性。本文将详细介绍 `go-crypto` 的安装、特性及应用实例。
198 0
|
4月前
|
存储 安全 网络协议
Elasticsearch 配置文件解析
【10月更文挑战第3天】Elasticsearch 配置文件解析
147 3
|
4月前
|
SQL Oracle 关系型数据库
SQL整库导出语录:全面解析与高效执行策略
在数据库管理和维护过程中,整库导出是一项常见的需求,无论是为了备份、迁移还是数据分析,掌握如何高效、准确地导出整个数据库至关重要
|
3月前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
131 2
|
2月前
|
设计模式 存储 安全
【23种设计模式·全精解析 | 创建型模式篇】5种创建型模式的结构概述、实现、优缺点、扩展、使用场景、源码解析
创建型模式的主要关注点是“怎样创建对象?”,它的主要特点是"将对象的创建与使用分离”。这样可以降低系统的耦合度,使用者不需要关注对象的创建细节。创建型模式分为5种:单例模式、工厂方法模式抽象工厂式、原型模式、建造者模式。
【23种设计模式·全精解析 | 创建型模式篇】5种创建型模式的结构概述、实现、优缺点、扩展、使用场景、源码解析
|
2月前
|
存储 设计模式 算法
【23种设计模式·全精解析 | 行为型模式篇】11种行为型模式的结构概述、案例实现、优缺点、扩展对比、使用场景、源码解析
行为型模式用于描述程序在运行时复杂的流程控制,即描述多个类或对象之间怎样相互协作共同完成单个对象都无法单独完成的任务,它涉及算法与对象间职责的分配。行为型模式分为类行为模式和对象行为模式,前者采用继承机制来在类间分派行为,后者采用组合或聚合在对象间分配行为。由于组合关系或聚合关系比继承关系耦合度低,满足“合成复用原则”,所以对象行为模式比类行为模式具有更大的灵活性。 行为型模式分为: • 模板方法模式 • 策略模式 • 命令模式 • 职责链模式 • 状态模式 • 观察者模式 • 中介者模式 • 迭代器模式 • 访问者模式 • 备忘录模式 • 解释器模式
【23种设计模式·全精解析 | 行为型模式篇】11种行为型模式的结构概述、案例实现、优缺点、扩展对比、使用场景、源码解析

热门文章

最新文章

推荐镜像

更多