我正在使用Boost.program_options来解析命令行以实现POSIX实用程序。举一个简单的例子,采取cmp
。如何在--help输出中显示命令行操作数描述
现在我想有一个额外的参数--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中找到它。
UPDATE为了大家的利益,我提交了一个feature request这个,希望以后向兼容的方式。
坏消息,不幸':(' – rubenvb 2013-04-25 09:38:11