请教getopt_long_only()函数的问题?

jackydi jackydi 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这个函数不能使用大写字母的短选项吗?
看下面的代码:

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>

static struct option long_options[] =
{
{"reqarg", required_argument, NULL, 'r'},
{"optarg", optional_argument, NULL, 'o'},
{"noarg1", required_argument, NULL, 'N'},
{"noarg", required_argument, NULL, 'n'},
{NULL, 0, NULL, 0},
};

int main(int argc, char *argv[])
{
int opt;
int digit_optind = 0;
int option_index = 0;
char *string = "A::b:c:N";
while((opt =getopt_long_only(argc,argv,string,long_options,&option_index))!= -1)
{
printf("opt = %c\t\t", opt);
printf("optarg = %s\t\t",optarg);
printf("optind = %d\t\t",optind);
printf("argv[optind] =%s\t\t", argv[optind]);
printf("option_index = %d\n",option_index);
}
}



在命令行中直接结果如下:

# ./t -r 324 -n 234 -N asdf
opt = r optarg = 324 optind = 3 argv[optind] =-n option_index = 0
./t: option '-n' is ambiguous
opt = ? optarg = (null) optind = 4 argv[optind] =234 option_index = 0
./t: unrecognized option '-N'
opt = ? optarg = (null) optind = 6 argv[optind] =asdf option_index = 0

为啥会有这两个红色字体的显示?struct option long_options[] 中不能定义大写字母的短选项值吗?
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
jackydi
工具平台和程序库 24,850 成员 27,330 内容