C语言getopt()函数的使用

getopt(分析命令行参数

相关函数表头文件

#include <unistd.h>

定义函数

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

函数说明

getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。

短参数的定义

   getopt()使用optstring所指的字串作为短参数列表,象"1ac:d::"就是一个短参数列表。短参数的定义是一个'-'后面跟一个字母或数字,象-a, -b就是一个短参数。每个数字或字母定义一个参数。 

  其中短参数在getopt定义里分为三种:
  1. 不带值的参数,它的定义即是参数本身。
  2. 必须带值的参数,它的定义是在参数本身后面再加一个冒号。
  3. 可选值的参数,它的定义是在参数本身后面加两个冒号 。
  在这里拿上面的”1ac:d::”作为样例进行说明,其中的1,a就是不带值的参数,c是必须带值的参数,d是可选值的参数。
  在实际调用中,’-1 -a -c cvalue -d’, ‘-1 -a -c cvalue -ddvalue’, ‘-1a -ddvalue -c cvalue’都是合法的。这里需要注意三点:
  1. 不带值的参数可以连写,象1和a是不带值的参数,它们可以-1 -a分开写,也可以-1a或-a1连写。
  2. 参数不分先后顺序,’-1a -c cvalue -ddvalue’和’-d -c cvalue -a1’的解析结果是一样的。
  3. 要注意可选值的参数的值与参数之间不能有空格,必须写成-ddvalue这样的格式,如果写成-d dvalue这样的格式就会解析错误。

返回值

  getopt()每次调用会逐次返回命令行传入的参数。
  当没有参数的最后的一次调用时,getopt()将返回-1。
  当解析到一个不在optstring里面的参数,或者一个必选值参数不带值时,返回’?’。
  当optstring是以’:’开头时,缺值参数的情况下会返回’:’,而不是’?’ 。

范例

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

int main(int argc,char *argv[])
{
    int i;
    printf("-------------\n");
    for(i=0;i<argc;i++)
    {
        printf("%s\n",argv[i]);
    }
    printf("------------------\n");

    int ch;
    printf("\n\n");
    printf("optind:%d,opterr:%d\n",optind,opterr);
    printf("---------------\n");
    while((ch=getopt(argc,argv,"ab:c:de::"))!=-1)
    {
        printf("optind:%d\n",optind);
        switch(ch)
        {
            case 'a':
                printf("HAVE option:-a\n\n");
                break;
            case 'b':
                printf("HAVE option :-b\n");
                printf("The argument of -b is %s\n\n",optarg);
                break;
            case 'c':
                printf("HAVE option :-c\n");
                printf("The argument of -c is %s\n\n",optarg);
                break;
            case 'd':
                printf("HAVE option:-d\n");
                break;
            case 'e':
                printf("HAVE option:-e\n");
                printf("The argument of -e is %s\n\n",optarg);
                break;
            case '?':
                printf("Unknown option:%c\n",(char)optopt);
                break;
        }
    }
    printf("------------\n");
    printf("optind=%d,argv[%d]=%s\n",optind,optind,argv[optind]);
    printf("---------------------\n");
    for(i=0;i<argc;i++)
    {
        printf("%s\n",argv[i]);
    }
    printf("--------------\n");
}

注:

如”ab:c:de::”,getopt会依次解析出-a,-b,-c,-d,-e,然后利用switch语句对参数选型进行配置。
-a 无参数
-b 必须有参数
-c 必须有参数
-d 无参数
-e 可以有参数,有参数必须紧邻参数写,也可以没有参数,没有参数时,其值为null
参数选项后面无分号:则多个参数选项可以紧邻着写,也可分开写
参数选项后一个分号:会找到一个可疑参数(可能为参数,也可能为没写参数后直接跟的参数选项),否则就报错
参数选项后两个分号:会找到紧邻参数选项的参数,若找不到紧邻的参数,则认为无参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言getopt()函数用于分析命令行参数。它的声明为: int getopt(int argc, char * const argv[], const char * optstring); 该函数通过解析命令行参数来获取选项和参数的值。用户可以在命令行中使用不同的选项来传递参数给程序。 下面是一个getopt()函数的示例代码: #include <unistd.h> #include <stdio.h> int main(int argc, char * argv[]){ int i; printf("%s\n","argv原序:"); for(i = 0;i < argc;i++){ printf("%s ",argv[i]); } printf("\n"); printf("START@optind:%d,opterr:%d\n",optind,opterr); int ret; while((ret = getopt(argc,argv,"aW;b:c:de::")) != -1){ switch(ret){ case 'a': printf("Having option -a\n"); break; case 'b': printf("having option -b,and its argument is %s\n",optarg); break; case 'c': printf("having option -c,and its argument is %s\n",optarg); break; case 'd': printf("Having option -d\n"); break; case 'e': printf("having option -e,it is optional,and its argument is %s\n",optarg); break; case '?': printf("Unknown option -%c\n",(char)optopt); break; } } printf("END@optind:%d,argv[%d]:%s\n",optind,optind,argv[optind]); printf("%s\n","argv现序:"); for(i = 0;i < argc;i++){ printf("%s ",argv[i]); } printf("\n"); return 0; } 该示例演示了如何使用getopt()函数解析命令行参数。程序通过指定选项和参数的形式来接受用户输入,并根据不同的选项执行不同的操作。在示例中,选项包括:a、b、c、d和e,其中e选项是可选的。通过在命令行中输入相应的选项和参数,程序会根据不同的选项执行相应的代码逻辑。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C语言getopt()函数和select()函数使用方法](https://download.csdn.net/download/weixin_38621104/14868224)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C语言——getopt函数](https://blog.csdn.net/weixin_40763897/article/details/87898328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值