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; }