c语言getopt long,使用getopt_long()从命令行获取参数

众所周知,C程序的主函数有两个参数,其中,第一个参数是整型,可以获得包括程序名字的参数个数,第二个参数是字符数组指针或字符指针的指针,可以按顺序获得命令行上各个字符串参数。其原形是:

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

或者

int main(int argc, char **argv);

如果有一个解析CDR的程序,名叫destroy,负责将一个二进制格式的CDR文件转换为文本文件,输出的文本的样式由另外一个描述文件定义,那么,命令行要求输入的参数就有三个:CDR文件名、输出文件名和描述文件名。其中,前两个参数是必须输入的,第三个的描述文件名可以不输入,程序会自动采用默认的输出样式。很自然,主函数的三个参数就应该这样排列:

./destroy cdr cdr.txt [cdr.desc]

这样做在一般情况下不会有太大问题,问题来源于扩展性的需求。如果有一天,用户要求解析程序能够按关键字解析,只有含有关键字的CDR才能够输出。解决方法很简单,只要在参数列表的最后,加上它就可以了。不过,这样就使得原本可选的描述文件名变为必须输入:

./destroy cdr cdr.txt cdr.desc [keyword]

因为不改的话,你就不知道,第三个参数究竟是描述文件名,还是关键字。现在还算好办,如果以后陆续有增加参数的需求,关键字也变成必须输入了,这个时候,如果要查找全部CDR,你还得定义一个“特殊的关键字”,告诉程序,把数据统统给我捞出来……

有鉴于此,在Unix/Linux的正式的项目上,程序员通常会使用getopt()或者getopt_long()来获得输入的参数。两者的一个区别在于getopt()只支持短格式参数,而getopt_long()既支持短格式参数,又支持长格式参数。

短格式:./destroy -f cdr -o cdr.txt -c cdr.desc -k 123456

长格式:./destroy --file cdr --output cdr.txt --config cdr.desc --keyword 123456

引入了getopt()和getopt_long()的项目,设计者可以按需要,方便地增加参数,或者随意地放置参数的先后次序,只需要在程序中判断,哪些参数是必须的就可以了。关于这两个函数的用法,大家可以上网搜索一下,不再累述。附件destroy_linux.c给出了在Linux下使用getopt_long()的实例。

