命令行程序选项解析函数—getopt

命令行工具下的参数选项有两种,长选项短选项。短选项以-开头,后面跟单个字母;长选项以--开头,后面可跟多个字母。

 

功能:解析命令行短选项参数

函数原型:

#include <getopt.h>

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

几个外部变量

extern char *optarg;  

extern int optind, opterr, optopt;

optarg:若短选项后有参数,则optarg指向该参数

optind:扫描选项时,标识下一个选项的索引;扫描结束后,标识第一个非选项参数索引

opterr:出现不可识别的选项时,getopt将打印错误信息。将opterr设为0,可不打印错误信息。

optopt:存放不可识别的选项至optopt

 

1. 参数

argc:参数的个数(main)

argv:参数数组(main)

optstring:短选项字符集合,如 -i -n中的i,n

 

若选项后面有参数,则选项字符后加:, 对应的参数值保存在外部变量optarg中

如optstring 为"i:a",则表示程序支持两个短选项 -i arg和-a, -i后面须有参数值

当执行./a.out -i filename -a时,optarg指针就指向filename

 

2. 解析过程

getopt首先扫描argv[1]到argv[argc-1],并将选项及参数依次放到argv数组的最左边,非选项参数依次放到argv的最后边

如执行程序为:

     0     1   2  3  4  5  6   7  8  9 

$ ./mygetopt file1 -i infile -a -o outfile -v -h file2

 

扫描过程中,optind是下一个选项的索引, 非选项参数将跳过,同时optind增1。optind初始值为1。当扫描argv[1]时,为非选项参数,跳过,optind=2;扫描到-i选项时,后面有参数,下一个将要扫描的选项是-a,则optind更改为4;扫描到-a选项时,下一个选项是-o,optind=5;扫描到-o选项时,后面有参数,下一个选项是-v,optind=7;扫描到-v选项时,下一个选项是-h,optind=8;扫描到-h选项时,optind=9

 

扫描结束后,getopt会将argv数组修改成下面的形式

     0    1  2  3  4  5   6  7  8   9

$./mygetopt -i infile -a -o outfile -v -h file1 file2

 

同时,optind会指向非选项的第一个参数,如上面,optind将指向file1

 

 

3. 返回值

若getopt找到短选项字符,则返回该选项字符;

若出现不能接受的选项字符或丢失选项参数,则返回?,同时optopt将被设置成相应选项字符;

则后面没有选项字符,则返回-1

 

4. 测试

 mygetopt.c

  1. #include<stdio.h>  
  2. #include<getopt.h>  
  3.   
  4. void usage(const char *p);  
  5.   
  6. int main(int argc, char *argv[])  
  7. {  
  8.     int ch=0;  
  9.     opterr=0; // prevent error information to print for unrecognized options  
  10.     while( (ch=getopt(argc, argv, "i:ao:vh") ) != -1 )  
  11.      {  
  12.         switch(ch)  
  13.         {  
  14.            case 'i':  
  15.                 printf("option i: %s/n", optarg);  
  16.                 printf("optind=%d/n/n", optind);  
  17.                 break;  
  18.            case 'a':  
  19.                 printf("option a :%c/n", ch);  
  20.                 printf("optind=%d/n/n", optind);  
  21.                 break;  
  22.            case 'o':  
  23.                 printf("option o: %s/n", optarg);  
  24.                 printf("optind=%d/n/n", optind);  
  25.                 break;  
  26.            case 'v':  
  27.                 printf("option v: %c/n", ch);  
  28.                 printf("optind=%d/n/n", optind);  
  29.                 break;  
  30.            case 'h':  
  31.                 usage(argv[0]);  
  32.                 printf("optind=%d/n/n", optind);  
  33.                 break;  
  34.            default:  
  35.                 printf("unrecognized option: %c/n", optopt);  
  36.                 usage(argv[0]);  
  37.         }  
  38.      }  
  39.     if ( optind < argc )  
  40.        {  
  41.           printf("/nnon-option arguments below:/n");  
  42.           while( optind < argc )  
  43.             {  
  44.                 printf("%s/n", argv[optind++]);  
  45.             }  
  46.        }  
  47.     return 0;  
  48. }  
  49. void usage(const char *p)  
  50. {  
  51.    printf("Usage: %s [-i infile] [-a] [-o outfile] [-v] [-h] [file]/n", p);  
  52. }  

执行结果:

./mygetopt file1 -i infile -a -o outfile -v -h file2

option i: infile

optind=4

 

option a :a

optind=5

 

option o: outfile

optind=7

 

option v: v

optind=8

 

Usage: ./mygetopt [-i infile] [-a] [-o outfile] [-v] [-h] [file]

optind=9

 

non-option arguments below:

file1

file2    


转载自:http://blog.csdn.net/zhangyang0402/article/details/5671410

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值