Boost C++ Libraries: Ticket #3813: program_options::options_description.add() permits duplicate options https://svn.boost.org/trac10/ticket/3813 <p> 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: </p> <pre class="wiki">#include &lt;boost/program_options.hpp&gt; #include &lt;iostream&gt; 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 &lt;&lt; opts; return 0; } </pre><p> Producing this output: </p> <pre class="wiki"> --help first --help --help second --help -a [ --alpha ] first -a -a [ --apple ] second -a </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3813 Trac 1.4.3 Sascha Ochsenknecht Mon, 04 Jan 2010 11:05:23 GMT owner changed https://svn.boost.org/trac10/ticket/3813#comment:1 https://svn.boost.org/trac10/ticket/3813#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Vladimir Prus</span> to <span class="trac-author">Sascha Ochsenknecht</span> </li> </ul> Ticket Ralph van Etten <ralph@…> Fri, 16 Dec 2011 09:23:23 GMT <link>https://svn.boost.org/trac10/ticket/3813#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3813#comment:2</guid> <description> <p> This bug still exists in 1.48.0, the documentation says it will throw duplicate_variable_error but this exception is never thrown anywere. </p> <p> I needed this functionality but I was wondering if it can be added to the library without breaking other code using program_options. </p> <p> 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. </p> <p> 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 <strong>and</strong> the option is actually used on the command line. </p> <p> Is this behavior intentional? </p> <p> 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. </p> <p> 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. </p> <p> I can create a patch for either solution, but I am not sure which one is preferred. </p> </description> <category>Ticket</category> </item> </channel> </rss>