cbef093dcc044b2793832001e2365e43.png#includecbef093dcc044b2793832001e2365e43.png#includecbef093dcc044b2793832001e2365e43.png#includecbef093dcc044b2793832001e2365e43.png#includecbef093dcc044b2793832001e2365e43.png

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.pngvoidprint_usage(constchar*program_name){

df37983f39daa189b8c814e01a6a9011.png    printf("%s 1.0.0 (2010-06-13)\n", program_name);

df37983f39daa189b8c814e01a6a9011.png    printf("This is a program decoding a BER encoded CDR file\n");

df37983f39daa189b8c814e01a6a9011.png    printf("Usage: %s -f  -o  [-c ] [-k ]\n", program_name);

df37983f39daa189b8c814e01a6a9011.png    printf("-f --file       the CDR file to be decoded\n");

df37983f39daa189b8c814e01a6a9011.png    printf("-o --output     the output file in plain text format\n");

df37983f39daa189b8c814e01a6a9011.png    printf("-c --config     the description file of the CDR file, if not given, use default configuration\n");

df37983f39daa189b8c814e01a6a9011.png    printf("-k --keyword    the keyword to search, if not given, all records will be written into output file\n");

0ac3a2d53663ec01c7f7225264eeefae.png}cbef093dcc044b2793832001e2365e43.png

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.pngintmain(intargc,char*argv[]){

df37983f39daa189b8c814e01a6a9011.pngchar*file_name=NULL;

df37983f39daa189b8c814e01a6a9011.pngchar*output_name=NULL;

df37983f39daa189b8c814e01a6a9011.pngchar*config_name=NULL;

df37983f39daa189b8c814e01a6a9011.pngchar*keyword=NULL;

df37983f39daa189b8c814e01a6a9011.png

df37983f39daa189b8c814e01a6a9011.pngconstchar*short_opts="hf:o:c:k:";

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngconststructoption long_opts[]={

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"help", no_argument, NULL,'h'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"file", required_argument, NULL,'f'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"output", required_argument, NULL,'o'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"config", required_argument, NULL,'c'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"keyword", required_argument, NULL,'k'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{0,0,0,0}4a5daaec04350a363f186a4d2c5ed6ce.png    };

df37983f39daa189b8c814e01a6a9011.pnginthflag=0;

df37983f39daa189b8c814e01a6a9011.png

df37983f39daa189b8c814e01a6a9011.pngintc;

df37983f39daa189b8c814e01a6a9011.png    opterr=0;

df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngwhile( (c=getopt_long(argc, argv, short_opts, long_opts, NULL))!=-1){

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngswitch( c ){

df37983f39daa189b8c814e01a6a9011.pngcase'h':

df37983f39daa189b8c814e01a6a9011.png                hflag=1;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'f':

df37983f39daa189b8c814e01a6a9011.png                file_name=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'o':

df37983f39daa189b8c814e01a6a9011.png                output_name=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'c':

df37983f39daa189b8c814e01a6a9011.png                config_name=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'k':

df37983f39daa189b8c814e01a6a9011.png                keyword=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'?':

df37983f39daa189b8c814e01a6a9011.pngif( optopt=='f'||optopt=='o'||optopt=='c'||optopt=='k')

df37983f39daa189b8c814e01a6a9011.png                    printf("Error: option -%c requires an argument\n", optopt);

df37983f39daa189b8c814e01a6a9011.pngelseif( isprint(optopt) )

df37983f39daa189b8c814e01a6a9011.png                    printf("Error: unknown option '-%c'\n", optopt);

df37983f39daa189b8c814e01a6a9011.pngelsedf37983f39daa189b8c814e01a6a9011.png                    printf("Error: unknown option character '\\x%x'\n", optopt);

df37983f39daa189b8c814e01a6a9011.pngreturn1;

df37983f39daa189b8c814e01a6a9011.pngdefault:

df37983f39daa189b8c814e01a6a9011.png                abort();

4a5daaec04350a363f186a4d2c5ed6ce.png        }4a5daaec04350a363f186a4d2c5ed6ce.png    }df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngif( hflag||argc==1){

df37983f39daa189b8c814e01a6a9011.png        print_usage(argv[0]);

df37983f39daa189b8c814e01a6a9011.pngreturn0;

4a5daaec04350a363f186a4d2c5ed6ce.png    }f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngif(!file_name ){

df37983f39daa189b8c814e01a6a9011.png        printf("Error: file name must be specified\n");

df37983f39daa189b8c814e01a6a9011.pngreturn1;

4a5daaec04350a363f186a4d2c5ed6ce.png    }f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngif(!output_name ){

df37983f39daa189b8c814e01a6a9011.png        printf("Error: output name must be specified\n");

df37983f39daa189b8c814e01a6a9011.pngreturn1;

4a5daaec04350a363f186a4d2c5ed6ce.png    }df37983f39daa189b8c814e01a6a9011.png

df37983f39daa189b8c814e01a6a9011.png//if not setting default, Linux OK, but SunOS core dumpdf37983f39daa189b8c814e01a6a9011.pngif(!config_name ) config_name="(null)";

df37983f39daa189b8c814e01a6a9011.pngif(!keyword ) keyword="(null)";

df37983f39daa189b8c814e01a6a9011.png    printf("Parameters got: file_name = %s, output_name = %s, config_name = %s, keyword = %s\n", file_name, output_name, config_name, keyword);

df37983f39daa189b8c814e01a6a9011.pngreturn0;

0ac3a2d53663ec01c7f7225264eeefae.png}

另外一个区别是,getopt()几乎通用于所有类Unix系统,而getopt_long()只有在GNU的Unix/Linux下才能用。如果把上述程序放到Tru64上编译,就会出现以下错误:

cc -o destroy destroy_linux.c

cc: Error: destroy_linux.c, line 24: In the initializer for long_opts, an array's element type is incomplete, which precludes its initialization. (incompelinit)

{"help", no_argument, NULL, 'h'},

----------------^

所以,如果一定要在Tru64等非GNU的OS上做到长格式的效果,除了自己另起炉灶之外,基本上只好借助一些跨平台的开源项目了。附件里的getopt_long.c和getopt.h是从opensolaris的网站上抄下来的,是包含在sg3_utils软件包中的程序。sg3_utils具体是什么,我也不知道,据说是一个Linux的开发包,用来直接使用SCSI命令集访问设备。(sg3_utils is a package of utilities for accessing devices that use SCSI command sets.)反正拿来能用就是了!

