getopt()

getopt被用来解析命令行选项参数。

#include <unistd.h>
      extern char *optarg;  //选项的参数指针
      extern int optind,   //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。 
      extern int opterr,  //当opterr=0时,getopt不向stderr输出错误信息。
      extern int optopt;  //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt 中,getopt返回'?’、
      int getopt(int argc, char * const argv[], const char *optstring);
 调用一次,返回一个选项。在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含选项的命令行参数。

首先说一下什么是选项,什么是参数。

1.单个字符,表示选项,

2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。

例如gcc -g -o test test.c ,其中g和o表示选项,test为选项o的参数。

上面是getopt()函数的基本含义,大家懂得了这些之后,我们一个例子加深一下理解。

例如我们这样调用getopt(argc, argv, "ab:c:de::");
从上面我们可以知道,选项a,d没有参数,选项b,c有一个参数,选项e有有一个参数且必须紧跟在选项后不能以空格隔开。getopt首先扫描argv[1]到argv[argc-1],并将选项及参数依次放到argv数组的最左边,非选项参数依次放到argv的最后边。

执行程序为:
                            5              
$ ./test file1 -a  -b -c code -d file2 -e file3
  扫描过程中,optind是下一个选项的索引, 非选项参数将跳过,同时optind增1。optind初始值为1。当扫描argv[1]时,为非选项参数,跳过,optind=2;扫描到-a选项时,下一个将要扫描的选项是-b,则optind更改为3;扫描到-b选项时,后面有参数(会认为-c为选项b的参数),optind=5,扫描到code非选项跳过optind=6;扫描到-d选项,后面没有参数,optind=7;扫描到file2非选项跳过optind=8;扫描到-e后面本来应该有参数,optind=9但是有空格所以e的参数为空。
 
扫描结束后,getopt会将argv数组修改成下面的形式
                    2                                       9
$ ./test  -a  -b  -c  -d  -e  file1  code  file2  file3
 
同时,optind会指向非选项的第一个参数,如上面,optind将指向file1
代码如下:

#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
   int aflag=0, bflag=0, cflag=0;
   int ch;
printf("optind:%d,opterr:%d\n",optind,opterr);
printf("--------------------------\n");
   while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
   {
       printf("optind: %d,argc:%d,argv[%d]:%s\n", optind,argc,optind,argv[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]);
}


执行结果:
shiqi@wjl-desktop:~/code$ vim getopt.c
shiqi@wjl-desktop:~/code$ gcc getopt.c -o g
shiqi@wjl-desktop:~/code$ ./g file1 -a -b -c code -d file2 -e file3
optind:1,opterr:1
--------------------------
optind: 3,argc:10,argv[3]:-b
HAVE option: -a

optind: 5,argc:10,argv[5]:code
HAVE option: -b
The argument of -b is -c

optind: 7,argc:10,argv[7]:file2
HAVE option: -d

optind: 9,argc:10,argv[9]:file3
HAVE option: -e
The argument of -e is (null)   

----------------------------
optind=6,argv[6]=file1        //while循环执行完后,optind=6
`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、付费专栏及课程。

余额充值