Linux/Uinx 系统编程:getopt()函数用法

Linux/Uinx 系统编程:getopt()函数用法

getopt()

函数描述
getopt()函数是用来分析命令行参数的,该函数由Unix标准库提供,包含在<unistd.h>头文件中。

函数原型

#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;

各参数说明

  • argc:通常由main函数直接传入,表示参数的数量。
  • argv:通常也由main函数直接传入,表示参数的字符串变量数组。
  • optstring:由所有合法的选项字符组成的字符串。比如你的程序允许的选项是 -E-n,那么optstring的值就是"En"

执行过程
当给定getopt()命令参数的数量 (argc)、指向这些参数的数组 (argv) 和选项字串 (optstring) 后,getopt()将返回第一个选项,并设置一些全局变量。使用相同的参数再次调用该函数时,它将返回下一个选项,并设置相应的全局变量。如果不再有可识别的选项,将返回-1,此任务就完成了。

底层实现
getopt()函数通过解析命令行参数,返回找到的下一个短选项,如果遇到无法识别的选项则返回’?'。当没有更多短选项时它返回-1,并且设置全局变量optind的值指向argv中所有段选项之后的第一个元素。

执行示例

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

int main (int argc, char **argv) {
   
    int i;
    int option;

    /* parse short options */
    while ((option = getopt(argc, argv, "bEnsTv")) != -1) {
   
        switch (option) {
   
            case 'b':
                puts("Put line numbers next to non-blank lines");
                break;
            case 'E':
                puts("Show the ends of lines as $");
                break;
            case 'n':
                puts("Put line numbers next to all lines");
                break;
            case 's':
                puts("Suppress printing repeated blank lines");
                break;
            case 'T':
                puts("Show tabs as ^I");
                break;
            case 'v':
                puts("Verbose");
                break;
            default: /* '?' */
                puts("What's that??");
        }
    }

    /* print the rest of the command line */
    puts("------------------------------");
    for (i = optind; i < argc; i++) {
   
        puts(argv[i]);
    }

    return 0;
}

这个演示程序没有实现cat命令的所有选项,但它只是能够解析命令行。每当发现一个合法的命令行选项,它就打印出相应的提示消息。

返回值
getopt()函数在成功解析一个选项时返回该选项的字符(因为字符可以转为整数)。若解析完毕,则返回-1。

执行结果
执行结果取决于getopt()函数是否成功解析了所有的命令行选项。如果成功,那么所有的命令行选项将被解析,如果失败,那么getopt()函数将返回’?'。

optarg

optarg是一个全局变量,用于存储命令行参数的值。它通常与getopt函数一起使用,用于解析命令行参数。getopt函数可以帮助我们解析命令行参数,并将其转换为可用的选项和参数。而optarg则用于存储选项的参数值。

例如,如果我们有一个命令行工具,它接受一个选项-a,该选项需要一个参数,我们可以使用getoptoptarg来获取这个参数。当我们在命令行中运行tool -a argument时,getopt函数会解析选项-a,并将argument的值存储在optarg中。

下面举个例子方便getoptoptarg的关系和理解:

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

int main(int argc, char *argv[]) {
   
    int opt;
    while ((opt = getopt(argc, argv, "a:")) != -1) {
   
        switch (opt) {
   
            case 'a':
                printf("Option -a has argument %s\n", optarg);
                break;
            default:
                printf("Unknown option\n");
        }
    }
    return 0;
}

在上面的例子中,定义了一个选项-a,该选项需要一个参数(由a:表示)。当我们运行./program -a argument时,getopt函数会解析选项-a,并将argument的值存储在optarg中。然后,我们可以在程序中使用这个值。。

相关推荐

  1. Linux/Uinx 系统编程getopt()函数

    2024-01-28 05:10:01       31 阅读
  2. SummaryWriter函数

    2024-01-28 05:10:01       31 阅读
  3. getopt()函数详细解释!保证看明白

    2024-01-28 05:10:01       33 阅读
  4. apply&lambda函数

    2024-01-28 05:10:01       41 阅读
  5. python:map()函数

    2024-01-28 05:10:01       13 阅读
  6. linux bash shell的getopt以及函数用法小记

    2024-01-28 05:10:01       32 阅读
  7. MATLAB中dlmwrite函数

    2024-01-28 05:10:01       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-28 05:10:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-28 05:10:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-28 05:10:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-28 05:10:01       20 阅读

热门阅读

  1. 力扣24-两两交换链表中的节点

    2024-01-28 05:10:01       40 阅读
  2. 基于SpringBoot实现策略模式提供系统接口扩展能力

    2024-01-28 05:10:01       36 阅读
  3. LeetCode-题目整理【11】:回溯算法

    2024-01-28 05:10:01       21 阅读
  4. Nginx限流详解

    2024-01-28 05:10:01       37 阅读
  5. Linux的几个常用基本指令2

    2024-01-28 05:10:01       35 阅读
  6. Python 每日学习 7.字符串

    2024-01-28 05:10:01       28 阅读
  7. 大语言模型-任务规划与分解论文

    2024-01-28 05:10:01       34 阅读