【发布时间】:2013-11-05 17:24:14
【问题描述】:
我需要从boost::program_options::option_description 类中获取默认值。
我检查了源代码,看起来它以std::string 和boost::any 存储默认值,但它存储在私有m_default_as_text 中,因此我无法从那里提取此信息。
我能得到的只是这样的格式化参数
arg (=10)
但我只想得到 10 个。
我也可以通过调用value_semantic::apply_default方法获得默认值boost::any
boost::any default_value;
opt_ptr->semantic()->apply_default(default_value)
但是当我迭代 option_description 集合时,我不知道要执行 boost::any_cast 的确切类型,我只想打印它。
更新
namespace po = boost::program_options;
po::options_description descr("options");
descr.add_options()
("help,h", "produce help message")
("a", po::value<int>()->default_value(42));
for (auto opt: descr.options())
{
std::cout << opt->format_parameter() << std::endl;
}
在这里打印
arg (=42)
我想在没有类型知识的情况下获得 42 作为字符串。
有什么办法吗?
【问题讨论】:
-
我不清楚您要做什么。你能在问题中添加更多的伪代码吗?
-
当然。更新了问题。
-
完全没有想法?作为一种解决方法,我现在正在解析 arg(=42),但我更喜欢更好的方法。
标签: c++ boost boost-program-options