请教getopt_long_only()函数的问题?
这个函数不能使用大写字母的短选项吗?
看下面的代码:
在命令行中直接结果如下:
# ./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[] 中不能定义大写字母的短选项值吗?
看下面的代码:
#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[] 中不能定义大写字母的短选项值吗?
...全文
相关推荐
GNU 编码标准
- **getopt_long_only()**:与`getopt_long()`相似,但仅接受长选项而不接受短选项。
详细解析命令行的getopt_long()函数
**未定义选项处理**:如果遇到未知选项,`getopt_long()` 会返回 `?`,并可以通过 `optopt` 获取未知选项的字符。
windows下的getopt/getopt_long等函数,vs2010实测可用
GNU libc提供了getopt和getopt_long用于解析命令行参数,使用方便,但是windows环境没有提供。故将GNU libc提供的源码稍加修改,整理出了windows下可用的getop
getopt.h getopt getopt_long
win10 vs2019下的 getopt及getopt_longgetopt/getopt_long函数是GNU C中的函数
windows 版本 getopt_long
本文介绍了getopt函数的实现,该函数用于解析命令行参数,支持标准Unix风格,并允许灵活插入选项。代码文件属于GNU C库,提供了getopt、getopt_long和getopt_long_on


工具平台和程序库
