请教getopt_long_only()函数的问题?

jackydi 2019-07-26 05:56:58
这个函数不能使用大写字母的短选项吗?
看下面的代码:

#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[] 中不能定义大写字母的短选项值吗?
...全文
422 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenls_JL 2021-01-18
  • 打赏
  • 举报
回复
楼主的问题解决了吗。能分享下嘛。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