c c++ getopt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
/*
原型:int getopt(int argc, char * const argv[], const char *optstring);
 
四个全局变量:
extern char *optarg;             //指向选项的参数的指针。
extern int optind,              //存储位置,下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr,        //opterr=0时,getopt不向stderr输出错误信息。
extern int optopt;        //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中。
 
关于optstring
如:    optstring=ab:c::d::时,
1ab:c::d::的意思是有四个选项-a -b -c -d
2、其中b c d选项后面有冒号说明b c d后面必须跟参数;
3b后一个冒号,则参赛和选项之间可有空格,也可没有;
4c d后面有两个冒号,说明c d后面的参数必须紧跟在选项后面。
如:./a.out -a -b argOfb -cargOfc -dargOfd
如果写成:./a.out -a -b argOfb -c argOfc -dargOfd c选项后的参数实际为null
 
getopt的使用时的主要注意点:
1getopt每调用一次,返回一个选项,注意是选项不是参数;
2optarg存放getopt所返回的选项后的参数,选项后没有参数则为null(实际运行时没有或者optstring中的选项后没有:都视为null)
3、当再也检查不到optstring所包含的选项时返回-1
4、当检查到不在optstring中的选项或缺少必要的参数时,返回?并将该选项存在optopt中。
 
总结:
在使用时,只需简记getopt返回选项,optarg指向参数即可。
 
*/
#include <unistd.h>
 
int main(int argc, char **argv)
{
    int opt;
    opterr = 0;
   
    while( (opt = getopt(argc, argv, "ab:c::d::")) != -1 )
    {
         switch(opt)
          {
              case 'a':
                  printf("option=a, opt=%d, optarg=%s, optind=%d, optopt=%d\n", opt, optarg, optind, optopt);
                  break;
              case 'b':
                  printf("option=b, opt=%d, optarg=%s, optind=%d, optopt=%d\n", opt, optarg, optind, optopt);
                    break;
              case 'c':
                  printf("option=c, opt=%d, optarg=%s, optind=%d, optopt=%d\n", opt, optarg, optind, optopt);
                     break;
              case 'd':
                  printf("option=d, opt=%d, optarg=%s, optind=%d, optopt=%d\n", opt, optarg, optind, optopt);
                     break;
              case '?':
                  printf("option=?, opt=%d, optarg=%s, optind=%d, optopt=%d\n", opt, optarg, optind, optopt);
                   break;
                default:
                  printf("option=default, opt=%d, optarg=%s, optind=%d, optopt=%d\n", opt, optarg, optind, optopt);
                     break;
          }
    }
    return 0;
}


/*

root@ubuntu:~/eclipseworkspace# ./a.out -a -b barg -ccarg -ddarg
option=a, opt=97, optarg=(null), optind=2, optopt=0
option=b, opt=98, optarg=barg, optind=4, optopt=0
option=c, opt=99, optarg=carg, optind=5, optopt=0
option=d, opt=100, optarg=darg, optind=6, optopt=0


root@ubuntu:~/eclipseworkspace# ./a.out -a
option=a, opt=97, optarg=(null), optind=2, optopt=0


root@ubuntu:~/eclipseworkspace# ./a.out -ccarg
option=c, opt=99, optarg=carg, optind=2, optopt=0


root@ubuntu:~/eclipseworkspace# ./a.out -c carg
option=c, opt=99, optarg=(null), optind=2, optopt=0


root@ubuntu:~/eclipseworkspace# ./a.out
root@ubuntu:~/eclipseworkspace# ./a.out a
root@ubuntu:~/eclipseworkspace# ./a.out -b barg
option=b, opt=98, optarg=barg, optind=3, optopt=0


root@ubuntu:~/eclipseworkspace# ./a.out -b
option=?, opt=63, optarg=(null), optind=2, optopt=98


root@ubuntu:~/eclipseworkspace# ./a.out -b -c
option=b, opt=98, optarg=-c, optind=3, optopt=0




*/
原文地址:https://www.cnblogs.com/helloweworld/p/2695792.html