点击下载getopt_long

拿过来后,把他们放到与destroy_linux.c同一目录下,只需要把destroy_linux.c的头文件改一个地方,#include 改为#include “getopt.h”,就能够编译运行了。而且,这样改好后,不仅在Tru64上能运行,在Linux、SunOS上也能运行。

cbef093dcc044b2793832001e2365e43.png#includecbef093dcc044b2793832001e2365e43.png#includecbef093dcc044b2793832001e2365e43.png#includecbef093dcc044b2793832001e2365e43.png#include"getopt.h"cbef093dcc044b2793832001e2365e43.png

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.pngvoidprint_usage(constchar*program_name){

df37983f39daa189b8c814e01a6a9011.png    printf("%s 1.0.0 (2010-06-13)\n", program_name);

df37983f39daa189b8c814e01a6a9011.png    printf("This is a program decoding a BER encoded CDR file\n");

df37983f39daa189b8c814e01a6a9011.png    printf("Usage: %s -f  -o  [-c ] [-k ]\n", program_name);

df37983f39daa189b8c814e01a6a9011.png    printf("-f --file       the CDR file to be decoded\n");

df37983f39daa189b8c814e01a6a9011.png    printf("-o --output     the output file in plain text format\n");

df37983f39daa189b8c814e01a6a9011.png    printf("-c --config     the description file of the CDR file, if not given, use default configuration\n");

df37983f39daa189b8c814e01a6a9011.png    printf("-k --keyword    the keyword to search, if not given, all records will be written into output file\n");

0ac3a2d53663ec01c7f7225264eeefae.png}cbef093dcc044b2793832001e2365e43.png

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.pngintmain(intargc,char*argv[]){

df37983f39daa189b8c814e01a6a9011.pngchar*file_name=NULL;

df37983f39daa189b8c814e01a6a9011.pngchar*output_name=NULL;

df37983f39daa189b8c814e01a6a9011.pngchar*config_name=NULL;

df37983f39daa189b8c814e01a6a9011.pngchar*keyword=NULL;

df37983f39daa189b8c814e01a6a9011.png

df37983f39daa189b8c814e01a6a9011.pngconstchar*short_opts="hf:o:c:k:";

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngconststructoption long_opts[]={

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"help", no_argument, NULL,'h'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"file", required_argument, NULL,'f'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"output", required_argument, NULL,'o'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"config", required_argument, NULL,'c'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{"keyword", required_argument, NULL,'k'},

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.png{0,0,0,0}4a5daaec04350a363f186a4d2c5ed6ce.png    };

df37983f39daa189b8c814e01a6a9011.pnginthflag=0;

df37983f39daa189b8c814e01a6a9011.png

df37983f39daa189b8c814e01a6a9011.pngintc;

df37983f39daa189b8c814e01a6a9011.png    opterr=0;

df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngwhile( (c=getopt_long(argc, argv, short_opts, long_opts, NULL))!=-1){

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngswitch( c ){

df37983f39daa189b8c814e01a6a9011.pngcase'h':

df37983f39daa189b8c814e01a6a9011.png                hflag=1;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'f':

df37983f39daa189b8c814e01a6a9011.png                file_name=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'o':

df37983f39daa189b8c814e01a6a9011.png                output_name=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'c':

df37983f39daa189b8c814e01a6a9011.png                config_name=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'k':

df37983f39daa189b8c814e01a6a9011.png                keyword=optarg;

df37983f39daa189b8c814e01a6a9011.pngbreak;

df37983f39daa189b8c814e01a6a9011.pngcase'?':

df37983f39daa189b8c814e01a6a9011.pngif( optopt=='f'||optopt=='o'||optopt=='c'||optopt=='k')

df37983f39daa189b8c814e01a6a9011.png                    printf("Error: option -%c requires an argument\n", optopt);

df37983f39daa189b8c814e01a6a9011.pngelseif( isprint(optopt) )

df37983f39daa189b8c814e01a6a9011.png                    printf("Error: unknown option '-%c'\n", optopt);

df37983f39daa189b8c814e01a6a9011.pngelsedf37983f39daa189b8c814e01a6a9011.png                    printf("Error: unknown option character '\\x%x'\n", optopt);

df37983f39daa189b8c814e01a6a9011.pngreturn1;

df37983f39daa189b8c814e01a6a9011.pngdefault:

df37983f39daa189b8c814e01a6a9011.png                abort();

4a5daaec04350a363f186a4d2c5ed6ce.png        }4a5daaec04350a363f186a4d2c5ed6ce.png    }df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngif( hflag||argc==1){

df37983f39daa189b8c814e01a6a9011.png        print_usage(argv[0]);

df37983f39daa189b8c814e01a6a9011.pngreturn0;

4a5daaec04350a363f186a4d2c5ed6ce.png    }f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngif(!file_name ){

df37983f39daa189b8c814e01a6a9011.png        printf("Error: file name must be specified\n");

df37983f39daa189b8c814e01a6a9011.pngreturn1;

4a5daaec04350a363f186a4d2c5ed6ce.png    }f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngif(!output_name ){

df37983f39daa189b8c814e01a6a9011.png        printf("Error: output name must be specified\n");

df37983f39daa189b8c814e01a6a9011.pngreturn1;

4a5daaec04350a363f186a4d2c5ed6ce.png    }df37983f39daa189b8c814e01a6a9011.png

df37983f39daa189b8c814e01a6a9011.png//if not setting default, Linux OK, but SunOS core dumpdf37983f39daa189b8c814e01a6a9011.pngif(!config_name ) config_name="(null)";

df37983f39daa189b8c814e01a6a9011.pngif(!keyword ) keyword="(null)";

df37983f39daa189b8c814e01a6a9011.png    printf("Parameters got: file_name = %s, output_name = %s, config_name = %s, keyword = %s\n", file_name, output_name, config_name, keyword);

df37983f39daa189b8c814e01a6a9011.pngreturn0;

0ac3a2d53663ec01c7f7225264eeefae.png}

