Boost C++ Libraries: Ticket #7495: Support for boost::optional in boost::program_options https://svn.boost.org/trac10/ticket/7495 <p> It would be nice to allow support for boost::optional&lt;T&gt; arguments in program_options, so you can write following: </p> <div class="wikipage" style="font-size: 100%"><div class="wiki-code"><div class="code"><pre><span class="n">optional</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">optionalArg</span><span class="p">;</span> <span class="n">options_description</span> <span class="nf">desc</span><span class="p">(</span><span class="s">&quot;allowed options&quot;</span><span class="p">);</span> <span class="n">desc</span><span class="p">.</span><span class="n">add_options</span><span class="p">()</span> <span class="p">(</span><span class="s">&quot;optional&quot;</span><span class="p">,</span> <span class="n">value</span><span class="p">(</span><span class="o">&amp;</span><span class="n">optionalArg</span><span class="p">),</span> <span class="s">&quot;specify optional argument&quot;</span><span class="p">);</span> </pre></div></div></div><p> And shouldn't bother with argument checking using variables_map. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7495 Trac 1.4.3 anonymous Wed, 06 Aug 2014 10:58:40 GMT <link>https://svn.boost.org/trac10/ticket/7495#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7495#comment:1</guid> <description> <p> I am very interested in this as well! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 02 Oct 2015 07:36:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7495#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7495#comment:2</guid> <description> <p> +1 </p> </description> <category>Ticket</category> </item> <item> <author>fakong@…</author> <pubDate>Mon, 19 Oct 2015 02:44:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7495#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7495#comment:3</guid> <description> <p> you only need to include this piece of code </p> <p> template&lt;class T&gt; std::istream&amp; operator&gt;&gt;(std::istream&amp; in, boost::optional&lt;T&gt;&amp; obj) { </p> <blockquote> <p> T value; in &gt;&gt; value; obj = value; return in; </p> </blockquote> <p> } </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Mon, 19 Oct 2015 05:31:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7495#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7495#comment:4</guid> <description> <p> It would seem such streaming support should be done in the optional library, not in program_options? </p> </description> <category>Ticket</category> </item> <item> <author>Edward Catmur <ed@…></author> <pubDate>Fri, 30 Oct 2015 10:44:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7495#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7495#comment:5</guid> <description> <p> Implementing optional support via streaming would have unwanted side effects. The correct place to implement optional support is via the validate customization point: </p> <pre class="wiki">namespace boost { template&lt;class T&gt; void validate(boost::any&amp; v, std::vector&lt;std::string&gt; const&amp; values, boost::optional&lt;T&gt;* typeTag, int) { if (!values.empty()) { boost::any a; using namespace boost::program_options; validate(a, values, (T*)0, 0); v = boost::any(boost::optional&lt;T&gt;(boost::any_cast&lt;T&gt;(a))); } } } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Fri, 30 Oct 2015 10:48:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7495#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7495#comment:6</guid> <description> <p> Pull request will be welcome. Is there a way to implement this with a forward declaration, so that program_options do not have to depend on boost::optional. </p> </description> <category>Ticket</category> </item> <item> <author>Edward Catmur <ed@…></author> <pubDate>Tue, 03 Nov 2015 19:23:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7495#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7495#comment:7</guid> <description> <p> Submitted pull request: <a class="ext-link" href="https://github.com/boostorg/program_options/pull/18"><span class="icon">​</span>https://github.com/boostorg/program_options/pull/18</a> </p> <p> Uses forward declaration to avoid dependency (except in tests). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Wed, 21 Dec 2016 10:47:41 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7495#comment:8 https://svn.boost.org/trac10/ticket/7495#comment:8 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket