Linux 解析长/短选项参数 getopt_long

代码的最后面有解释说明,直接上代码:

#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

extern char* optarg;
extern int optind;
extern int opterr;
extern int optopt;

const char getopt_str[] = "hab:c::";
static struct option long_options[] = 
{
    {"version", no_argument,        0,  'v' },
    {"size",    required_argument,  0,  's' },
    {"file",    optional_argument,  0,  'f' },
};

int main(int argc, char *argv[])
{
    int opt = 0;
    int longindex = 0;

    while((opt = getopt_long(argc, argv, getopt_str, long_options, &longindex)) != -1)
    {
        switch (opt)
        {
            case 'h':
                printf("opt is h:\n");
                printf("./a.out -h -a -b 11 -caa --version --size 123 --file=a.out\n");
                break;
            case 'a':
                printf("opt is a:\n");
                break;
            case 'b':
                printf("opt is b:\n");
                printf("b arg is:%s\n", optarg);
                break;
            case 'c':
                printf("opt is c:\n");
                printf("c arg is:%s\n", optarg);
                break;
            case 'v':
                printf("opt is %s:\n", long_options[longindex].name);
                printf("%s arg is:%s\n", long_options[longindex].name, optarg);
                break;
            case 's':
                printf("opt is %s:\n", long_options[longindex].name);
                printf("%s arg is:%s\n", long_options[longindex].name, optarg);
                break;
            case 'f':
                printf("opt is %s:\n", long_options[longindex].name);
                printf("%s arg is:%s\n", long_options[longindex].name, optarg);
                break;            
            default:
                break;
        }
    }

    return 0;
}


#if 0

全局变量:
optarg:指向当前选项对应的参数地址
optind:表示的是下一个将被处理到的参数在argv中的下标值。
opterr:如果opterr = 0,在getopt、getopt_long、getopt_long_only遇到错误将不会输出错误信息到标准输出流。opterr在非0时,向屏幕输出错误。
optopt:表示没有被未标识的选项。


optstring:短选项字符串
hab:c::
表示短选项h、a后面没有参数      注:选项后面没有冒号就表示没有参数
表示短选项b后面肯定有一个参数。其形式可以表示为 "-b123" 或者 "-b 123"   注:选项后面一个冒号,就表示必须要跟参数
表示短选项c后面可以跟参数也可以不跟参数。如果跟参数,短选项c和参数之间不能有空格。 其形式必须是 "-carg" 比如:"-c123"   注:选项后面两个冒号,就表示可以有参数也可以没有参数。

struct option 
{  
     const char *name;          //长选项的名字 比如 --file a.txt    name就是file        --file=a.txt    name就是file
     int         has_arg;       //长选项后面是否跟参数  no_argument/0:长选项后面不跟参数   required_argument/1: 长选项后面跟参数,参数格式“--参数 值” 或者 “--参数=值”   optional_argument/2:长选项后面跟参数,参数格式只能是“--参数=值”
     int        *flag;          //标志。flag为空时,getopt_long返回结构体val的值。 flag不为空时,getopt_long将返回0,flag指针将指向val值。
     int         val;           //flag为空时,val就是就是getopt_long的返回值。flag不为空时,val的地址就是就是flag指针指向的地址
};

#include <unistd.h>
#include <getopt.h>
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
argc:参数的个数
argv:保存参数的二维数组
optstring:短选项字符串
longopts:长选项结构体
longindex:如果longindex非空,那么改指针指向的内存用于保存找到的longopts的下标值。
#endif

编译:gcc getopt_long.c

执行:

./a.out -h
或者
./a.out -ah -b 11 -caa --version --size 123 --file=a.out

程序输出:

