C++ 使用getopt_long()解析命令行

C++ 使用getopt_long()解析命令行,c++,command-line,C++,Command Line,我似乎无法使用getopt_long()获取多个命令行参数。我的代码在打印1后出现故障。我有另一种方法来解析命令行,但这不起作用真让我头疼。这太简单了。有什么想法吗?谢谢 int main(int argc, char** argv) { int val; std::string clcf; int futures (0), blocks(0); static struct option long_options[] = { { "config" , require

我似乎无法使用getopt_long()获取多个命令行参数。我的代码在打印1后出现故障。我有另一种方法来解析命令行,但这不起作用真让我头疼。这太简单了。有什么想法吗?谢谢

int main(int argc, char** argv) {
int val;
std::string clcf;
int futures (0), blocks(0);


static struct option long_options[] =
{
    { "config"          , required_argument     , 0     , 'c' },
    { "sstest"            , optional_argument     , 0     , 's' },
    { "btest"          , optional_argument     , 0     , 'b' },
  //{ "ssfspreads"      , optional_argument     , 0     , 'S' },
  //{ "blockspreads"    , optional_argument     , 0     , 'B' },
    { 0                 , 0                     , 0     ,  0  }
};

while ((val = getopt_long (argc, argv, GETARGLIST.c_str(), long_options, 0)) != -1)
{

    switch (val)
    {
        case 'c': clcf = optarg; break;
        case 's': 
            std::cout << "1" << std::endl;

            futures = atoi( optarg ); break;

            std::cout << "2"  << futures <<  std::endl;
            break;
        case 'b': 
            std::cout << "1" << std::endl;
            //blocks = atoi(optarg); 
            break;
        //case 'ss': ssf_spreads = get_value<unsigned>(optarg); break;
        //case 'bs': block_spreads = get_value<stdunsigned>(optarg); break;
        default: break;
    }
}
int main(int argc,char**argv){
int-val;
std::字符串clcf;
整数期货(0),区块(0);
静态结构选项长_选项[]=
{
{“config”,必需的_参数,0,'c'},
{“sstest”,可选的_参数,0,'s'},
{“btest”,可选的_参数,0,'b'},
//{“ssfspreads”,可选的_参数,0,'S'},
//{“blockspreads”,可选_参数,0,'B'},
{ 0                 , 0                     , 0     ,  0  }
};
while((val=getopt_long(argc,argv,GETARGLIST.c_str(),long_options,0))!=-1)
{
开关(val)
{
案例“c”:clcf=optarg;中断;
案例s:

std::cout你是如何传递参数的?你应该传递没有空格的参数,否则它会出错。这有一些细节:

当你使用非标准的东西时,你应该提到它。我猜这是getopt.h中的GNU getopt_long,但它可以是任何东西。