如何接受空值在boost :: program_options [英] How to accept empty value in boost::program_options

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

问题描述

我使用boost :: program_options库来处理命令行参数。
我需要通过-r选项接受一个文件名,如果它是空的(-r没有params)我需要使用stdin。

  desc.add_options()
(replay,r,boost :: program_options :: value< std :: string>(),bla bla bla)

在这种情况下,boost不会接受-r没有参数并抛出异常。
default_value()选项不起作用,即使用户没有给-r选项也会使库返回值。



任何想法如何

请使用 implicit_value 方法,例如 p>

  desc.add_options()
(replay,r,po :: value< std :: string& - > implicit_value(stdin),bla bla bla)

0或1个令牌,如果没有提供令牌,它将像提供'stdin'一样工作。当然,你可以选择任何其他隐式值 - 包括空字符串和' - '如mch建议。


I'm using boost::program_options library to process command line params. I need to accept a file name via -r option, in case if it is empty (-r given without params) I need to use stdin.

desc.add_options()
 ("replay,r", boost::program_options::value<std::string>(), "bla bla bla")

In this case boost wouldn't accept -r without params and throw an exception. default_value () option does not work as well as it would make library return value even if user didn't give -r option.

Any ideas how to work around?

解决方案

Please use the implicit_value method, e.g

desc.add_options()
 ("replay,r", po::value<std::string>()->implicit_value("stdin"), "bla bla bla")

This makes the option accept either 0 or 1 token, and if no tokens are provided, it will act as if 'stdin' was provided. Of course, you can pick any other implicit value -- including empty string and '-' as suggested by mch.

这篇关于如何接受空值在boost :: program_options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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