Opened 13 years ago

Last modified 11 years ago

#3813 new Bugs

program_options::options_description.add() permits duplicate options

Reported by: Matthew Wesley <weslem-boosttrac@…> Owned by: Sascha Ochsenknecht
Milestone: Boost 1.42.0 Component: program_options
Version: Boost 1.41.0 Severity: Problem
Keywords: Cc:

Description

The documentations for options_description.add() says it will through duplicate_variable_error for duplicate short or long options. I have check versions 1.32.0 and 1.41.0, and both permit this code:

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

namespace po = boost::program_options;
 
int main()
{
    po::options_description opts;

    opts.add_options()
        ("help", "first  --help")
        ("help", "second --help")
        ;
    opts.add_options()
        ("alpha,a", "first  -a")
        ("apple,a", "second -a")
        ;
 

    std::cout << opts;

    return 0;
}

Producing this output:

  --help                first  --help
  --help                second --help
  -a [ --alpha ]        first  -a
  -a [ --apple ]        second -a

Change History (2)

comment:1 by Sascha Ochsenknecht, 13 years ago

Owner: changed from Vladimir Prus to Sascha Ochsenknecht

comment:2 by Ralph van Etten <ralph@…>, 11 years ago

This bug still exists in 1.48.0, the documentation says it will throw duplicate_variable_error but this exception is never thrown anywere.

I needed this functionality but I was wondering if it can be added to the library without breaking other code using program_options.

For instance, program_options/test/exception_test.cpp depends on add_options() not to throw an exception (even though the documentation says it will throw and exception as it adds the same option twice) to test the ambiguous_option exception.

It seems to me duplicate_variable_error and ambiguous_option are the same kind of error but thrown at different times. The first is (should be) thrown when the same option is added twice. The latter is thrown only after parsing/storing the command line and the option is actually used on the command line.

Is this behavior intentional?

I prefer the add_options() function to throw an exception (or even assert) when a duplicate option is added because IMHO it is an error which should be solved by the programmer before the program can actually run.

However, fixing this might break a lot of code so at least the documentation should be updated and the duplicate_variable_error should be removed.

I can create a patch for either solution, but I am not sure which one is preferred.

Note: See TracTickets for help on using tickets.