在阅读了关于 argp 的著名 pdf 之后,我想用它做点什么,但我遇到了问题,在这个例子中:
static int parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'd':
{
unsigned int i;
for (i = 0; i < atoi (arg); i++)
printf (".");
printf ("\n");
break;
}
}
return 0;
}
int main (int argc, char **argv)
{
struct argp_option options[] =
{
{ "dot", 'd', "NUM", 0, "Show some dots on the screen"},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, 0 };
return argp_parse (&argp, argc, argv, 0, 0, 0);
}
-d 接受 int 类型的参数,但是如果我想获取 char 或 char 数组作为参数呢?pdf 不包括文档。
我开始学习 C,我对它有基本的了解,我更熟悉其他语言,所以要了解更多关于它的信息,我想存档,但我不明白如何让它接受一个字符大批。
将 arg 与 char 进行比较时不起作用的代码:
static int parse_opt(int key, char *arg, struct argp_state *state)
{
switch(key)
{
case 'e':
{
//Here I want to check if "TOPIC" has something, in this case, a char array
//then based on that, do something.
if (0 == strcmp(arg, 'e'))
{
printf("Worked");
}
}
}
return 0;
}//End of parse_opt
int main(int argc, char **argv)
{
struct argp_option options[] =
{
{"example", 'e', "TOPIC", 0, "Shows examples about a mathematical topic"},
{0}
};
struct argp argp = {options, parse_opt};
return argp_parse (&argp, argc, argv, 0, 0, 0);
}//End of main
提前致谢。