兴海北路

---男儿仗剑自横行
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

  • 随笔 - 85
  • 文章 - 0
  • 评论 - 17
  • 引用 - 0

常用链接

留言簿(6)

随笔分类

随笔档案

收藏夹

全是知识啊

搜索

  •  

最新评论

阅读排行榜

评论排行榜

C语言:getopt_long()函数的作用
    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,标识结束。在本例中,它应为以下的样子:
    const struct option long_options[] = {
        {"help", 0, NULL, 'h'},
        {"output", 1, NULL, 'o'},
        {"version", 0, NULL, 'v'},
        {NULL, 0, NULL, 0}
    };
     调用时需要把main的两个参数argc和argv以及上述两个数据结构传给getopt_long()函数。
#include <getopt.h>
#include 
<stdio.h>
#include 
<stdlib.h>

/* The name of this program. */
const char* program_name;

/* Prints usage information for this program to STREAM (typically
   stdout or stderr), and exit the program with EXIT_CODE. Does not
   return. 
*/

void print_usage(FILE* stream, int exit_code)
{
    fprintf(stream, 
"Usage: %s options [ inputfile ]\n"
        program_name);
    fprintf(stream, 
        
"  -h  --hlep          Display this usage information.\n"
        
"  -o  --output filename Write output to file.\n"
        
"  -v  --verbose         Print verbose message.\n");
    exit (exit_code);
}


/* Main program entry point. ARGC contains number of argument list
   elements; ARGV is an array of pointers to them. 
*/


int main(int argc, char *argv[])
{
    
int next_option;

    
/* A string listing valid short options letters. */
    
const char* const short_options = "ho:v";
    
    
/* An array describing valid long options. */
    
const struct option long_options[] = {
        
{"help"0, NULL, 'h'},
    
{"output"1, NULL, 'o'},
    
{"verbose"0, NULL, 'v'},
    
{NULL, 0, NULL, 0}    /* Required at end of array. */
    }
;

    
/* The name of the file to receive program output, or NULL for
     * standard output. 
     
*/

    
const char* output_filename = NULL;

    
/* Whether to display verbose messages. */
    
int verbose = 0;

    
/* Remember the name of the program, to incorporate in messages.
     * The name is stored in argv[0].
     
*/

    program_name 
= argv[0];

    
do {
        next_option 
= getopt_long (argc, argv, short_options,
        long_options, NULL);
    
switch (next_option)
    
{
        
case 'h':    /* -h or --help */
        
/* User has requested usage information. Print it to
         * standard output, and exit with exit code zero
         * (normal termination).
         
*/

        print_usage(stdout, 
0);

        
case 'o':    /* -o or --output */
        
/* This option takes an argument, the name of the
         * output file.
         
*/

        output_filename 
= optarg;
        
break;

        
case 'v':    /* -v or --verbose */
        verbose 
= 1;
        
break;

        
case '?':    /* The user specified an invalid option. */
        
/* Print usage information to standard error, and exit
         * with exit code one (indicating abnormal
         * termination).
         
*/

        print_usage(stderr, 
1);

        
case -1:    /* Done with options. */
        
break;

        
default:    /* Something else: unexpected. */
        abort();
    }

    }
 
    
while (next_option != -1);

    
/* Done with options. OPTINO points to first nonoption argument.
     * FOr demonstration purposes, print them if the verbose option
     * was specified.
     
*/

    
if (verbose)
    
{
        
int i;
    
for (i = optind; i < argc; ++i)
        printf(
"Argument: %s\n", argv[i]);
    }


    
/* The main program goes here. */
    
return 0;
}

   

posted on 2010-06-13 15:32 随意门 阅读(7742) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理