2013-04-07 21 views
5

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

這是無法顯示file1file2選項的說明。我當然可以將它們添加到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這個,希望以後向兼容的方式。

回答

1

這並不理想,但如何創建一個「虛擬」程序選項集,讓boost :: program_options格式化程序爲您設置幫助文本的格式,然後用快速替換刪除破折號?

然後輸出除options幫助文本以外的幫助文本。

事情是這樣的:

po::options_description dummy_options("Operands"); 
dummy_options.add_options() 
    ("file1", po::value<std::string>(), "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.") 
    ("file2", po::value<std::string>(), "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.") 
    ; 

std::stringstream s; 
s << dummy_options; 
std::string dummy_help_text = s.str(); 
boost::replace_all(dummy_help_text, "--", ""); 
boost::replace_all(dummy_help_text, "arg", "  "); 

std::cout << dummy_help_text << std::endl; 

輸出看起來是這樣的:

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. 

因爲,除其他事項外,列之間的間隔不會幫助輸出匹配這不是理想的其他options輸出。但對於快速而骯髒的東西來說,這基本上可行,但它可以。

無論如何,這是一種想法。

(我在Boost.Program_Options API中看不到任何東西,它可以讓你做到「正確」,https://stackoverflow.com/a/3621947/368896暗示這種東西不被支持,但現在已經有3年了)

+0

壞消息,不幸':(' – rubenvb 2013-04-25 09:38:11

相關問題