我有一个C程序,我接受多个参数。在这里,我有一个用于数据存储和磁盘的公共标志d
。在检查案例store
之前,有没有办法可以按顺序检查标记并获取d
的值。我尝试了各种方法,例如在此之前添加while循环以检查s
,然后输入此循环等。
static void
ParseOptions(int argc, char* argv[])
{
int c, option_index;
int ind = 0;
while((c = getopt_long(argc, argv, "s:d:",
long_options, &option_index))!= -1) {
ind = optind;
switch(c) {
case 's':
optionStore = true;
store = strdup(optarg);
break;
case 'd':
if(strcmp(store,"datastore") == 0){
printf("In datastore\n");
datastore = strdup(optarg);
}
else if(strcmp(store,"disk") == 0){
printf("In disk\n");
disk = strdup(optarg);
}
break;
default:
exit(-1);
}
}
}
不知道如何解决这个问题。
答案 0 :(得分:1)
您需要将标记optarg
的{{1}}存储在临时变量中,并在循环退出后使用它来设置d
或disk
:
datastore