qidong.liu@cqrnd01:~/lqd/c/get_opt$ gcc getopt_long.c
qidong.liu@cqrnd01:~/lqd/c/get_opt$ ./a.out -h
opt is h:
./a.out -h -a -b 11 -caa --version --size 123 --file=a.out
qidong.liu@cqrnd01:~/lqd/c/get_opt$ ./a.out -ah -b 11 -caa --version --size 123 --file=a.out
opt is a:
opt is h:
./a.out -h -a -b 11 -caa --version --size 123 --file=a.out
opt is b:
b arg is:11
opt is c:
c arg is:aa
opt is version:
version arg is:(null)
opt is size:
size arg is:123
opt is file:
file arg is:a.out
qidong.liu@cqrnd01:~/lqd/c/get_opt$

全局变量:

optarg:指向当前选项对应的参数地址

optind:表示的是下一个将被处理到的参数在argv中的下标值。

opterr:如果opterr = 0,在getopt、getopt_long、getopt_long_only遇到错误将不会输出错误信息到标准输出流。opterr在非0时,向屏幕输出错误。

optopt:表示没有被未标识的选项。


 

optstring:短选项字符串

hab:c::

表示短选项h、a后面没有参数      注:选项后面没有冒号就表示没有参数

表示短选项b后面肯定有一个参数。其形式可以表示为 "-b123" 或者 "-b 123"   注:选项后面一个冒号,就表示必须要跟参数

表示短选项c后面可以跟参数也可以不跟参数。如果跟参数,短选项c和参数之间不能有空格。 其形式必须是 "-carg" 比如:"-c123"   注:选项后面两个冒号,就表示可以有参数也可以没有参数。

struct option

{  

     const char *name;          //长选项的名字 比如 --file a.txt    name就是file        --file=a.txt    name就是file

     int         has_arg;       //长选项后面是否跟参数  no_argument/0:长选项后面不跟参数   required_argument/1: 长选项后面跟参数,参数格式“--参数 值” 或者 “--参数=值”   optional_argument/2:长选项后面跟参数,参数格式只能是“--参数=值”

     int        *flag;          //标志。flag为空时,getopt_long返回结构体val的值。 flag不为空时,getopt_long将返回0,flag指针将指向val值。

     int         val;           //flag为空时,val就是就是getopt_long的返回值。flag不为空时,val的地址就是就是flag指针指向的地址

};

#include <unistd.h>

#include <getopt.h>

int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);

argc:参数的个数

argv:保存参数的二维数组

optstring:短选项字符串

longopts:长选项结构体

longindex:如果longindex非空,那么改指针指向的内存用于保存找到的longopts的下标值。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`getopt_long` 是一个 C 语言的函数,用于解析命令行参数。与传统的 `getopt` 函数不同,`getopt_long` 支持选项(如 --help)和选项(如 -h)。 下面是 `getopt_long` 的基本用法: ```c #include <stdio.h> #include <stdlib.h> #include <getopt.h> int main(int argc, char *argv[]) { int c; int digit_optind = 0; while (1) { int option_index = 0; static struct option long_options[] = { {"add", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, 'c'}, {"file", required_argument, 0, 0 }, {0, 0, 0, 0 } }; c = getopt_long(argc, argv, "abc:d:012", long_options, &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"); break; case '0': case '1': case '2': if (digit_optind != 0 && digit_optind != optind) printf("digits occur in two different argv-elements.\n"); digit_optind = optind; 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 '?': break; default: printf("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } exit(EXIT_SUCCESS); } ``` 上面的代码演示了 `getopt_long` 的基本用法。其中 `long_options` 数组定义了选项的名称、参数类型以及对应的选项(如果有的话)。在调用 `getopt_long` 函数时,第三个参数选项字符串,其中冒号表示该选项需要参数。 函数返回值为下一个选项的字符编码,或者 -1 表示已经解析完所有选项。如果选项需要参数,则该参数存储在全局变量 `optarg` 中。 更多关于 `getopt_long` 的用法,可以参考其官方文档:http://man7.org/linux/man-pages/man3/getopt_long.3.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

monkey_llll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值