Boost C++ Libraries: Ticket #1132: zero_tokens doesn't work https://svn.boost.org/trac10/ticket/1132 <p> In the documentation for program_options, under the "Library Overview"/"Syntactic Information", the documentation appears to be incorrect. </p> <p> For the following option description: </p> <pre class="wiki"> ("verbose", value&lt;string&gt;()-&gt;zero_tokens(), "verbosity level") </pre><p> It states that: </p> <blockquote> <p> "the user may either provide a single token for the value, or no token at all." </p> </blockquote> <p> I wrote a test program based on the options given in this section of the manual, and I get different behavior for the 'verbose' option. In fact, the actual behavior is almost intuitive: the option is unusable. No matter what you do, its an error. Seems natural since you're saying the option has a value associated with it, yet no tokens may be given to specify the value. </p> <p> It also turns out that 'multitoken()' does not allow you to pass multiple tokens, but it does raise an exception if you specify anything after the first value, i.e.: </p> <pre class="wiki">./a.out --email blah@blah me@somewhere exception: in option 'email': multiple values not allowed ./a.out --email blah@blah --help exception: in option 'email': multiple values not allowed </pre><p> Here's my test program: </p> <pre class="wiki">#include &lt;boost/program_options.hpp&gt; #include &lt;string&gt; #include &lt;iostream&gt; using namespace boost::program_options; using namespace std; int main(int argc,char *argv[]) { options_description desc; desc.add_options() ("help", "produce help message") ("compression", value&lt;string&gt;(), "compression level") ("verbose", value&lt;string&gt;()-&gt;zero_tokens(), "verbosity level") ("email", value&lt;string&gt;()-&gt;multitoken(), "email to send to") ; variables_map vm; try { store(parse_command_line(argc, argv, desc), vm); notify(vm); } catch (exception &amp;e) { cout &lt;&lt; "exception: " &lt;&lt; e.what() &lt;&lt; endl; return -1; } if (vm.count("help")) cout &lt;&lt; desc &lt;&lt; endl; if (vm.count("compression")) cout &lt;&lt; "compression " &lt;&lt; vm["compression"].as&lt;string&gt;() &lt;&lt; endl; if (vm.count("verbose")) cout &lt;&lt; "verbose " &lt;&lt; vm["verbose"].as&lt;string&gt;() &lt;&lt; endl; if (vm.count("email")) cout &lt;&lt; "email " &lt;&lt; vm["email"].as&lt;string&gt;() &lt;&lt; endl; return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1132 Trac 1.4.3 crazykriz@… Wed, 24 Oct 2007 15:32:10 GMT <link>https://svn.boost.org/trac10/ticket/1132#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:1</guid> <description> <p> Can (still) verify this bug who seems to be rather old, because even in Boost 1.31.x I couldn't use the zero_tokens() option. I wrote an email to Vladimir Prus but never got an answer. </p> <p> I hope this mess will be fixed in near future. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Wed, 24 Oct 2007 15:48:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1132#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:2</guid> <description> <p> For the record, using words like "mess" is not likely to get any fix sooner :-P </p> </description> <category>Ticket</category> </item> <item> <author>crazykriz@…</author> <pubDate>Thu, 25 Oct 2007 15:04:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1132#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/1132#comment:2" title="Comment 2">vladimir_prus</a>: </p> <blockquote class="citation"> <p> For the record, using words like "mess" is not likely to get any fix sooner :-P </p> </blockquote> <p> Don't mind, "mess" wasn't related to your work (which is excellent), but related to the situation ;-) But, do you know what's going wrong with zero_tokens() and multitokens()? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Tue, 25 Mar 2008 16:19:01 GMT</pubDate> <title>type changed https://svn.boost.org/trac10/ticket/1132#comment:4 https://svn.boost.org/trac10/ticket/1132#comment:4 <ul> <li><strong>type</strong> <span class="trac-field-old">Support Requests</span> → <span class="trac-field-new">Bugs</span> </li> </ul> Ticket Vladimir Prus Sat, 17 May 2008 07:28:33 GMT summary, milestone changed https://svn.boost.org/trac10/ticket/1132#comment:5 https://svn.boost.org/trac10/ticket/1132#comment:5 <ul> <li><strong>summary</strong> <span class="trac-field-old">program_options documentation error.</span> → <span class="trac-field-new">zero_tokens clarification</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.36.0</span> </li> </ul> Ticket grywacz@… Sat, 17 Jan 2009 21:11:03 GMT <link>https://svn.boost.org/trac10/ticket/1132#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:6</guid> <description> <p> The bug (at least for zero_tokens) is still there - verified with 1.34 and 1.37. </p> </description> <category>Ticket</category> </item> <item> <author>grywacz@…</author> <pubDate>Sat, 17 Jan 2009 21:18:20 GMT</pubDate> <title>summary changed https://svn.boost.org/trac10/ticket/1132#comment:7 https://svn.boost.org/trac10/ticket/1132#comment:7 <ul> <li><strong>summary</strong> <span class="trac-field-old">zero_tokens clarification</span> → <span class="trac-field-new">zero_tokens doesn't work</span> </li> </ul> <p> The simplest program to test with: </p> <p> #include &lt;boost/program_options.hpp&gt; #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; namespace po = boost::program_options; int main(int argc, char *argv[]) { </p> <blockquote> <p> po::options_description desc("test"); desc.add_options() </p> <blockquote> <p> ("server", po::value&lt;std::string&gt;()-&gt;zero_tokens()); </p> </blockquote> </blockquote> <p> </p> <blockquote> <p> po::variables_map vm; </p> </blockquote> <p> </p> <blockquote> <p> po::store(po::command_line_parser(argc, argv).options(desc).run(), vm); </p> </blockquote> <p> } </p> Ticket anonymous Thu, 08 Oct 2009 12:41:05 GMT <link>https://svn.boost.org/trac10/ticket/1132#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:8</guid> <description> <p> The problem with multitoken is still there in Boost 1.40 :-( </p> <pre class="wiki">./a.out --email blah@blah me@somewhere exception: in option 'email': multiple values not allowed </pre><p> I could use value&lt;vector&lt;string&gt; &gt;(), but this implies that the option can occur more than one time on command line which I don't want allow in my case. </p> <p> zero_token() also broken, I always get: </p> <pre class="wiki">exception: in option 'verbose': at least one value required </pre> </description> <category>Ticket</category> </item> <item> <author>s.ochsenknecht@…</author> <pubDate>Wed, 04 Nov 2009 12:41:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1132#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:9</guid> <description> <p> I looked a bit deeper to the zero_token() issue. I found out that there is implicit_value(...) function which can be used for this feature. If an option occurs without any further tokens and this option is allowed to stay without any arguments, then this implicit value is assign: </p> <pre class="wiki"> options_description desc; desc.add_options() ("verbose", value&lt; int &gt;()-&gt;implicit_value(1), "verbosity level") ; </pre><p> Anyhow documentation has to be updated and maybe the zero_tokens() function needs to be removed. The multitoken() issue is still open. </p> <p> Please comment. Thanks. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Thu, 05 Nov 2009 09:23:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1132#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:10</guid> <description> <p> Another way how zero_tokens is used to good effect is the 'bool_switch' function: </p> <blockquote> <p> BOOST_PROGRAM_OPTIONS_DECL typed_value&lt;bool&gt;* bool_switch(bool* v) { </p> <blockquote> <p> typed_value&lt;bool&gt;* r = new typed_value&lt;bool&gt;(v); r-&gt;default_value(0); r-&gt;zero_tokens(); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> return r; </p> </blockquote> <p> } </p> </blockquote> <p> This, together with validator for the bool types, allows to make --whatever be interpreted as 'true' value for 'whatever' option. Note this code: </p> <blockquote> <p> BOOST_PROGRAM_OPTIONS_DECL void validate(any&amp; v, const vector&lt;string&gt;&amp; xs, </p> <blockquote> <p> bool*, int) </p> </blockquote> <p> { </p> <blockquote> <p> check_first_occurrence(v); string s(get_single_string(xs, true)); </p> </blockquote> </blockquote> <p> The 'true' passed to 'get_single_string' means that if there are no tokens, the empty string should be returned -- which is then interpreted as true value. </p> <p> Therefore, 'zero_tokens' makes sense exclusively when the validator for the type handles empty string or 'implied_value' is also specified. Seems like purely documentation bug to me, then. </p> </description> <category>Ticket</category> </item> <item> <author>calum.mitchell@…</author> <pubDate>Wed, 29 Dec 2010 20:38:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1132#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1132#comment:11</guid> <description> <p> I ran into this problem when reading the documentation for 1.45.0 and trying to compile/run the example code. </p> <p> The resolution appears to be: </p> <p> desc.add_options() </p> <blockquote> <p> ("help", "produce help message")<br /> ("compression", value&lt;string&gt;(), "compression level")<br /> ("verbose", value&lt;string&gt;()-&gt;implicit_value("0"), "verbosity level")<br /> ("email", value&lt;vector&lt;string&gt;&gt;()-&gt;multitoken(), "email to send to")<br /> ; </p> </blockquote> <p> As mentioned in an earlier comment, the use of value&lt;vector&lt;string&gt;&gt;() for a multitoken() option means that the option can occur more than one time on the command line. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Sat, 08 Jan 2011 11:45:55 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/1132#comment:12 https://svn.boost.org/trac10/ticket/1132#comment:12 <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> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/67774" title="Clarify docs for 'zero_token'. Fixes #1132. ">[67774]</a>) Clarify docs for 'zero_token'. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/1132" title="#1132: Bugs: zero_tokens doesn't work (closed: fixed)">#1132</a>. </p> Ticket