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/ 欢迎转载,请保留此版权声明. -- ...
随机推荐
- SolrJ的使用
SolrJ的使用 1.添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- linux kernel mini2440 start.S head-common.S 部分注释
内核版本:2.6.32.2(mini2440光盘源码) github地址:https://github.com/guanglun/mini2440_uboot_linux (for_len分支 htt ...
- mac brew install error
Error: No available formula with the name "pygame" ==> Searching for a previously delet ...
- python 多线程threading的学习一
1. import threading #引入线程模块 2 申明实例 t = threading.Thread(target = fun, args = (,)) 说明:参数target 是要运行的 ...
- jenkins 构建完毕后接着构建另外一个构建的方法
- Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- celery定时任务
from celery import Celery from datetime import timedelta app = Celery('gx', broker='redis://localhos ...
- boolean表达式与在if条件中的运用
1.boolean语句 boolean 类型的数据 有两个值 false 和true; 2.在if判断的条件语句中 例如: boolean arn =false 在if 判断的括号中可以表示为 !a ...
- PhysicalBasedRendering(一)物理篇
很多人对PBR的理解是存在偏差的,跳不出传统渲染模型的思维圈子,把它理解成一种模拟效果更为精确的算法公式,虽然在某种程度上是对的,但没有看到PBR的本质. PBR是对光在真实世界中与环境交互的一种近似 ...
- (转)Mysql哪些字段适合建立索引
工作中处理数据时,发现某个表的数据达近亿条,所以要为表建索引提高查询性能,以下两篇文章总结的很好,记录一下,以备后用. 数据库建立索引常用的规则如下: 1.表的主键.外键必须有索引: 2.数据量超过3 ...