Boost C++ Libraries: Ticket #2982: Required Arguments https://svn.boost.org/trac10/ticket/2982 <p> If I have a list of 10 parameters, and all are required, </p> <p> I end up with a giant block of these: </p> <p> if(!vm.count("input")) </p> <blockquote> <p> { </p> <blockquote> <p> std::cout &lt;&lt; "Input was not set." &lt;&lt; std::endl; exit(-1); </p> </blockquote> <p> } </p> </blockquote> <p> It would be nice to do something like: desc.add_options() </p> <blockquote> <p> ("input", po::value&lt;std::string&gt;(&amp;<a class="missing wiki">InputFile</a>)-&gt;required(), "Set input file.") </p> </blockquote> <p> That will perform that check automatically. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2982 Trac 1.4.3 s.ochsenknecht@… Thu, 22 Oct 2009 17:29:09 GMT attachment set https://svn.boost.org/trac10/ticket/2982 https://svn.boost.org/trac10/ticket/2982 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">patch_ticket2982.diff</span> </li> </ul> Ticket s.ochsenknecht@… Thu, 22 Oct 2009 17:38:39 GMT <link>https://svn.boost.org/trac10/ticket/2982#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:1</guid> <description> <p> I find it useful, so I added a patch which implements the required() flag as described. It throws an exception from parsing if a required option isn't present. </p> <p> This is my test case: </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 opts; opts.add_options() ("cfgfile,c", value&lt;std::string&gt;()-&gt;required(), "the configfile") ("fritz,f", value&lt;std::string&gt;()-&gt;default_value("/other/file"), "the output file") ; variables_map vm; try { store(parse_command_line(argc, argv, opts), vm); notify(vm); } catch (required_option&amp; e) { // new exception type cout &lt;&lt; "required option: " &lt;&lt; e.what() &lt;&lt; endl; return -1; } catch (...) { cout &lt;&lt; "other exception: " &lt;&lt; endl; return -1; } } </pre><p> Does it make sense? Is it useful? </p> <p> I'm not a Boost maintainer, so I won't change the state of this ticket :-). </p> <p> Thanks </p> </description> <category>Ticket</category> </item> <item> <author>Sascha Ochsenknecht <s.ochsenknecht@…></author> <pubDate>Fri, 23 Oct 2009 06:51:30 GMT</pubDate> <title>cc set https://svn.boost.org/trac10/ticket/2982#comment:2 https://svn.boost.org/trac10/ticket/2982#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">s.ochsenknecht@…</span> added </li> </ul> Ticket Vladimir Prus Sun, 01 Nov 2009 10:58:00 GMT <link>https://svn.boost.org/trac10/ticket/2982#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:3</guid> <description> <p> Sascha, I am not sure if the location of the check is right. I'd suspect that user wants the option to be always specified, but does not necessary care where it comes from. Maybe, a check inside 'notify' would be better? </p> </description> <category>Ticket</category> </item> <item> <author>s.ochsenknecht@…</author> <pubDate>Sun, 01 Nov 2009 13:21:48 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2982#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:4</guid> <description> <p> I was also not very sure if this is right place. Since I'm still not very familiar with the internals of this library. I just cam to that place by tracing with the debugger ... </p> <p> I'm going to check your proposal with 'notify' within the next days. Keep you informed. Thanks for reviewing. </p> </description> <category>Ticket</category> </item> <item> <author>s.ochsenknecht@…</author> <pubDate>Wed, 04 Nov 2009 10:10:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2982#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:5</guid> <description> <p> I just checked if it is possible to place the check in notify(), but there is one problem. The notify function just gets the variables_map parameter and not the options_description's which have the information about required options. </p> <p> I have following solutions in mind: </p> <p> 1) in store() we store any required options with an empty value (if not occur) in the variables_map. In notify we then check for empty() and throw exception if empty() and required(). Any side effects? My preferred solution. </p> <p> 2) we pass options_description's to notify() to be able to perform the cross check, but this breaks the interface (or we overload notify, but then its up to the user to choose the correct notify() ... :-( </p> <p> 3) we store a reference/pointer to options_description's in variables_map. :-( </p> <p> ... </p> <p> Please comment. Thanks </p> </description> <category>Ticket</category> </item> <item> <author>s.ochsenknecht@…</author> <pubDate>Wed, 04 Nov 2009 12:01:26 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2982 https://svn.boost.org/trac10/ticket/2982 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">patch_ticket2982_2.diff</span> </li> </ul> <p> patch </p> Ticket s.ochsenknecht@… Wed, 04 Nov 2009 12:02:24 GMT attachment set https://svn.boost.org/trac10/ticket/2982 https://svn.boost.org/trac10/ticket/2982 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">required.cpp</span> </li> </ul> <p> testcase </p> Ticket s.ochsenknecht@… Wed, 04 Nov 2009 12:08:04 GMT <link>https://svn.boost.org/trac10/ticket/2982#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:6</guid> <description> <p> I implemented option (1), see patch_ticket2982_2.diff and the attached testcase required.cpp. </p> <p> store() now adds empty variable_value's to the variable_map. I also override the count() method of the underlying std::map in that way that the empty variable_values are not counted. </p> <p> Hm, this can have some side effects when operator[] is used: const variable_value&amp; operator[](const std::string&amp; name) const </p> <p> But I think the user has to check for !empty() anyhow ..!? </p> <p> Please comment. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Mon, 09 Nov 2009 16:15:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2982#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:7</guid> <description> <p> Sascha, </p> <p> thanks for the patch. I am out of time for today, but will review it as soon as possible. </p> <p> Thanks, Volodya </p> </description> <category>Ticket</category> </item> <item> <author>Sascha Ochsenknecht <s.ochsenknecht@…></author> <pubDate>Tue, 17 Nov 2009 12:51:06 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2982#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2982#comment:7" title="Comment 7">vladimir_prus</a>: </p> <blockquote class="citation"> <p> Sascha, </p> <p> thanks for the patch. I am out of time for today, but will review it as soon as possible. </p> </blockquote> <p> Probably storing the required options together with the parsed options in the same map isn't a good idea. Maybe a new member variable (in variables_map) containing the required options would be better. I'll modify the patch again within the next days. </p> <p> Thanks, Sascha </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Sascha Ochsenknecht</dc:creator> <pubDate>Thu, 26 Nov 2009 11:05:23 GMT</pubDate> <title>owner, status, milestone changed https://svn.boost.org/trac10/ticket/2982#comment:9 https://svn.boost.org/trac10/ticket/2982#comment:9 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Vladimir Prus</span> to <span class="trac-author">Sascha Ochsenknecht</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.39.0</span> → <span class="trac-field-new">Boost 1.42.0</span> </li> </ul> Ticket Sascha Ochsenknecht Thu, 10 Dec 2009 08:46:47 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2982#comment:10 https://svn.boost.org/trac10/ticket/2982#comment:10 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</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/58263" title="Enhancement to flag options as required, Fixes #2982">[58263]</a>) Enhancement to flag options as required, Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2982" title="#2982: Feature Requests: Required Arguments (closed: fixed)">#2982</a> </p> Ticket anonymous Thu, 10 Dec 2009 08:52:00 GMT <link>https://svn.boost.org/trac10/ticket/2982#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:11</guid> <description> <p> I modified the attached patch in that way that required options are stored as an additional member in variables_map. The check if all required options occur is done in notify(). </p> <p> Cheers, Sascha </p> </description> <category>Ticket</category> </item> <item> <author>ihorfg@…</author> <pubDate>Wed, 07 Sep 2016 15:24:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2982#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2982#comment:12</guid> <description> <p> How to use the required flag for arguments that could be set from both argument list and configuration file? Parsing command line in such a case throws an exception while the missing required arguments can exist in configuration file. </p> </description> <category>Ticket</category> </item> </channel> </rss>