升压程序选项中的互斥选项集 [英] Sets of mutually exclusive options in boost program options

查看:264
本文介绍了升压程序选项中的互斥选项集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序( prog.exe )支持以下四个标志: -P -p -b -s 。但是:




  • -b -p 必须一起指定,构成一个集合,并具有数值例如 -b 42
  • 不能使用上述设置指定
  • -s




因为 prog.exe 只能作为

运行

  prog.exe -P -s 

  prog.exe -P -b -42 -p 8 

有没有办法在boost程序选项中指定上述相互排斥的命令行选项?

解决方案

您应该先从几个教程开始 [1] [2] 来了解 boost :: program_options



然后,您可以定义两个互斥选项,只需定义一个小函数,如 real.cpp 。例如,您可以指定定义 conflicting_options()函数的两个冲突(或依赖)选项:

  void conflictsting_options(const boost :: program_options :: variables_map& vm,
const std :: string& opt1,const std :: string& opt2)
{
if(vm.count(opt1)&&!vm [opt1] .defaulted()&&
vm.count(opt2)&&!vm [opt2] .defaulted ))
{
throw std :: logic_error(std :: string(Conflicting options')+
opt1 +'和'+ opt2 +'。
}
}

然后调用

  conflicting_options(vm,quiet,verbose); $ <$> $>  

boost :: program_options :: store $ c>



我仍然不明白是否可以定义两个相互排斥的位置选项,但这应该是另一个问题。


My program (prog.exe) supports the following four flags: -P, -p , -b and -s. However:

  • -b and -p must be specified together, constitute a set, and have numeric values e.g. -b 42
  • -s cannot be specified with the above set, and vice versa
  • -P is mandatory in both cases

As such prog.exe can only be run as either

prog.exe -P -s 

or

prog.exe -P -b -42 -p 8

Is there a way to specify the above sets of mutually exclusive command line options in boost program options?

解决方案

You should start with a few tutorials [1][2] to understand how boost::program_options works.

Then, you can define two mutually exclusive options simply defining a small function as explained in real.cpp. For example, you can specify two conflicting (or depending) options defining a conflicting_options() function:

void conflicting_options(const boost::program_options::variables_map & vm,
                         const std::string & opt1, const std::string & opt2)
{
    if (vm.count(opt1) && !vm[opt1].defaulted() &&
        vm.count(opt2) && !vm[opt2].defaulted())
    {
        throw std::logic_error(std::string("Conflicting options '") +
                               opt1 + "' and '" + opt2 + "'.");
    }
}

and then calling

conflicting_options (vm, "quiet", "verbose");

right after boost::program_options::store()

I still don't understand if it's possible to define two mutually exclusive positional options, but that should be another question.

这篇关于升压程序选项中的互斥选项集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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