boost program_options的"多次出现"例外

R S*_*R S 1 c++ boost boost-program-options

我在boost的program_options(版本1.42)上编写以下代码.这似乎是直截了当的,并且与本教程非常相似.但是,我收到"multiple_occurrences"错误.进一步调查发现它(可能)是引发它的"文件名"参数.

我给出的参数是:

3 1 test.txt 100
Run Code Online (Sandbox Code Playgroud)

我没有任何见解......任何帮助将不胜感激.

po::options_description common("Common options");

common.add_options()
    ("help", "produce help message")
    ("motif_size", po::value<int>(&motif_size), "Size of motif (subgraph)")
    ("prob", po::value<double>(&prob), "Probably to continue examining an edge")
    ("filename", po::value<string>(&input_filename), "Filename of the input graph")
    ("repeats", po::value<int>(&n_estimates), "Number of estimates")
    ;

po::options_description all;
all.add(common);

po::positional_options_description p;
p.add("motif_size", 0).add("prob", 1).add("filename", 2).add("repeats", 3);

po::variables_map vm;   
po::store(po::command_line_parser(argc, argv).
      options(all).positional(p).run(), vm);
po::notify(vm); 
Run Code Online (Sandbox Code Playgroud)

Sam*_*ler 6

编辑:

第二个参数po::positional_options_description::add最大计数,而不是位置.该位置隐含在您指定位置选项的顺序中.所以

p.add("motif_size", 0).add("prob", 1).add("filename", 2).add("repeats", 3);
Run Code Online (Sandbox Code Playgroud)

应该

p.add("motif_size", 1).add("prob", 1).add("filename", 1).add("repeats", 1);
Run Code Online (Sandbox Code Playgroud)

这是一个可编辑的片段

include <boost/program_options.hpp>

#include <iostream>
#include <string>

int
main(unsigned argc, char** argv)
{
    namespace po = boost::program_options;
    po::options_description common("Common options");

    common.add_options()
        ("help", "produce help message")
        ("motif_size", po::value<int>(), "Size of motif (subgraph)")
        ("prob", po::value<double>(), "Probably to continue examining an edge")
        ("filename", po::value<std::string>(), "Filename of the input graph")
        ("repeats", po::value<int>(), "Number of estimates")
        ;

    po::options_description all;
    all.add(common);

    po::positional_options_description p;
    p.add("motif_size", 1).add("prob", 1).add("filename", 1).add("repeats", 1);

    po::variables_map vm;
    try {
        po::store(po::command_line_parser(argc, argv).
                options(all).positional(p).run(), vm);
        po::notify(vm); 
    } catch ( const boost::program_options::error& e ) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

和示例调用.

macmini:~ samm$ g++ parse.cc -lboost_program_options
macmini:~ samm$ ./a.out 3 1 test.txt 100
macmini:~ samm$ 
Run Code Online (Sandbox Code Playgroud)

我原来的答案如下.


什么版本的program_options?我使用boost 1.39遇到同样的问题,为了解决这个问题,我最终使用了boost 1.42.

这是描述问题的故障的链接,如果您不想或不能升级您的升级副本,则可以应用补丁.要使用新功能,请执行以下操作

try {
    // argument parsing goes here
} catch ( const boost::program_options::multiple_occurrences& e ) {
    std::cerr << e.what() << " from option: " << e.get_option_name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)