【发布时间】:2013-04-07 13:43:23
【问题描述】:
我正在使用 Boost.program_options 来解析命令行以实现 POSIX 实用程序。举个简单的例子,以cmp.
现在我想要一个额外的参数--help,它显示了所有参数的描述,这在这种情况下很重要。我有:
po::options_description options("Options");
options.add_options()("help", "Show this help output.")
(",l", "(Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.")
(",s", "Write nothing for differing files; return exit status only.")
po::positional_options_description operands;
operands.add("file1", 1);//, "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.")
operands.add("file2", 1);//, "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.");
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).positional(operands).run(), vm);
po::notify(vm);
if(vm.count("help"))
{
std::cout << "cmp: compare two files\nUsage: cmp [ -l | -s ] file1 file2\n" << options;
return 0;
}
无法显示file1 和file2 选项的描述。我当然可以将它们添加到options,但这会添加至少两个不需要的参数[-]-file{1,2},这是我真的不想要的。我只想要--help 的这个输出(没有明显的硬编码):
cmp: compare two files
Usage: cmp [ -l | -s ] file1 file2
Options:
--help Show this help output.
-l (Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.
-s Write nothing for differing files; return exit status only.
Operands:
file1 A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.
file2 A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.
有没有什么方法可以在不破坏图书馆的情况下实现这一目标?我认为这是非常基本的东西,但我在任何tutorials 中都找不到它。
更新为了大家的利益,我为此提交了feature request,希望以向后兼容的方式。
【问题讨论】:
标签: c++ boost boost-program-options