getopt使用方法

getopt使用方法


头文件:

#include<unistd.h>

定义:

 int getopt(int argc,char * const argv[ ],const char * optstring);

说明:

extern char *optarg;
extern int optind, opterr, optopt;
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果在处理期间遇到了不符合optstring指定的其他选项getopt()将显示一个错误消息,并将全域变量optarg设为“?”字符,如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。

例:
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    ......
    char* config_1;
    char* config_2;
    char* config_3;
    int flag1 = 0;
    int flag2 = 0;
    int x;
    while ((x = getopt(argc, argv, "abc:e:p:")) != EOF) {
        switch(x) {
            case 'a':
                return fn_a();
            case 'b':
                flag1 = 1;
                return fn_b();
            case 'c':
                config_1 = optarg;
                break;
            case 'e':
                config_2 = optarg;
                 break;
            case 'p':
                 config_3 = optarg;
                 flag_2 = 1;
                 return fn_p(config_3);
            default:
                 printf("Error argument: %c\n", char(optopt));
                 return usage();
        }
    }
}

GetOpt.h是GNU标准库的头文件,包含从命令行提取参数的工具,用于基于文本的C/C++应用程序。以下是其使用的相关要点: ### 编译与链接 由于getopt.h不是ANSI C标准库的一部分,getopt必须编译到每个使用它的项目中,或者编译成一个静态类,显式地链接到程序中。在Windows下有预编译的getopt版本可用 [^1]。 ### 全局参数 - `extern char *optarg`:当前选项带参数时,`optarg`指向该参数。 - `extern int optind`:`argv`的索引。通常选项参数取得完毕时,通过此变量可以取得非选项参数(`argv[optind]`)。 - `extern int opterr`:将此变量设置为0,可以抑制`getopt()`输出错误信息。 - `extern int optopt`:一个选项在`argv`中有,但在`optstring`中不存在时,或者一个带参数的选项没有参数时,`getopt()`返回`?`,同时将`optopt`设为该选项 [^4]。 ### 函数原型 `int getopt_long(int argc, char* const argv[], const char* optstring, const struct option* longopts, int* longindex)` [^5]。 ### 使用示例 下面是一个简单的使用`getopt.h`的C语言示例代码: ```c #include <stdio.h> #include <getopt.h> int main(int argc, char *argv[]) { int opt; while ((opt = getopt(argc, argv, "abc:")) != -1) { switch (opt) { case 'a': printf("Option -a was specified.\n"); break; case 'b': printf("Option -b was specified.\n"); break; case 'c': printf("Option -c was specified with value %s.\n", optarg); break; case '?': if (optopt == 'c') fprintf(stderr, "Option -%c requires an argument.\n", optopt); else if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c'.\n", optopt); else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; default: abort(); } } for (int index = optind; index < argc; index++) { printf("Non-option argument %s\n", argv[index]); } return 0; } ``` 在这个示例中,`getopt`函数用于解析命令行参数。`"abc:"`是`optstring`,表示支持`-a`、`-b`和`-c`选项,其中`-c`选项需要一个参数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值