我正在使用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