getopt()

本文详细介绍了命令行参数解析函数getopt()的使用方法,包括其参数解释、函数原型、变量作用及常见用法示例。通过实例演示了如何正确解析命令行参数,帮助开发者更高效地处理命令行应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cnblogs.com/sunyubo/archive/2010/09/17/2282120.html

http://vopit.blog.51cto.com/2400931/440453


函数getopt()用来分析命令行参数,其函数原型和相关变量声明如下:

#include 
extern char *optarg; 
extern int optind,  // 初始化值为1,下一次调用getopt时,从optind存储的位置重新开始检查选项,也就是从下一个'-'的选项开始。 
extern int opterr,  // 初始化值为1,当opterr=0时,getopt不向stderr输出错误信息。 
extern int optopt;  // 当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中,getopt返回’?’。

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

optarg和optind是两个最重要的external                                                                                              
变量。optarg是指向参数的指针(当然这只针对有参数的选项);optind是argv[]数组的索引,众所周知,argv[0]是函数名称,所有参数从argv[1] 
开始,所以optind被初始化设置指为1。 每调用一次getopt()函数,返回一个选项,如果该选项有参数,则optarg指向该参数。                  
在命令行选项参数再也检查不到optstring中包含的选项时,返回-1。

函数getopt()有三个参数,argc和argv[]应该不需要多说,下面说一下字符串optstring,它是作为选项的字符串的列表。

函数getopt()认为optstring中,以'-                                                                                                 
’开头的字符(注意!不是字符串!!)就是命令行参数选项,有的参数选项后面可以跟参数值。optstring中的格式规范如下: 
1) 单个字符,表示选项, 
2) 单个字符后接一个冒号”:”,表示该选项后必须跟一个参数值。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。 
3) 单个字符后跟两个冒号”:”,表示该选项后必须跟一个参数。                                                                        
参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩展)。

 

  1. #include   
  2. #include   
  3.   
  4. int main(int argc,char *argv[])   
  5. {   
  6.   int ch;   
  7.   opterr=0;   
  8.   
  9.   while((ch=getopt(argc,argv,"a:b::cde"))!=-1)   
  10.   {   
  11.     printf("/n/n/n");   
  12.     printf("optind:%d/n",optind);   
  13.     printf("optarg:%s/n",optarg);   
  14.     printf("ch:%c/n",ch);   
  15.     switch(ch)   
  16.     {     
  17.       case 'a':   
  18.         printf("option a:'%s'/n",optarg);   
  19.         break;   
  20.       case 'b':   
  21.         printf("option b:'%s'/n",optarg);   
  22.         break;   
  23.       case 'c':   
  24.         printf("option c/n");   
  25.         break;   
  26.       case 'd':   
  27.         printf("option d/n");   
  28.         break;   
  29.       case 'e':   
  30.         printf("option e/n");   
  31.         break;   
  32.       default:  
  33.   
  34.         printf("other option:%c/n",ch);   
  35.     }   
  36.     printf("optopt+%c/n",optopt);   
  37.   }   
  38. }  













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是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。

范例 

  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3.  
  4. int main(int argc, int *argv[])  
  5. {  
  6.         int ch;  
  7.         opterr = 0;  
  8.         while ((ch = getopt(argc,argv,"a:bcde"))!=-1)  
  9.         {  
  10.                 switch(ch)  
  11.                 {  
  12.                         case 'a':  
  13.                                 printf("option a:'%s'\n",optarg);  
  14.                                 break;  
  15.                         case 'b':  
  16.                                 printf("option b :b\n");  
  17.                                 break;  
  18.                         default:  
  19.                                 printf("other option :%c\n",ch);  
  20.                 }  
  21.         }  
  22.         printf("optopt +%c\n",optopt);  
  23. }  

执行:

  1. $ ./getopt -a  
  2. other option :?  
  3. optopt +a  
  4. $ ./getopt -b  
  5. option b :b  
  6. optopt +  
  7. $ ./getopt -c  
  8. other option :c  
  9. optopt +  
  10. $ ./getopt -d  
  11. other option :d  
  12. optopt +  
  13. $ ./getopt -abcd  
  14. option a:'bcd' 
  15. optopt +  
  16. $ ./getopt -bcd  
  17. option b :b  
  18. other option :c  
  19. other option :d  
  20. optopt +  
  21. $ ./getopt -bcde  
  22. option b :b  
  23. other option :c  
  24. other option :d  
  25. other option :e  
  26. optopt +  
  27. $ ./getopt -bcdef  
  28. option b :b  
  29. other option :c  
  30. other option :d  
  31. other option :e  
  32. other option :?  
  33. optopt +f  

 


05-26
`getopt` 是一个用于解析命令行参数的 C 函数,它可以帮助程序员在命令行中获取输入的参数。 `getopt` 会检查命令行参数中以单破折线(-)或双破折线(--)开头的选项。如果选项后面需要跟一个参数,则可以使用空格或等号来分隔选项和参数。例如,使用 `-o output.txt` 或 `--output=output.txt` 来指定输出文件名。 `getopt` 函数的使用需要包含头文件 `getopt.h`。常用的函数包括 `getopt` 和 `getopt_long`。`getopt` 用于解析简单的命令行选项,而 `getopt_long` 则支持更复杂的选项,例如长选项名(long options)和选项的描述信息。 下面是一个示例代码,演示了如何使用 `getopt` 函数解析命令行参数: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char **argv) { int opt; char *output_file = NULL; while ((opt = getopt(argc, argv, "o:")) != -1) { switch (opt) { case 'o': output_file = optarg; break; default: fprintf(stderr, "Usage: %s [-o output_file]\n", argv[0]); exit(EXIT_FAILURE); } } printf("Output file: %s\n", output_file ? output_file : "stdout"); return 0; } ``` 在上面的代码中,我们使用 `getopt` 函数来解析命令行参数。选项字符串 `"o:"` 表示程序支持一个 `-o` 选项,该选项需要一个参数。如果解析成功,`getopt` 函数返回该选项的字符表示(即 `'o'`),并将选项参数保存在全局变量 `optarg` 中。如果解析失败,`getopt` 函数返回 `-1`。 程序中还使用了标准的错误输出 `stderr` 和退出函数 `exit` 来处理错误情况。最后,程序输出解析结果,并退出。 注意,该示例代码仅用于演示 `getopt` 函数的基本用法,实际应用中可能需要更复杂的选项解析和错误处理逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值