Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::program_options bug or feature?

Very simple example:



#include <string>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int argc, char* argv[])
{
    po::options_description recipients("Recipient(s)");
    recipients.add_options()
        ("csv",         po::value<std::string>(),     ""  )
        ("csv_name",    po::value<unsigned>(),        ""  )
    ;

    po::options_description cmdline_options;
    cmdline_options.add(recipients);

    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(cmdline_options).run(), vm);
    po::notify(vm);

    return 0;
}


And some tests:


>Test --csv test
in option 'csv_name': invalid option value

>Test --csv_name test
in option 'csv_name': invalid option value

>Test --csv_name 0

>Test --csv text
in option 'csv_name': invalid option value

>Test --csv 0

>Test --csv_name 0

>Test --csv_name 0 --csv text
multiple occurrences

Looks like that boost::program_option threats parameter "csv" as "csv_name".
Is it a feature or bug?

like image 917
Dmitriy Avatar asked Apr 07 '10 02:04

Dmitriy


2 Answers

Yes, this is a "feature" due to default options parsing style. Try with short options, like:

recipients.add_options()
    ("csv,c",      po::value<std::string>(), ""  )
    ("csv_name,C", po::value<unsigned>(),    ""  )
;

Or play with the basic_command_line_parser::style(int) method. I haven't tried this, so YMMV.

like image 89
Nikolai Fetissov Avatar answered Oct 04 '22 02:10

Nikolai Fetissov


I am afraid this is a bug. But, it should be fixed in 1.42 -- which version did you try with?

like image 35
Vladimir Prus Avatar answered Oct 04 '22 04:10

Vladimir Prus