提高program_options:使用零参数选项多次吗? [英] boost program_options: using zero-parameter options multiple times?

查看:135
本文介绍了提高program_options:使用零参数选项多次吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用零参数选项多次使用的boost :: program_options。

I am wondering whether it is possible to use zero-parameter options multiple times with boost::program_options.

我心目中是这样的:

mytool --load myfile --print_status --do-something 23 --print_status

这是很容易得到与的有一个的print_status参数这方面的工作,但并不明显,我一个人如何可以使用这个选项两次(在我的情况下,提升将引发一个异常零参数选项指定一次以上)。

It is easy to get this working with one "print_status" parameter, but it is not obvious to me how one could use this option two times (in my case, boost throws an exception if a zero-parameter option is specified more than once).

所以,问题是:

有没有(简单)的方式与实现这一目标超出了从program_options箱功能?

现在,看来这是当前program_options实施的缺点。

Right now, it seems this is a drawback of the current program_options implementation.

P.S:

有已经过去(双双超过四十岁)类似的问题,在没有解决方案发现:

There have already been similar questions in the past (both over four years old), where no solution was found:

http://lists.boost.org/boost-users/ 2006/08 / 21631.php

http://benjaminwolsey.de/de/node/103

此线程包含一个解决方案,但它并不明显无论是工作之一,它似乎是这样一个简单的功能,而复杂的:

This thread contains a solution, but it is not obvious whether it is a working one, and it seems rather complex for such a simple feature:

<一个href=\"http://stackoverflow.com/questions/17093579/specifying-levels-e-g-verbose-using-boost-program-options\">Specifying使用水平(例如--verbose)升压program_options

推荐答案

如果您不需要算选项已被指定的次数,这是相当容易的(如果有点古怪);只是变量声明为矢量&lt;&布尔GT; 并设置以下参数:

If you don't need to count the number of times the option has been specified, it's fairly easy (if a little odd); just declare the variable as vector<bool> and set the following parameters:

std::vector<bool> example;
// ...
desc.add_options()
    ("example,e",
     po::value(&example)
     ->default_value(std::vector<bool>(), "false")
     ->implicit_value(std::vector<bool>(1), "true")
     ->zero_tokens()
    )
// ...

指定矢量燮presses多个参数检查; DEFAULT_VALUE 表示,向量应该在默认情况下是空的, implicit_value 说,如果<将其设置为1元向量code> -e / - 例如中指定, zero_tokens 表示,不消耗任何下列标记

Specifying a vector suppresses multiple argument checking; default_value says that the vector should by default be empty, implicit_value says to set it to a 1-element vector if -e/--example is specified, and zero_tokens says not to consume any following tokens.

如果 -e - 例如指定至少一次,的例子。大小()将完全 1 ;否则将 0

If -e or --example is specified at least once, example.size() will be exactly 1; otherwise it will be 0.

例。

如果您的的要算多少次的选项时,它很容易编写自定义类型和验证:

If you do want to count how many times the option occurs, it's easy enough to write a custom type and validator:

struct counter { int count = 0; };
void validate(boost::any& v, std::vector<std::string> const& xs, counter*, long)
{
    if (v.empty()) v = counter{1};
    else ++boost::any_cast<counter&>(v).count;
}

例。

请注意,不像在链接的问题,这并不让另外指定一个值(如 - 冗长6 ) - 如果你想要做一些复杂的你会需要编写一个自定义的 value_semantic 的子类,因为它不是由升压现有的语义支持。

Note that unlike in the linked question this doesn't allow additionally specifying a value (e.g. --verbose 6) - if you want to do something that complex you would need to write a custom value_semantic subclass, as it's not supported by Boost's existing semantics.

这篇关于提高program_options:使用零参数选项多次吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