提升program_options开/关标志

时间:2015-10-16 13:33:43

标签: c++ boost boost-program-options

使用bool_switch,我可以编写一个命令行选项来打开标志:

bool flag;

po::options_description options;
options.add_options()
    ("on", po::bool_switch(&flag)->default_value(false))
    ;

现在./a.out flag==false./a.out --onflag==true options.add_options() ("on", po::bool_switch(&flag)->default_value(false)) ("off", po::anti_bool_switch(&flag)) // ???? ; 。但是,为了明确,我还想添加一个命令行选项来关闭标志 off 。类似的东西:

anti_bool_switch

有没有办法在program_options库中执行<div class="col-xs-12 forcontent element-contents element-latest-posts" role="document"> <div class="content row"> <div class="latest-posts "> <div class="row"> <article class="col-xs-12 col-md-4"> <header> <h2 class="entry-title"><a href="/stc/holidays/uncategorized/yet-another-test-post/">Yet another test post</a></h2> </header> <section class="featured-image"> <a href="/stc/holidays/uncategorized/yet-another-test-post/" class="featured-image-link"> <img src="/stc/wp-content/uploads/2015/05/180H.jpg" class="attachment-post-thumbnail wp-post-image" alt="180H"> </a> </section> <section class="entry-summary"> <p>Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Donec ullamcorper nulla non me</p> </section> <footer> <p class="byline author vcard"><time class="updated" datetime="2015-10-16T14:03:00+00:00" title="" data-toggle="tooltip" data-placement="right" data-original-title="16th October, 2015"><i class="fa fa-clock-o"></i> Posted 23 mins ago</time> - By <a href="//localhost:3000/stc/holidays/author/c9admin/" rel="author" class="fn">Ash</a></p> <a href="/stc/holidays/uncategorized/yet-another-test-post/" class="btn btn-default btn-block featured-image-link"> Continued </a> </footer> </article> <article class="col-xs-12 col-md-4"> <header> <h2 class="entry-title"><a href="/stc/holidays/uncategorized/another-test-post/">Another Test Post</a></h2> </header> <section class="featured-image"> <a href="/stc/holidays/uncategorized/another-test-post/" class="featured-image-link"> <img src="/stc/wp-content/uploads/2015/05/188H.jpg" class="attachment-post-thumbnail wp-post-image" alt="188H"> </a> </section> <section class="entry-summary"> <p>Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Donec ullamcorper nulla non metus auctor fringilla. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur. Maecenas sed diam eget risus varius blandit sit amet non magna.</p> </section> <footer> <p class="byline author vcard"><time class="updated" datetime="2015-10-16T14:02:29+00:00" title="" data-toggle="tooltip" data-placement="right" data-original-title="16th October, 2015"><i class="fa fa-clock-o"></i> Posted 24 mins ago</time> - By <a href="//localhost:3000/stc/holidays/author/c9admin/" rel="author" class="fn">Ash</a></p> <a href="/stc/holidays/uncategorized/another-test-post/" class="btn btn-default btn-block featured-image-link"> Continued </a> </footer> </article> <article class="col-xs-12 col-md-4"> <header> <h2 class="entry-title"><a href="/stc/holidays/uncategorized/a-large-post/">A large Post</a></h2> </header> <section class="featured-image"> <a href="/stc/holidays/uncategorized/a-large-post/" class="featured-image-link"> <img src="/stc/wp-content/uploads/2015/04/slide-3.jpg" class="attachment-post-thumbnail wp-post-image" alt="slide-3"> </a> </section> <section class="entry-summary"> <p>Maecenas sed diam eget risus varius blandit sit amet non magna.</p> </section> <footer> <p class="byline author vcard"><time class="updated" datetime="2015-05-21T13:16:54+00:00" title="" data-toggle="tooltip" data-placement="right" data-original-title="21st May, 2015"><i class="fa fa-clock-o"></i> Posted 5 months ago</time> - By <a href="//localhost:3000/stc/holidays/author/c9admin/" rel="author" class="fn">Ash</a></p> <a href="/stc/holidays/uncategorized/a-large-post/" class="btn btn-default btn-block featured-image-link"> Continued </a> </footer> </article> <article class="col-xs-12 col-md-4"> <header> <h2 class="entry-title"><a href="/stc/holidays/uncategorized/test/">test</a></h2> </header> <section class="featured-image"> <a href="/stc/holidays/uncategorized/test/" class="featured-image-link"> <img src="/stc/wp-content/uploads/2015/05/168H.jpg" class="attachment-post-thumbnail wp-post-image" alt="168H"> </a> </section> <section class="entry-summary"> <p>Maecenas sed diam eget risus varius blandit sit amet non magna.</p> </section> <footer> <p class="byline author vcard"><time class="updated" datetime="2015-05-21T13:14:06+00:00" title="" data-toggle="tooltip" data-placement="right" data-original-title="21st May, 2015"><i class="fa fa-clock-o"></i> Posted 5 months ago</time> - By <a href="//localhost:3000/stc/holidays/author/c9admin/" rel="author" class="fn">Ash</a></p> <a href="/stc/holidays/uncategorized/test/" class="btn btn-default btn-block featured-image-link"> Continued </a> </footer> </article> <article class="col-xs-12 col-md-4"> <header> <h2 class="entry-title"><a href="/stc/holidays/uncategorized/hello-world/">Hello world!</a></h2> </header> <section class="featured-image"> <a href="/stc/holidays/uncategorized/hello-world/" class="featured-image-link"> <img src="/stc/wp-content/uploads/2015/04/slide-6.jpg" class="attachment-post-thumbnail wp-post-image" alt="slide-6"> </a> </section> <section class="entry-summary"> <p>Maecenas faucibus mollis interdum. Donec ullamcorper nulla non metus auctor fringilla. Integer posuere erat a ante venenatis dapibus posuere velit ali</p> </section> <footer> <p class="byline author vcard"><time class="updated" datetime="2015-03-19T14:48:48+00:00" title="" data-toggle="tooltip" data-placement="right" data-original-title="19th March, 2015"><i class="fa fa-clock-o"></i> Posted 7 months ago</time> - By <a href="//localhost:3000/stc/holidays/author/c9admin/" rel="author" class="fn">Ash</a></p> <a href="/stc/holidays/uncategorized/hello-world/" class="btn btn-default btn-block featured-image-link"> Continued </a> </footer> </article> </div> ,还是我基本上必须编写代理bool引用?

2 个答案:

答案 0 :(得分:3)

我能够提出的一件事(不确定这是否是最好的方法)是使用implicit_value()

po::typed_value<bool>* store_bool(bool* flag, bool store_as)
{
    return po::value(flag)->implicit_value(store_as)->zero_tokens();
}

value必须使用所需的默认值进行初始化,否则符合所需的功能:

bool value = false;
options.add_options()
    ("on", store_bool(&value, true))
    ("off", store_bool(&value, false))
    ;

答案 1 :(得分:1)

我不确定你的要求是否合理。如果用户输入“./a.out --on --off”,会发生什么?

否则,您只需确保在用户输入“./a.out --off”并且您将获得所需行为时,不会收到“无法识别的选项”消息。

bool flag;
bool flag_off

po::options_description options;
options.add_options()
    ("on", po::bool_switch(&flag)->default_value(false))
    ("off", po::bool_switch(&flag_off)->default_value(false))    ;

...

if( flag_on && flag_off )
{
  cout << "nasty error" << endl; exit(1)
}