2013-04-07 32 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

相关问题