使用boost::program_options打印普通参数和位置参数的帮助
Print help for both normal and positional args with boost::program_options
当你使用Boost库program_options
时,很容易为你的程序打印帮助:
boost::program_options::variables_map options;
boost::program_options::options_description optionsDesc;
boost::program_options::positional_options_description positionalOptionsDesc;
//...
if(options.count("help"))
{
cerr << optionsDesc << endl;
}
但是如何将positional_options_description
中的选项添加到帮助消息中呢?在本教程中,我可以看到这种设置的输出,在本节的末尾:
http://www.boost.org/doc/libs/1_52_0/doc/html/program_options/tutorial.html id2607297
选项input-file
在帮助中打印,它是位置的。但我看不见代码。是否有一种内置的方式来打印它,就像options_description
一样,或者你必须手动完成?显然<<
不能用于positional_options_description
,编译错误是:
error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
注意,流式描述只输出选项。它不打印程序的名称或程序功能的实际描述。您应该手动打印任何位置参数,作为输出消息的一部分:
不是if (vm.count("help")) {
cout << "Usage: options_description [options]n";
cout << desc;
return 0;
}
你可以很容易地说
if (vm.count("help")) {
cout << "Usage: " << argv[0] << " [options] <description of positional 1> <description of positional 2> ...n";
cout << desc;
return 0;
}
查看boost::program_options::positional_options_description.name_for_position(i)
错误信息是无关的,我忘记了cpp11
我是这样自动打印位置选项的:
void printUsage(const std::string &argv0)
{
std::ostream &os = std::cout;
os << "Usage:" << std::endl;
// print only basename of argv[0]
boost::filesystem::path p(argv0);
os << " " << p.filename().string();
os << " [options]";
std::string last = "";
int rep = 0;
for(int i = 0; i < positional_options_description_.max_total_count(); i++)
{
const std::string &n = positional_options_description_.name_for_position(i);
if(n == last)
{
if(!rep) os << " ...";
if(rep++ > 1000) break;
}
else
{
os << " " << n;
last = n;
rep = 0;
}
}
os << std::endl << std::endl;
os << options_description_ << std::endl;
}
检查重复参数名称的逻辑只需要当你有重复的选项,可以重复无限次,即count等于-1,否则你可以简化一点,例如将if... else if ...
替换为os << " " << n;
。
在当前(1.68)版本的boost中,没有办法告诉选项描述是否是位置的,所以没有办法改进帮助,例如,从打印中排除位置选项。
相关文章:
- boost::p rogram_options 在指定意外的位置参数时不报告任何错误
- 提升::P设置选项无法使所有参数都处于位置状态
- 处于默认参数位置的 Lambda 无法访问好友成员。这是编译器错误吗?
- ClangTool 如何获取变量声明中模板参数的位置?
- 了解 glm::ortho() 的参数如何影响投影后的顶点位置
- 参数中的与号位置
- 为什么后缀失败并且前缀在传递迭代器作为参数并在尾部位置递归时工作正常
- 应删除传递参数时创建对象的位置
- 参数包不在最后位置的部分类模板专用化
- 让我们C++推断定义参数的类/命名空间的位置
- g++ 对象文件参数位置很重要,并给出无法正常编译的不同结果
- gcc 的 std::bind 在源中的哪个位置将参数复制到数据结构中?
- STL中set的insert函数中为什么存在位置参数
- 使用Args.中的n个参数..从位置m开始
- C/C++:函数参数列表中的"const"位置
- 在模板上下文中通过引用传递的参数的"const"限定符的位置
- 在C++函数中的适当位置处理参数
- 按位置查找未标记的模板选项/参数/参数
- Boost:无法识别的位置参数选项
- 为什么我总是在传递位置参数时得到默认值