getopt_long()函数不返回'?'或':'表示命令行参数中的错误[C ++]

时间:2015-08-02 19:33:15

标签: c++

我想知道为什么以下代码中的getopt_long()不会为我的程序中当前定义的选项的无效选项或缺失值返回'?'':'。此外,当我要打印optoptopterr时,它不会反映我的计划的当前状态。 非常感谢你!

void parse_options(int argc,char **argv,program_options &opt)
{
    int verbose_flag=0;
    const char* opt_string =":k:s:c:g:o:t:f";
    static struct option long_options[]=
    {
        {"verbose",no_argument,&verbose_flag,1},
        {"filter",no_argument,NULL,'f'},
        {"kmer size",required_argument,NULL,'k'},
        {"gap size",required_argument,NULL,'s'},
        {"coverage",required_argument,NULL,'c'},
        {"threads",required_argument,NULL,'t'},
        {"genome size",required_argument,NULL,'g'},
        {"output file name",required_argument,NULL,'o'},
        {NULL,no_argument,NULL,0}
    };
   int option_index=0;
   int ch;
   while((ch=getopt_long(argc,argv,opt_string,long_options,&option_index))!=-1)
   {
       /* if(ch==0)
        {

          std::cerr<<std::endl;
          std::cerr<<"--- invalid option or missing value "<<std::endl;
          print_usage();
          exit(1);


        }*/

        if(ch=='k')
        {opt.k=atoi(optarg);}
        else if(ch=='s')
        {opt.s=atoi(optarg);opt.compute_g=false;}
        else if(ch=='c')
        {opt.c=atoi(optarg);opt.compute_c=false;}
        else if(ch=='t')
        {opt.t=atoi(optarg);}
        else if(ch=='g')
        {opt.genomesize=atoll(optarg);}
        else if(ch=='f')
        {opt.filter = true;}
        else if(ch=='o')
        {opt.output_prefix_name=optarg;}
        else if(ch=='?')
        {std::cout<<" invalid option "<<std::endl;}
        else if(ch==':')
        {std::cout<<" option with missing argument"<<std::endl;}

  }

  for(int i=optind;i<argc;++i)
       opt.read_files.push_back(argv[i]);

   if(verbose_flag)
     {opt.verbose=true;}

}

0 个答案:

没有答案