Welcome 微信登录

首页 / 操作系统 / Linux / Linux选项-getopt/getopt_long

一、命令行简介 解释分析命令行通常是所以程序的第一个任务,C语言通过argc和argv参数来访问它的命令行参数。 最简单的命令行处理技术可以通过if判断来表示,如下例:
  1. if(argc>1 && argv[1][0] =="-" && argv[1][1] =="h")  //判断命令行参数是否为-h   
  2. {   
  3.      do _ some thing();   
  4. }   
这样处理简单有序的命令行还可以,对于复杂的命令行处理显得有心无力,于是GNU提供两个函数专门用来处理命令行参数:getopt 和getopt_long 二、getopt函数 getopt() 函数声明如下:
  1. #include <unistd.h>     
  2. int getopt(int argc, char *const argv[], const char *optstring);    
  3.      
  4. extern char *optarg;    
  5.    
  6. extern int optind, opterr, optopt;    
说 明:函数的argc和argv参数通常直接从main()的参数直接传递而来。optstring是选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。当给定getopt()命令参数的数量 (argc)、指向这些参数的数组 (argv) 和选项字串 (optstring) 后,getopt() 将返回第一个选项,并设置一些全局变量。使用相同的参数再次调用该函数时,它将返回下一个选项,并设置相应的全局变量。如果不再有可识别的选项,将返回 -1,此任务就完成了。getopt()所设置的全家变量包括:optarg ---- 当前选项参数字符(如果有的话)optind ---- argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在
            argv[optind]至argv[argc-1]中可以找到。optopt ---- 用于当发现无效选项字符的时候,getopt函数或者返回 "?" 或者返回 ":" 字符,并且optopt包含了
            所发现的无效选项字符,或者当输入非法参数的时候,由optopt带回输入的非法参数(字符)opterr ---- 这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。另外:如果optstring参数的第一个字符是冒号,那么getopt会根据错误情况返回不同的字符:(1):当错误是无效选项,getopt返回 "?" (2):当错误是缺少选项参数,getopt返回 ":" (3): 无错误发生时,正常返回的是对应的字符!注:GNU getopt()第三个特点是optstring中的选项字符后面接两个冒号,就允许该选项有可选的选项参数。在选项参数不存在的情况下,GNU getopt()返回选项字符并将optarg设置为NULL。 例子:
  1. #include<stdio.h>   
  2. #include<unistd.h>   
  3. #include<getopt.h>   
  4. /*the variables bellow was define in getopt.h 
  5. extern char *optarg;  
  6. extern int optind, opterr, optopt; 
  7. */  
  8. char* para = ":ab:c";  
  9. void print_extern_val(void)  
  10. {  
  11.     printf("optarg=%s.optind=%d.opterr=%d.optopt=%d=%c[END]/n",  
  12.     optarg, optind, opterr, optopt, optopt);  
  13. }  
  14. int main(int argc,char* argv[])  
  15. {  
  16.       
  17.     int oc = -1;  
  18.       
  19.     char* b_input = NULL;  
  20.       
  21.     while((oc = getopt(argc,argv,para)) != -1)    
  22.     {         
  23.         switch(oc)        
  24.         {  
  25.               
  26.         case "a":         
  27.             printf("input para is a/n");              
  28.             print_extern_val();  
  29.             break;  
  30.               
  31.         case "b":     
  32.             b_input = optarg;         
  33.             printf("input para is b,and optarg is %s/n",b_input);     
  34.             print_extern_val();  
  35.             break;  
  36.               
  37.         case "c":             
  38.             printf("input para is c/n");          
  39.             print_extern_val();  
  40.             break;  
  41.               
  42.         case ":":             
  43.             printf("option %c requires an argument/n",optopt);    
  44.             print_extern_val();   
  45.             break;  
  46.               
  47.         case "?":             
  48.         default:  
  49.             printf("option %c is invalid:ignored/n",optopt);  
  50.             print_extern_val();  
  51.             break;            
  52.         }         
  53.     }  
  54.       
  55.     return 0;     
  56. }   
结果:     [root@localhost opts]# ./getopt -a      input para is a     optarg=(null).optind=2.opterr=1.optopt=0=[END]     [root@localhost opts]# ./getopt -a -b     input para is a     optarg=(null).optind=2.opterr=1.optopt=0=[END]     option b requires an argument     optarg=(null).optind=3.opterr=1.optopt=98=b[END]     [root@localhost opts]# ./getopt -a -b mingming     input para is a     optarg=(null).optind=2.opterr=1.optopt=0=[END]     input para is b,and optarg is mingming     optarg=mingming.optind=4.opterr=1.optopt=0=[END]     [root@localhost opts]# ./getopt -a -d     input para is a     optarg=(null).optind=2.opterr=1.optopt=0=[END]     option d is invalid:ignored     optarg=(null).optind=3.opterr=1.optopt=100=d[END]
  • 1
  • 2
  • 下一页
Ubuntu 10.04手动更新kernelLinux中NFS的配置及应用讲解相关资讯      Linux函数 
  • Linux C语言中gotoxy函数  (04月11日)
  • Linux进程之Fork函数  (04/16/2015 08:48:35)
  • Linux中getrusage的使用  (11/08/2014 07:07:38)
  • Linux内核中min和max的实现  (03月03日)
  • Linux下mmap函数的一个练习  (01/19/2015 21:11:21)
  • Linux下confstr与uname函数_获取C  (10/28/2014 20:23:36)
本文评论 查看全部评论 (0)
表情: 姓名: 字数