“多次出现” boost program_options 的例外

发布于 2024-09-12 01:27:18 字数 1008 浏览 11 评论 0原文

我正在 boost 的program_options(版本1.42)上编写以下代码。这看起来很简单,并且与教程中的内容几乎相同。但是,我收到“multiple_occurrences”错误。进一步调查发现(可能)是“文件名”参数引发了它。

我给出的参数是:

3 1 test.txt 100

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

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); 

I am writing the following code on boost's program_options (version 1.42). This seems straight-forward and taken pretty much as is from the tutorial. However, I get a "multiple_occurrences" error. Further investigation discovers that it's (probably) the "filename" parameter that raises it.

The parameters I am giving are:

3 1 test.txt 100

I have no insight to it whatsoever.. any help will be appreciated.

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); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-09-19 01:27:18

编辑

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

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

应该是

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

这是一个可编译的片段

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;
}

和示例调用。

macmini:~ samm$ g++ parse.cc -lboost_program_options
macmini:~ samm$ ./a.out 3 1 test.txt 100
macmini:~ samm$ 

我原来的答案如下。


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

以下是描述问题的 ticket 的链接,以及适用于您的补丁不想或无法升级您的提升副本。要使用新功能,请执行以下操作

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;
}

EDIT:

the second parameter to po::positional_options_description::add is the max count, not the position. The position is implied in the order you specify the positional options. So

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

should be

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

Here's a compilable snippet

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;
}

and sample invocation.

macmini:~ samm$ g++ parse.cc -lboost_program_options
macmini:~ samm$ ./a.out 3 1 test.txt 100
macmini:~ samm$ 

My original answer is below.


What version of program_options? I had the same problem using boost 1.39, to solve it I ended up using boost 1.42.

Here's a link to the ticket describing the problem, and a patch to apply if you don't want to or can't upgrade your copy of boost. To use the new functionality, do something like this

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文