ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

boost 操作系统相关的库- program_options系统 (命令行,配置文件,环境变量中解析)

2022-02-14 18:34:43  阅读:205  来源: 互联网

标签:配置文件 show value options add program include boost po


program_options具体含义

https://blog.csdn.net/weixin_39766005/article/details/121790676

以下功能:

 首先从命令行解析,如果没有设置,则从配置文件中解析,如果配置文件也没设置,则从环境变量中解析

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <boost/filesystem.hpp>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <typeinfo>
#include <algorithm>
#include <list>
#include <set>
#include <future>
using namespace std;
#include<iostream>
#include<thread>
#include <mutex>
#include <signal.h>
#include <fstream>
#include <condition_variable>
#include <ctime>
#include <iostream>
#include <string>
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/asio.hpp>
#include <signal.h>
#include <boost/optional.hpp>
#include <bits/stdc++.h>
#include <boost/optional.hpp>
#include <iterator>
#include <boost/program_options.hpp>using namespace std;
using namespace boost::filesystem;
namespace po=boost::program_options;
int main(int argc,char* argv[])
{
  po::options_description des("add option:");
  des.add_options()
  ("help,h","show some help info")
  ("vesion,v","show version info")
  ("size,s",po::value<int>(),"show size info")
  ("method,m",po::value<std::string>()->default_value("GET"),"show method...")
  ("config,f",po::value<std::string>()->default_value("test.ini"),"set config file");
  po::options_description hide("show hide");
  hide.add_options()
  ("filename,f",po::value<std::vector<std::string>>(),"all filename");//需要设置多个则设置类型保存为vector即可
  po::options_description all;
  all.add(des).add(hide);//隐藏filename的帮助信息
  po::positional_options_description pos_de;
  pos_de.add("filename",-1);//位置选项设置为-1,表示所有无选项的都作为filename
  po::variables_map vm;
  try
  {
    /* code */
//从命令行解析 auto parse_base=po::command_line_parser(argc,argv).options(all).positional(pos_de).run(); po::store(parse_base,vm); } catch(const std::exception& e) { std::cerr << e.what() << '\n'; } //config 从配置文件中读取,vm中key一旦设置,后续设置无效 auto config_file=vm["config"].as<std::string>(); //check config_file exist TODO po::options_description _config; _config.add_options() ("size,-s",po::value<int>(),"show size info"); po::store(po::parse_config_file<char>(config_file.c_str(),_config),vm); //env 从环境变量中读取读取 export TEST_SIZE=1234 ,前提之前未设置过size po::options_description env_des; env_des.add_options() ("size,-s",po::value<int>(),"show size info") ("method,m",po::value<std::string>()->default_value("GET"),"show method..."); po::store(po::parse_environment(env_des,[](const string& env_name) { std::map<std::string,std::string> _env_map{{"TEST_SIZE","size"},{"TEST_METHOD","method"}}; return _env_map[env_name]; }),vm); vm.notify(); if(argc<2 || vm.count("help")==1) { cout<<des<<endl; return 1; } if(vm.count("version")==1) { cout<<"version 1.0"<<endl; return 1; } if(vm.count("size")==1) { cout<<"size:"<<vm["size"].as<int>()<<endl; } else { cout<<"--size no invalid"<<endl; return 1; } if(vm.count("method")==1) { cout<<"method:"<<vm["method"].as<std::string>()<<endl; } if(vm.count("filename")==1) { cout<<"filename:"; for(auto& file:vm["filename"].as<std::vector<std::string>>()) { cout<<file<<endl; } } return 0; }

 

标签:配置文件,show,value,options,add,program,include,boost,po
来源: https://www.cnblogs.com/bwbfight/p/15893701.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有