我是使用命令行参数进行编程的新手,所以尽管找不到相似的帖子,我仍然坚持这个问题。
我需要传递一个包含多个参数的命令行选项,例如
./ testProgram --command arg1 arg2 arg3
这是我的代码
static struct option long_options[] = {
{"rdonly", required_argument, 0, 'a'},
{"wronly", required_argument, 0, 'b'},
{"command", required_argument, 0, 'c'},
{"verbose", no_argument, 0, 'd'},
{0,0,0,0}
};
c=getopt_long(argc,argv, "", long_options, &option_index);
if(c == -1)
break;
switch(c) {
case 'a':
rdonly(fd,optarg);
break;
case 'b':
wronly(fd,optarg);
break;
case 'c':
command(optind,optarg,argc,argv);
break;
case 'd':
printf("its verbose\n");
break;
default:
printf("WRONG OPTION\n");
}
}
//command function
int command(int optind, char *optarg, int argc, char* argv)
{
printf("Its option command with argument %s\n",optarg);
return 0;
}