views:

324

answers:

2

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?

+4  A: 

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.

Nikolai N Fetissov
I are right. parser.style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing); helps me. Thanks.
Dmitriy
+2  A: 

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

Vladimir Prus
I'm using Version 1.42.0. As I already said parser.style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing); helps me.
Dmitriy
Hi Vladimir. Nice to see the author answering for the library :) What do you think the bug is/was - the default parsing style?
Nikolai N Fetissov
No, the default style is fine. However, if you have two options, one named "csv" and another named "csv_name", and the command line has "--csv", then it's reasonable to prefer full match to an approximate match. I believe the fix is this: https://svn.boost.org/trac/boost/changeset/59744
Vladimir Prus
Hi Vladimir. Boost 1.43 released, but looks like the bugfix doesn't included. Could you confirm that the bug is not fixed in boost 1.43, please?
Dmitriy
Unfortunately, it seems like I have failed to merge this fix to the trunk -- so you get to apply it manually from the changeset link I gave above. I am really sorry!
Vladimir Prus