boost program_option case insensitive parsing

家住魔仙堡 提交于 2019-12-11 00:29:14

问题


Has anyone worked out how to get boost program options to parse case insensitive argument lists

In the boost documentation, it appears that it is supported. See http://www.boost.org/doc/libs/1_53_0/boost/program_options/cmdline.hpp

Namely, setting the style_t enum flag such as long_case_insensitive. However, I'm not sure how to do it. Eg how would you get the following code snippet to accept --Help or --help or --HELP

    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ("compression", po::value<double>(), "set compression level")
    ;

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

    if (vm.count("help")) {
        cout << desc << "\n";
        return 0;
    }

回答1:


You can modify the style when you call store. I believe this should work for you:

namespace po_style = boost::program_options::command_line_style;

po::variables_map vm;        
po::store(po::command_line_parser(argc, argv).options(desc)
          .style(po_style::unix_style|po_style::case_insensitive).run(), vm);
po::notify(vm);    


来源:https://stackoverflow.com/questions/15419665/boost-program-option-case-insensitive-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!