找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4563|回复: 0

C语言:getopt_long()函数的作用

[复制链接]
发表于 2012-3-9 23:20:52 | 显示全部楼层 |阅读模式
   Linux系统下,需要大量的命令行选项,如果自己手动解析他们的话实在是有违软件复用的思想,不过还好,GNU C library留给我们一个解析命令行的接口(X/Open规范),好好使用它可以使你的程序改观不少。
    使用getopt_long()需要引入头文件:#include<getopt.h>
    现在我们使用一个例子来说明它的使用。
    一个应用程序需要如下的短选项和长选项
    短选项       长选项                 作用
    -h           --help             输出程序命令行参数说明然后退出
    -o filename  --output filename  给定输出文件名
    -v           --version          显示程序当前版本后退出
    为了使用getopt_long()函数,我们需要先确定两个结构:
    1.一个字符串,包括所需要的短选项字符,如果选项后有参数,字符后加一个":"符号。本例中,这个字符串应该为"ho:v"。(因为-o后面有参数filename,所以字符后面需要加":")。
    2. 一个包含长选项字符串的结构体数组,每一个结构体包含4个域,第一个域为长选项字符串,第二个域是一个标识,只能为0或1,分别代表没有选项或有选项。第三个域永远为NULL。第四个选项域为对应的短选项字符串。结构体数组的最后一个元素全部位NULL和0,标识结束。在本例中,它应为以下的样子:
  1.    const struct option long_options[] = {
  2.         {"help", 0, NULL, 'h'},
  3.         {"output", 1, NULL, 'o'},
  4.         {"version", 0, NULL, 'v'},
  5.         {NULL, 0, NULL, 0}
  6.     };
复制代码
     调用时需要把main的两个参数argc和argv以及上述两个数据结构传给getopt_long()函数。
  1. #include <getopt.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /**//* The name of this program. */
  5. const char* program_name;
  6. /**//* Prints usage information for this program to STREAM (typically
  7.    stdout or stderr), and exit the program with EXIT_CODE. Does not
  8.    return. */
  9. void print_usage(FILE* stream, int exit_code)
  10. {
  11.     fprintf(stream, "Usage: %s options [ inputfile ]\n",
  12.         program_name);
  13.     fprintf(stream,
  14.         "  -h  --hlep          Display this usage information.\n"
  15.         "  -o  --output filename Write output to file.\n"
  16.         "  -v  --verbose         Print verbose message.\n");
  17.     exit (exit_code);
  18. }
  19. /**//* Main program entry point. ARGC contains number of argument list
  20.    elements; ARGV is an array of pointers to them. */
  21. int main(int argc, char *argv[])
  22. {
  23.     int next_option;
  24.     /**//* A string listing valid short options letters. */
  25.     const char* const short_options = "ho:v";
  26.    
  27.     /**//* An array describing valid long options. */
  28.     const struct option long_options[] = {
  29.         {"help", 0, NULL, 'h'},
  30.     {"output", 1, NULL, 'o'},
  31.     {"verbose", 0, NULL, 'v'},
  32.     {NULL, 0, NULL, 0}    /**//* Required at end of array. */
  33.     };
  34.     /**//* The name of the file to receive program output, or NULL for
  35.      * standard output.
  36.      */
  37.     const char* output_filename = NULL;
  38.     /**//* Whether to display verbose messages. */
  39.     int verbose = 0;
  40.     /**//* Remember the name of the program, to incorporate in messages.
  41.      * The name is stored in argv[0].
  42.      */
  43.     program_name = argv[0];
  44.     do {
  45.         next_option = getopt_long (argc, argv, short_options,
  46.         long_options, NULL);
  47.     switch (next_option)
  48.     {
  49.         case 'h':    /**//* -h or --help */
  50.         /**//* User has requested usage information. Print it to
  51.          * standard output, and exit with exit code zero
  52.          * (normal termination).
  53.          */
  54.         print_usage(stdout, 0);
  55.         case 'o':    /**//* -o or --output */
  56.         /**//* This option takes an argument, the name of the
  57.          * output file.
  58.          */
  59.         output_filename = optarg;
  60.         break;
  61.         case 'v':    /**//* -v or --verbose */
  62.         verbose = 1;
  63.         break;
  64.         case '?':    /**//* The user specified an invalid option. */
  65.         /**//* Print usage information to standard error, and exit
  66.          * with exit code one (indicating abnormal
  67.          * termination).
  68.          */
  69.         print_usage(stderr, 1);
  70.         case -1:    /**//* Done with options. */
  71.         break;
  72.         default:    /**//* Something else: unexpected. */
  73.         abort();
  74.     }
  75.     }
  76.     while (next_option != -1);
  77.     /**//* Done with options. OPTINO points to first nonoption argument.
  78.      * FOr demonstration purposes, print them if the verbose option
  79.      * was specified.
  80.      */
  81.     if (verbose)
  82.     {
  83.         int i;
  84.     for (i = optind; i < argc; ++i)
  85.         printf("Argument: %s\n", argv[i]);
  86.     }
  87.     /**//* The main program goes here. */
  88.     return 0;
  89. }
复制代码
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-29 19:32 , Processed in 0.011240 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表