C/C++ getopt/getopt_long 简单使用

C/C++ getopt/getopt_long 简单使用

在写c程序的时候,有时需要读入执行时的参数,这个就是用getopt实现的。

getopt

$ 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\n", opt);
            break;
        case 'b':
            printf("opt b==%c, arg: %s\n", opt, optarg);
            break;
        case 'c':
            printf("opt c==%c, arg: %s\n", opt, optarg);
            break;
        case 'd':
            printf("opt d==%c, arg: %s\n", 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:"

  1. a后面没有冒号,表示后面没有参数。
  2. b后面一个冒号,表示后面必须有参数,参数和选项b之间可以有空格,也可以没有空格。
  3. c后面两个冒号,表示后面可以有参数也可以没有参数,但是如果有参数,参数和选项c之间不能有空格。
  4. d选项同b。

getopt_long

$ 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\n", opt);
            break;
        case 'b':
            printf("opt b==%c, arg: %s\n", opt, optarg);
            break;
        case 'c':
            printf("opt c==%c, arg: %s\n", opt, optarg);
            break;
        case 'd':
            printf("opt d==%c, arg: %s\n", opt, optarg);
            break;
        case 1:
            printf("opt help==%d\n", opt);
            break;
        case 2:
            printf("opt version==%d\n", opt);
            break;
        case 3:
            printf("opt infile==%d arg: %s\n", opt, optarg);
            break;
        case 4:
            printf("opt outfile==%d arg: %s\n", opt, optarg);
            break;
        case 5:
            printf("opt logfile==%d arg: %s\n", 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

小结

  1. 多了一个struct option数组。
  2. struct optioin元素的意思分别是:选项、是否需要参数,存储选项内容,返回值。
  3. 第三个元素可以设置为NULL,这样就直接返回第四个元素的值。

总结

大致够用了。如有其它疑问可以查看man getopt或者而联系博主一起学习讨论。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容