gpt4 book ai didi

c++ - boost::program_options 和位置参数的问题

转载 作者:行者123 更新时间:2023-11-30 03:52:40 28 4
gpt4 key购买 nike

海湾合作委员会 4.7.2/ boost 1.58.0

我正在尝试看起来像这样的代码,几乎完全取自文档中的示例:

namespace po = boost::program_options;

po::options_description desc("Allowed options");

desc.add_options()
("help","produce help message")
;

po::positional_options_description pos_desc;

pos_desc.add("input-file",-1);

po::variables_map vm;

// The following line throws an std::logic_error
// what() - error_with_option_name::m_option_style can only be one of
// [0, allow_dash_for_short, allow_slash_for_short, allow_long_disguise
// or allow_long]
po::store(po::command_line_parser(argc,argv).options(desc)
.positional(pos_desc)
.run(),
vm);

...

logic_error 异常在注释指示的行抛出,当我执行应用程序时:

myapp filename1

它显示了在没有(位置)参数的情况下运行时的用法。为什么在使用位置命令行参数时抛出?

最佳答案

您需要将“input-file”参数添加到desc,然后添加到pos_desc,而不仅仅是后者。这是我通常做的一个例子。

namespace po = boost::program_options;

string fin_name;

try {
po::options_description all_opt("Options");
all_opt.add_options()
("help,h", "produce help message")
("input,i", po::value<string>(&fin_name),
"input files with histograms")
;

po::positional_options_description pos;
pos.add("input",-1);

po::variables_map vm;
po::store(po::command_line_parser(argc, argv)
.options(all_opt).positional(pos).run(), vm);
if (argc == 1 || vm.count("help")) {
cout << all_opt << endl;
return 0;
}
po::notify(vm);
}
catch(exception& e) {
cerr << "\033[31mError: " << e.what() <<"\033[0m"<< endl;
return 1;
}

关于c++ - boost::program_options 和位置参数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30538857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com