Linux下编译

-bash-3.2$ gcc -o destroy destroy.c getopt_long.c

短格式,全部输入

-bash-3.2$ ./destroy -f aaa -o aaa.txt -c ccc -k 222

Parameters got: file_name = aaa, output_name = aaa.txt, config_name = ccc, keyword = 222

前两个长格式,后两个短格式

-bash-3.2$ ./destroy --file aaa --output aaa.txt -c ccc -k 222

Parameters got: file_name = aaa, output_name = aaa.txt, config_name = ccc, keyword = 222

漏掉一个必须输入的参数会报错

-bash-3.2$ ./destroy -output aaa.txt

Error: file name must be specified

次序随意,长短混用

-bash-3.2$ ./destroy -c ccc -o aaa.txt -k 222 --file aaa

Parameters got: file_name = aaa, output_name = aaa.txt, config_name = ccc, keyword = 222

题外话,#include 与#include “filename.h”有什么区别,是面试C程序员经常问到的一个问题。答案大家都知道了,#include ,编译器从标准库路径搜索filename.h,而#include “filename.h”,编译器从用户的工作路径搜索filename.h。

此外,网上也有人说从glibc(http://sourceware.org/glibc/)上把getopt.h、getopt.c和getoptl.c拿过来也能够用。我也试过,但是不清楚什么原因不成功。

在这个小实验的过程中,还发现了C语言在各个OS下的一些细小差异,比如destroy.c里,79行到82行:

cbef093dcc044b2793832001e2365e43.png//if not setting default, Linux OK, but SunOS core dumpcbef093dcc044b2793832001e2365e43.pngif(!config_name ) config_name="(null)";

cbef093dcc044b2793832001e2365e43.pngif(!keyword ) keyword="(null)";

cbef093dcc044b2793832001e2365e43.pngprintf("Parameters got: file_name = %s, output_name = %s, config_name = %s, keyword = %s\n", file_name, output_name, config_name, keyword);

如果不设置空指针的默认值,Linux和Tru64都会自动帮你转换而避免运行时错误,但是SunOS不会,它会死给你看。

./destroy -f aaa -o aaa.txt

Segmentation Fault (core dumped)

再比如,第62行的abort()在头文件stdlib.h中定义,如果不包含此文件,SunOS与Tru64编译都没问题,Linux编译时会警告:

warning: incompatible implicit declaration of built-in function abort

由此看来,虽然C也公认是可移植性比较好的语言,但是在跨平台的项目中,也应该注意这些微小的差别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值