getopt(),getopt_long(),getopt_long_only()

本文详细介绍了C库中的getopt和getopt_long函数的使用方法,包括参数解析、选项处理和错误处理等核心内容,旨在帮助开发者高效地处理命令行参数。

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

 原:http://blog.youkuaiyun.com/songqqnew/article/details/7006541

man 3 getopt
NAME
       getopt, getopt_long, getopt_long_only - Parse command-line options
       #include <unistd.h>
       int getopt(int argc, char * const argv[],
                  const char *optstring);
       extern char *optarg;
       extern int optind, opterr, optopt;
       #define _GNU_SOURCE

       The  getopt()  function parses the command-line arguments.  Its arguments argc and argv are the argument count and array as
       passed to the main() function on program invocation.  An element of argv that starts with ’-’ (and is not  exactly  "-"  or
       "--")  is  an  option  element.   The  characters  of  this element (aside from the initial ’-’) are option characters.  If
       getopt() is called repeatedly, it returns successively each of the option characters from each of the option elements.

[cpp]  view plain copy
  1. #include <unistd.h>    
  2. #include <stdlib.h>    
  3. #include <stdio.h>    
  4.   
  5. int    
  6. main(int argc, char *argv[])    
  7. {    
  8.     int  opt;    
  9.     char* short_options="nt:";  
  10.     while ((opt = getopt(argc, argv,short_options)) != -1) {    
  11.         switch (opt) {    
  12.         case 'n':    
  13.            printf("option n\n");    
  14.            break;    
  15.         case 't':    
  16.            printf("option t,optarg=%s\n",optarg);    
  17.            printf("str to int:%d\n",atoi(optarg));  
  18.            break;    
  19.         default:    
  20.            fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n",argv[0]);    
  21.         }    
  22.     }  
  23.     exit(EXIT_SUCCESS);    
  24. }  
[cpp]  view plain copy
  1. [root@localhost test]# ./test1 -t 66 -n  
  2. option t,optarg=66  
  3. str to int:66  
  4. option n  


********************************************************************

getopt_long的原型是int getopt_long(int argc, char * const argv[], 

        const char *optstring, 
        const struct option *longopts, 
        int *longindex);  


       The  getopt_long() function works like getopt() except that it also accepts long options, started with two dashes.  (If the
       program accepts only long options, then optstring should be specified as an empty string  (""),  not  NULL.)   Long  option
       names  may  be  abbreviated  if the abbreviation is unique or is an exact match for some defined option.  A long option may
       take a parameter, of the form --arg=param or --arg param.


       longopts is a pointer to the first element of an array of struct option declared in <getopt.h> as

           struct option {
               const char *name;
               int         has_arg;
               int        *flag;
               int         val;
           };

       The meanings of the different fields are:

       name   is the name of the long option.

       has_arg
              is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option  requires  an
              argument; or optional_argument (or 2) if the option takes an optional argument.

       flag   specifies  how results are returned for a long option.  If flag is NULL, then getopt_long() returns val.  (For exam-
              ple, the calling program may set val to the equivalent short option character.)  Otherwise, getopt_long() returns 0,
              and  flag  points  to a variable which is set to val if the option is found, but left unchanged if the option is not
              found.

       val    is the value to return, or to load into the variable pointed to by flag.

       The last element of the array has to be filled with zeroes.

       If longindex is not NULL, it points to a variable which is set to the index of the long option relative to longopts.

       getopt_long_only() is like getopt_long(), but ’-’ as well as ’--’ can indicate a long option.  If  an  option  that  starts
       with ’-’ (not ’--’) doesn’t match a long option, but does match a short option, it is parsed as a short option instead.


getopt_long例子参考
http://blog.youkuaiyun.com/ast_224/article/details/3861625

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值