1.関数プロトタイプ:
#include <unistd.h>
extern char * optarg;
extern int optind、opterr、optopt;
#include <getopt.h>
int getopt_long(int argc、char * const argv []、const char * optstring、const struct option * longopts、int * longindex);
2.説明:
1。argcとargvは、main関数の2つのパラメーターと一致しています。
2. optstring:短いオプション文字列を表します。
「abc:d:012f ::」の意味:サポートできる短いオプションは次のとおりです。-a-b-c -d -0 -1 -2 -fなど。
(1)コロンのない文字は1つだけです。-----オプションのみを示します。例:-d
(2)文字+コロン:オプションの後のパラメーターを示します。例:-c 123
(3)文字+ 2つのコロン:オプションのパラメーターの後にオプションを続けることができます。パラメーターがある場合、スペースを含めることはできません
。例:-f789 3. longopts:longオプション構造:
struct option
{
const char *name;
int has_arg;
int *flag;
int val;
};
struct option long_options[] = {
{"add", required_argument, NULL, 0 },
{"append", no_argument, NULL, 0 },
{"delete", required_argument, NULL, 0 },
{"verbose", no_argument, NULL, 0 },
{"create", required_argument, NULL, 'c'},
{"file", required_argument, NULL, 0 },
{"test1", no_argument, &lopt, 1 },
{"test2", no_argument, &lopt, 2 },
{"test3", no_argument, &lopt, 3 },
{0, 0, 0, 0 } //最后一行必须为零,否则段错误
};
(1)name:
表示长选项的名称,如:add、append
(2)has_arg:
no_argument(或者是0)时:后面不跟参数,eg:--help
required_argument(或者是1)时:后面跟参数,eg:--add=123 /--add 123
optional_argument(或者是2)时:后面跟参数,eg:--add=123
(3)flag:
flag为NULL,返回val的值;
flag不为NULL: getopt_long 函数将返回0,且将flag指针指向val(等同于返回val的值)
(4)val:函数的返回值;
4.longindex :
指向当前长选项的第几个值,从0开始,即long_options的下标值:
5.全局变量:
(1)optarg:当前选项的参数值
(2)optind:表示下一个将被处理到的参数在argv中的下标值:
```c
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>
int main(int argc, char **argv)
{
int c;
int digit_optind = 0;
while (1)
{
int lopt = 0;
int option_index = 0;
struct option long_options[] = {
{"add", required_argument, NULL, 0 },
{"append", no_argument, NULL, 0 },
{"delete", required_argument, NULL, 0 },
{"verbose", no_argument, NULL, 0 },
{"create", required_argument, NULL, 'c'},
{"file", required_argument, NULL, 0 },
{"test1", no_argument, &lopt, 1 },
{"test2", no_argument, &lopt, 2 },
{"test3", no_argument, &lopt, 3 },
{0, 0, 0, 0 }
};
printf("[%s:%d]:[yang] -------------------- optind = %d\n",__FUNCTION__,__LINE__,optind);
printf("[%s:%d]:[yang] optarg = %s\n",__FUNCTION__,__LINE__,optarg);
c = getopt_long(argc, argv, "abc:d:012f::", long_options, &option_index);
printf("[%s:%d]:[yang] c = %d,c = %c, option_index = %d\n",__FUNCTION__,__LINE__,c,c,option_index);
if (c == -1)
break;
switch (c)
{
case 0: //长参数 --
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
switch (lopt)
{
case 1:
printf("long option lopt = %d\n", lopt);
break;
case 2:
printf("long option lopt = %d\n", lopt);
break;
case 3:
printf("long option lopt = %d\n", lopt);
break;
default:
printf("long option default\n");
break;
}
break;
case '0':
case '1':
case '2':
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value '%s'\n", optarg);
break;
case 'd':
printf("option d with value '%s'\n", optarg);
break;
case 'f':
if (optarg)
printf("option f with value '%s'\n", optarg);
else
printf("option f with no value \n");
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
printf("[%s:%d]:[yang] optind = %d\n",__FUNCTION__,__LINE__,optind);
printf("[%s:%d]:[yang] argc = %d\n",__FUNCTION__,__LINE__,argc);
if (optind < argc)
{
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
exit(EXIT_SUCCESS);
}
打印结果:
aston@ubuntu:/mnt/hgfs/share/source_insight2/main_0$ ./app.out --add=123 --append --test1 -0 -f456 -a
[main:29]:[yang] -------------------- optind = 1
[main:30]:[yang] optarg = (null)
[main:34]:[yang] c = 0,c = , option_index = 0
option add with arg 123
long option default
[main:29]:[yang] -------------------- optind = 2
[main:30]:[yang] optarg = 123
[main:34]:[yang] c = 0,c = , option_index = 1
option append
long option default
[main:29]:[yang] -------------------- optind = 3
[main:30]:[yang] optarg = (null)
[main:34]:[yang] c = 0,c = , option_index = 6
option test1
long option lopt = 1
[main:29]:[yang] -------------------- optind = 4
[main:30]:[yang] optarg = (null)
[main:34]:[yang] c = 48,c = 0, option_index = 0
option 0
[main:29]:[yang] -------------------- optind = 5
[main:30]:[yang] optarg = (null)
[main:34]:[yang] c = 102,c = f, option_index = 0
option f with value '456'
[main:29]:[yang] -------------------- optind = 6
[main:30]:[yang] optarg = 456
[main:34]:[yang] c = 97,c = a, option_index = 0
option a
[main:29]:[yang] -------------------- optind = 7
[main:30]:[yang] optarg = (null)
[main:34]:[yang] c = -1,c = �, option_index = 0
[main:103]:[yang] optind = 7
[main:104]:[yang] argc = 7