Search code examples
c++boostboost-program-options

boost program_option case insensitive parsing


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

Solution

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