如何使用boost :: program_options接受一个可选的标志? [英] How to use boost::program_options to accept an optional flag?

查看:205
本文介绍了如何使用boost :: program_options接受一个可选的标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个可选的标记,比如 -f / - 标志。由于这是一个标志,没有相关联的值。在我的code,我只需要知道标志是否被设置与否。什么是做到这一点使用boost :: program_options的正确方法?

I need to implement an optional flag, say -f/--flag. Since this is a flag, there is no value associated. In my code I only need to know whether the flag was set or not. What's the proper way to do this using boost::program_options?

推荐答案

一个方便的方式做,这是与 bool_switch 功能:

A convenient way to do this is with the bool_switch functionality:

bool flag = false;

namespace po = boost::program_options;

po::options_description desc("options");

desc.add_options()
  ("flag,f", po::bool_switch(&flag), "description");
po::variables_map vm;
//store & notify

if (flag) {
  // do stuff
}

这是比字符串手动检查(字符串只在整个定义中使用一次)更安全。

This is safer than manually checking for the string (string only used once in whole definition).

这篇关于如何使用boost :: program_options接受一个可选的标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