首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Boost程序选项中,如何判断是否指定了默认选项?

在Boost程序选项中,如何判断是否指定了默认选项?
EN

Stack Overflow用户
提问于 2021-06-20 08:45:39
回答 1查看 159关注 0票数 0

我正在编写一个程序,它需要一个大小、冗余和一些明文,并输出明文的代码。我希望按以下方式计算用于编码的大小和冗余:

  • 如果既未指定大小也未指定冗余,则冗余为3/7,且大小为所需大小。
  • 如果指定了大小,但没有指定冗余,则大小是指定的,而冗余是在指定大小中拟合明文的结果。如果明文小于这个大小的1/3,这是一个错误,因为最大冗余是2/3 (2/3和3/7来自汉明码)。如果明文太大,那也是一个错误。
  • 如果指定了冗余,但没有指定冗余,则大小就会达到所需的大小。
  • 如果两者都指定,则结果未定义。它可以与最后指定的任何一个一起使用,也可以任意选择一个。

代码位于https://github.com/phma/propolis;以下是相关部分:

代码语言:javascript
复制
#include <boost/program_options.hpp>
#include <cstdio>
#include <fstream>
#include <iostream>

using namespace std;
namespace po=boost::program_options;

int lastSizeRed=0;

void onSize(int size)
{
  lastSizeRed='s';
}

void onRed(string red)
{
  lastSizeRed='r';
}

int main(int argc,char **argv)
{
  int testflag=0,option_index=0,makedata=0;
  bool geneletters=false;
  int c,quality;
  double redundancy=0;
  int size=0;
  string text,infilename,outfilename;
  stringbuf filebuf;
  string redundancyStr,formatStr,patternStr;
  fstream infile;
  int format=FMT_PS,pattern=0;
  bool validCmd=true,helpFlag=false;
  po::options_description generic("Options");
  po::options_description hidden("Hidden options");
  po::options_description cmdline_options;
  po::positional_options_description p;
  po::variables_map vm;
  generic.add_options()
    ("size,s",po::value<int>(&size)->notifier(onSize),"Symbol size")
    ("redundancy,r",po::value<string>(&redundancyStr)->default_value("3/7")->notifier(onRed),"Redundancy (0,2/3]")
    ("text,t",po::value<string>(&text),"Text to encode")
    ("input,i",po::value<string>(&infilename),"File containing text to encode")
    ("output,o",po::value<string>(&outfilename),"Output file")
    ("format,f",po::value<string>(&formatStr)->default_value("ps"),"Output format")
    ("quality",po::value<int>(&quality)->default_value(1),"Quality of raster image (0-10)")
    ("pattern",po::value<string>(&patternStr),"Write a test pattern")
    ("writetables","Write decoding tables")
    ("geneletters","Optimize letters with genetic algorithm")
    ("test","Run tests")
    ("help","Show options");
  initialize();
  cmdline_options.add(generic).add(hidden);
  debugletters=0;
  try
  {
    po::store(po::command_line_parser(argc,argv).options(cmdline_options).positional(p).run(),vm);
    po::notify(vm);
    if (vm.count("test"))
      testflag=1;
    if (vm.count("writetables"))
      makedata=1;
    if (vm.count("geneletters"))
      geneletters=true;
    if (vm.count("help"))
      cout<<"Usage: propolis [options]\n"<<generic;
    cout<<"count(size)="<<vm.count("size")<<" count(redundancy)="<<vm.count("redundancy")<<" lastSizeRed="<<lastSizeRed<<endl;
  }
  catch (exception &ex)
  {
    cerr<<ex.what()<<endl;
    validCmd=false;
  }
  if (redundancyStr.length())
  {
    redundancy=parse_redundancy(redundancyStr);
    if (redundancy>0 && vm.count("redundancy"))
      size=0;
    else
    {
      cerr<<"Could not parse redundancy: "<<redundancyStr<<endl;
      validCmd=false;
    }
  }
  if (formatStr.length())
  {
    format=formatnum(formatStr);
    if (format<0)
      validCmd=false;
  }
  if (patternStr.length())
  {
    pattern=patternnum(patternStr);
    if (format<0)
      validCmd=false;
  }
}

我在“大小”和“冗余”上尝试了vm.count;不管它是否指定,它返回1表示“冗余”。我尝试在这两个选项中添加一个notifier;它总是通知冗余,并且首先通知冗余,即使它是在大小之后指定的(./propolis -s 4 -r 1/2lastSizeRed设置为's')。

EN

回答 1

Stack Overflow用户

发布于 2021-06-22 05:01:51

解决方案是使用额外解析器而不是通知器。如果未在命令行上指定值,则使用默认值调用通知程序;只使用命令行选项调用额外的解析器。

代码语言:javascript
复制
pair<string,string> checkSizeRed(const string &s)
{
  if (s.find("-s")==0 || s.find("--size")==0)
    lastSizeRed='s';
  if (s.find("-r")==0 || s.find("--red")==0)
    lastSizeRed='r';
  return make_pair(string(),string());
}
代码语言:javascript
复制
    po::store(po::command_line_parser(argc,argv).options(cmdline_options)
          .extra_parser(checkSizeRed).positional(p).run(),vm);

如果最后指定了lastSizeRed,则将--size设置为's‘;如果最后指定了--redundancy,则设置为'r’;如果没有指定,则设置为0。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68054146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档