gpt4 book ai didi

c++ - 如何使用 boost::progam_option 和 boost::command_line_Parser 验证文件扩展名

转载 作者:行者123 更新时间:2023-11-30 05:37:01 24 4
gpt4 key购买 nike

我正在编写一个使用 Boost 的程序选项库的程序,我无法使用 boost::program 选项验证文件扩展名:

 po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("diff,d", po::value< std::string >(),"Specify the .xyz file, name of the .xyz to create")**.xyz file I want to validate,while givin input as comman line **
;

po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);

最佳答案

好的,你需要实现validate

您可以使用标签类型,这样您就可以通过Argument Dependent Lookup 关联您的validate :

Live On Coliru

#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>
#include <vector>
#include <iostream>

namespace tag {
struct xyz_file {};

bool validate(boost::any& v, std::vector<std::string> const& ss, xyz_file*, int) {
if (ss.size() == 1 && boost::algorithm::iends_with(ss.front(), ".xyz"))
{
v = ss.front();
return true;
}
return false;
}
}

namespace po = boost::program_options;

int main(int ac, char** av) {
po::options_description desc("Allowed options");
desc.add_options()
("diff,d", po::value<tag::xyz_file>(), "xyz files only")
;

po::variables_map vm;

po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);

if (!vm["diff"].empty())
std::cout << "Parsed: " << vm["diff"].as<std::string>() << "\n";
else
std::cout << desc;
}

关于c++ - 如何使用 boost::progam_option 和 boost::command_line_Parser 验证文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33383822/

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