如何在升压接受空值:: program_options [英] How to accept empty value in boost::program_options
问题描述
我使用boost :: program_options库来处理命令行PARAMS。
我需要通过-r选项接受一个文件名,在情况下,如果它是空的(没有PARAMS -r给出)我需要使用标准输入。
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")
在这种情况下,升压不会没有PARAMS接受-r并抛出一个异常。
DEFAULT_VALUE()选项不起作用,以及它将使图书馆返回值,即使用户没有给出-r选项。
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.
任何想法如何解决?
推荐答案
请使用 implicit_value
方法,例如
desc.add_options()
("replay,r", po::value<std::string>()->implicit_value("stdin"), "bla bla bla")
此使得选项接受0或1令牌,并且如果没有提供令牌,它将采取行动,如果'标准输入'被提供为。当然,你可以选择任何其他内含价值 - 包括空字符串和 - 。通过MCH建议
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.
这篇关于如何在升压接受空值:: program_options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!