我一直在学习如何使用getopt_long()
,以及如何使用optlong来“读取”多个字符选项。
我需要解析从终端输入的以下条目:
./bomb –n String –cp Integer –i Integer –c Integer –fc String
在使用getoptlong之前,我定义了我的短选项和长选项:
if(argc != 11){
perror("Error en numero de argumentos \n");
exit(EXIT_FAILURE);
}
const char* const short_options = "n:i:c:";
static struct option long_options[] = {
{"cp", 1, NULL, 'a'},
{"fc", 1, NULL, 'b'},
{0, 0 , 0, 0}
};
我的 short_options
捕获了一个带有参数的n
(这就是冒号:
的含义),c
和i
同理。所以长选项也应该采用相同的方式(它们也都接受参数)。
while(opt != -1){
opt = getopt_long(argc, argv, short_options, long_options, NULL);
switch (opt){
case 'n':
//print it
case 'a':
//print it
}
}
现在问题是,这段代码对于解析
-c -i
和-n
等参数可以完美工作,它进入所属的情况并正确打印输出。我的问题是,它对于-cp
和-fc
无法正常工作。因为我以前没有使用过getopt()
,所以我真的不知道怎么解决这个问题。提前感谢。