• C语言的getopt


    By francis_hao    Jul 5,2017

     

    getopt:分析命令行选项

    概述

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

     

    描述

    getopt()函数分析命令行参数,它的参数argcargv就是程序传递给main()函数的参数,一个以'-'(不是仅有一个'-'或"--")开头的argv元素就是一个选项。'-'后面跟着的就是选项字符,调用多次getopt()函数会连续返回选项字符

    变量optind是下一个要处理的元素在argv中的索引,系统初始化该变量为1,调用者可以给它复位为1,以重新扫描argv或扫描一个新的参数向量。

    如果getopt()找到一个选项字符,它返回该字符,更新外部变量optind和静态变量nextchar,因此下一次调用getopt()函数时可以继续扫描剩余的选项。如果已经没有更多的选项字符了,getopt()则返回-1,然后,optind变为argv第一个不是选项元素的索引。

    optstring是输入参数,它是一个包含需要进行处理的选项字符的集合,如果字符后面跟着一个冒号,表示该选项需要一个参数,因此getopt()将放置一个指向选项后面参数的指针给optarg,注意,形如-t 1和-t1都是可以的,都会将"1"传给optarg。两个冒号说明选项参数是可选的。

    默认情况下,getopt()会排列它扫描到的argv中的内容,因此最终,所有不是选项的项会排在最后,另外实现的两种模式:
    1、如果optstring的第一个字符是'+',或者环境变量POSIXLY_CORRECT置位,那么getopt()遇到非选项参数即停止,
    2、如果optstring的第一个字符是'-',那么每个不是选项的参数,都被当作选项为1的参数处理
    命令行中一个特殊的参数"--",当getopt()遇到它时,会强制参数扫描结束。

    如果getopt()没有识别选项字符,它会打印一个错误信息到标准错误,存放这个字符到optopt并返回'?'。调用程序可以通过将opterr置0来防止产生错误信息的打印。

    关于长选项参见man getopt_long。

     

    示例

    本示例为man手册上的例子,添加了一些打印以验证部分描述中的说明。

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

    int main(int argc, char *argv[])
    {
        int flags, opt;
        int nsecs;
        int i;

        nsecs = 0;
        flags = 0;
        printf("first of all optind is %d ", optind);
        printf("---at the begining,list argv value--- ");
        for (i=0; i<argc; i++){
            printf("argv[%u] is %s ",i,argv[i]);
        }
        printf("----------------------------- ");
        while ((opt = getopt(argc, argv, "nt:")) != -1) {
            printf("current optind is %d ", optind);
            switch (opt) {
                case 'n':
                    flags++;
                    break;
                case 't':
                    nsecs = atoi(optarg);
                    flags++;
                    break;
                default: /* '?' */
                    fprintf(stderr, "Usage: %s [-t nsecs] [-n] name ",argv[0]);
                    exit(EXIT_FAILURE);
            }
        }

        printf("---at the end,list argv value--- ");
        for (i=0; i<argc; i++){
            printf("argv[%u] is %s ",i,argv[i]);
        }
        printf("----------------------------- ");
        printf("after all optind is %d ", optind);

        printf("flags=%d; nsecs=%d ", flags, nsecs);

        if (optind >= argc) {
            fprintf(stderr, "Expected argument after options ");
            exit(EXIT_FAILURE);
        }
        printf("name argument = %s ", argv[optind]);

        /* Other code omitted */

        exit(EXIT_SUCCESS);
    }

     

    一个执行实例:

    做以下说明:

    1. optind会指向下一次getopt()函数要处理的argv的索引,最终会指向第一个不是选项的参数的argv中的索引(例中的"name")。
    2. argv中的参数会被重新排序,不是选项的参数会排在选项参数之后。
    3. 需要参数的选项,这两种形式是一样的:-t1和-t 1。

     

    程序源码在github上,可以直接clone编译。

     


    本文由 刘英皓 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可。欢迎转载,请注明出处:
    转载自:http://www.cnblogs.com/yinghao1991/p/7123622.html

     

    参考

    【1】 man 3 getopt

     

     

     

  • 相关阅读:
    re | [SWPU2019]ReverseMe
    wp | re | 2020“巅峰极客”网络安全技能挑战赛
    re | [CFI-CTF 2018]IntroToPE
    re | [FlareOn1]Bob Doge
    re | [ACTF新生赛2020]SoulLike
    re | [GKCTF2020]Chelly's identity
    ospf配置与ofps选举DR/BDR
    静态路由的配置
    配置三层交换机实现vlan间通信
    hybrid接口
  • 原文地址:https://www.cnblogs.com/yinghao-liu/p/7123622.html
Copyright © 2020-2023  润新知