以下是一个使用getopt_long函数的示例: #include <stdio.h> #include <stdlib.h> #include <getopt.h> int main(int argc, char *argv[]) { int c; int digit_optind = 0; int aopt = 0, bopt = 0; char *copt = 0; char *envi_opt = 0; while ( (c = getopt(argc, argv, 'ab:c:...
getopt函数用于解析简单的短选项,而getopt_long_only函数则用于解析长选项和复杂的短选项。两个函数的源代码可以在glibc库的源代码中找到。 getopt函数的解析过程如下: - getopt函数首先检查optind变量,如果其值大于或等于argc,则表示已经解析完所有的命令行参数,返回-1。 - 之后,getopt函数将当前解析的参数字符串和...
最后说说getopt_long_only函数,它与getopt_long函数使用相同的参数表,在功能上基本一致,只是getopt_long只将--name当作长参数,但getopt_long_only会将--name和-name两种选项都当作长参数来匹配。在getopt_long在遇到-name时,会拆解成-n -a -m -e到optstring中进行匹配,而getopt_long_only只在-name不能在longop...
int getopt_long_only (int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind); 2、参数介绍 argc argv :直接从main函数传递而来 shortopts:短选项字符串。如”n:v",这里需要指出的是,短选项字符串不需要'-’,而且但选项需要传递参数时,...