C/C++ getopt/getopt_long 简单使用
在写c程序的时候,有时需要读入执行时的参数,这个就是用getopt实现的。
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 63 64 65 66 67 68 | $ vi test.cpp #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int opt; const char *optstring = "ab:c::d:"; while((opt = getopt(argc, argv, optstring)) != -1) { switch(opt) { case 'a': printf("opt a==%c ", opt); break; case 'b': printf("opt b==%c, arg: %s ", opt, optarg); break; case 'c': printf("opt c==%c, arg: %s ", opt, optarg); break; case 'd': printf("opt d==%c, arg: %s ", opt, optarg); break; default: printf("error opt %c"); return -1; } } return 0; } $ g++ -o test test.cpp $ ./test -a <<< 无参数 opt a==a $ ./test -a -b << 需要参数,不然会出错 opt a==a ./test: option requires an argument -- 'b' error opt x $ ./test -a -b 123 << 有参数不会出错 opt a==a opt b==b, arg: 123 $ ./test -a -b123 << 参数和选项之间无空格,依然可行 opt a==a opt b==b, arg: 123 $ ./test -a -b 123 -c << 没有参数不会出错 opt a==a opt b==b, arg: 123 opt c==c, arg: (null) $ ./test -a -b 123 -c 123 << 有参数但没有读取参数 opt a==a opt b==b, arg: 123 opt c==c, arg: (null) $ ./test -a -b 123 -c123 << 有参数且读取了参数 opt a==a opt b==b, arg: 123 opt c==c, arg: 123 $ ./test -a -b 123 -c123 -d 123 << 同参数b opt a==a opt b==b, arg: 123 opt c==c, arg: 123 opt d==d, arg: 123 |
小结
选项设置"ab:c::d:"
- a后面没有冒号,表示后面没有参数。
- b后面一个冒号,表示后面必须有参数,参数和选项b之间可以有空格,也可以没有空格。
- c后面两个冒号,表示后面可以有参数也可以没有参数,但是如果有参数,参数和选项c之间不能有空格。
- d选项同b。
getopt_long
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | $ vi test.cpp #include <unistd.h> #include <stdio.h> #include <getopt.h> int main(int argc, char **argv) { int opt, lopt, loidx; const char *optstring = "ab:c::d:"; const struct option long_options[] = { {"help", no_argument, &lopt, 1}, {"version", no_argument, &lopt, 2}, {"infile", required_argument, &lopt, 3}, {"outfile", required_argument, &lopt, 4}, {"logfile", optional_argument, &lopt, 5}, {0, 0, 0, 0} }; while((opt = getopt_long(argc, argv, optstring, long_options, &loidx)) != -1) { if(opt == 0) opt = lopt; switch(opt) { case 'a': printf("opt a==%c ", opt); break; case 'b': printf("opt b==%c, arg: %s ", opt, optarg); break; case 'c': printf("opt c==%c, arg: %s ", opt, optarg); break; case 'd': printf("opt d==%c, arg: %s ", opt, optarg); break; case 1: printf("opt help==%d ", opt); break; case 2: printf("opt version==%d ", opt); break; case 3: printf("opt infile==%d arg: %s ", opt, optarg); break; case 4: printf("opt outfile==%d arg: %s ", opt, optarg); break; case 5: printf("opt logfile==%d arg: %s ", opt, optarg); break; default: printf("error opt %c"); return -1; } } return 0; } $ g++ -o test test.cpp $ ./test --help opt help==1 $ ./test --help --infile pt help==1 ./test: option '--infile' requires an argument error opt x $ ./test --help --infile 123 opt help==1 opt infile==3 arg: 123 |
小结
- 多了一个
struct option 数组。struct optioin 元素的意思分别是:选项、是否需要参数,存储选项内容,返回值。- 第三个元素可以设置为NULL,这样就直接返回第四个元素的值。
总结
大致够用了。如有其它疑问可以查看