program_options
【program_options】
The program_options library allows program developers to obtain program options, that is (name, value) pairs from the user, via conventional methods such as command line and config file.
1、添加 options_description.
// Declare the supported options.
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level")
; po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm); if (vm.count("help")) {
cout << desc << "\n";
return ;
} if (vm.count("compression")) {
cout << vm["compression"].as<int>() << ".\n";
}
使用示例:

2、po::value
int opt;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("optimization", po::value<int>(&opt)->default_value(),
"optimization level")
("include-path,I", po::value< vector<string> >(),
"include path")
("input-file", po::value< vector<string> >(), "input file")
;
使用示例:

3、po::positional_options_description
po::positional_options_description p;
p.add("input-file", -); po::variables_map vm;
po::store(po::command_line_parser(ac, av).
options(desc).positional(p).run(), vm);
po::notify(vm);

4、config file
int opt;
string config_file; // Declare a group of options that will be
// allowed only on command line
po::options_description generic("Generic options");
generic.add_options()
("version,v", "print version string")
("help", "produce help message")
("config,c", po::value<string>(&config_file)->default_value("multiple_sources.cfg"),
"name of a file of a configuration.")
; // Declare a group of options that will be
// allowed both on command line and in
// config file
po::options_description config("Configuration");
config.add_options()
("optimization", po::value<int>(&opt)->default_value(),
"optimization level")
("include-path,I",
po::value< vector<string> >()->composing(),
"include path")
; // Hidden options, will be allowed both on command line and
// in config file, but will not be shown to the user.
po::options_description hidden("Hidden options");
hidden.add_options()
("input-file", po::value< vector<string> >(), "input file")
; po::options_description cmdline_options;
cmdline_options.add(generic).add(config).add(hidden); po::options_description config_file_options;
config_file_options.add(config).add(hidden); po::options_description visible("Allowed options");
visible.add(generic).add(config); po::positional_options_description p;
p.add("input-file", -); po::variables_map vm;
store(po::command_line_parser(ac, av).
options(cmdline_options).positional(p).run(), vm);
notify(vm); ifstream ifs(config_file.c_str());
if (!ifs){
cout << "can not open config file: " << config_file << "\n";
return ;
}
else{
store(parse_config_file(ifs, config_file_options), vm);
notify(vm);
} if (vm.count("help")) {
cout << visible << "\n";
return ;
} if (vm.count("version")) {
cout << "Multiple sources example, version 1.0\n";
return ;
} if (vm.count("include-path")){
cout << vm["include-path"].as< vector<string> >() << "\n";
} if (vm.count("input-file")){
cout << vm["input-file"].as< vector<string> >() << "\n";
} cout << opt << "\n";

参考:
1、https://www.boost.org/doc/libs/1_69_0/doc/html/program_options.html
program_options的更多相关文章
- boost:program_options
由于系统库getopt和getopt_long用起来不够直观,仔细看了下boost发现Boost.Program_options可以满足我的需求,它和getopt系列函数一样,可以抓起命令行参数,这里 ...
- program_options禁止命令行短参数
典型的 boost program_options的用法如下: #include <boost/program_options.hpp> using namespace boost::pr ...
- [C++Boost]程序参数项解析库Program_options使用指南
介绍 程序参数项(program options)是一系列name=value对,program_options 允许程序开发者获得通过命令行(command line)和配置文件(config fi ...
- boost之program_options库,解析命令行参数、读取配置文件
一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...
- 使用Boost program_options控制程序输入
简介 很多人使用界面来输入数据,本文的程序介绍如何使用Boost的program_options控制输入. 程序中使用了: 1. 短选项 2. 可以指定多个参数的选项 程序 #include < ...
- boost库中的 program_options
1.阅读rviz中的源码时在rviz/visualizer_app.cpp中遇到如下代码: po::options_description options; options.add_options() ...
- boost::program_options 解析命令行参数
源码: #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int a ...
- Boost命令行解释器的简单使用:Boost.Program_options
简介 如果使用比较多的命令行程序的话,对于命令行参数的输入肯定不会陌生,大部分的程序都是通过类似下面的形式进行输入的,比如熟悉的ls ls --all -l --color=auto 这里面包含了三种 ...
- C++ 系列:编译 boost
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
随机推荐
- CUDA 编程
作者:MingChaoSun 原文:https://blog.csdn.net/sunmc1204953974/article/details/51000970 一.CPU和GPU 上图是CPU与GP ...
- 关于Oracle字符集在dmp文件导入导出中的乱码影响
Oracle 在进行dmp备份和还原的时候,服务器端字符集和客户端字符集会对这个过程有较大影响,特别是数据表中存储了中文.存储过程中使用了中文编码(注释)的时候,如果没有处理好字符集的问题,在进行还原 ...
- Some elementary algorithms on discrete differential geometry(DDGSpring2016 Demos)
I studied the on-line course(http://brickisland.net/DDGSpring2016/) by myself, and here are the scre ...
- 京东饭粒捡漏V1.1.0
20180624 更新 V1.1.01.解决进程残留问题:2.加入急速下单模式: 功能介绍1.京东商城专用,支持饭粒模式下单,自己获得京豆返利 2.捡漏模式:帮助用户监控抢购商品,有库存的时候进行抢单 ...
- mysql binlog抽取某个表的数据
1.先把binlog文件转化为sql --base64-output=decode-rows -v > /var/mydiag.sql 2.抽取某一个表的数据 grep -B0 -A27 -w ...
- POI 生成、导出Excel(包含多个sheet)带 图片
1.导入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</ar ...
- ibatis.net 循环
if (oReqV[0]["tag"] != null && !string.IsNullOrEmpty(oReqV[0]["tag"].ToS ...
- 工作中拓展的加密解密传输方式. DES对称加密传输.
系统间通过xml传输, 不能采用明文, 就加密传输. 秘钥(真正有效的是前8位)存储于配置中. public static string EncryptStr(this string content, ...
- 生信基础知识【04】GO和pathway分析
非原创 参考资料: 一文掌握GO和pathway分析 - 生物信息学讨论版 -丁香园论坛http://www.dxy.cn/bbs/thread/34904124#34904124 GO富集 GO是G ...
- postgis创建空间数据库,导入shp数据
使用向数据库中导入数据