众所周知,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()的实例。
#include <stdio.h>
#include <stdlib.h> #include <unistd.h> #include <getopt.h> void print_usage(const char *program_name) { printf("%s 1.0.0 (2010-06-13)\n", program_name); printf("This is a program decoding a BER encoded CDR file\n"); printf("Usage: %s -f <file_name> -o <output_name> [-c <config_name>] [-k <keyword>]\n", program_name); printf(" -f --file the CDR file to be decoded\n"); printf(" -o --output the output file in plain text format\n"); printf(" -c --config the description file of the CDR file, if not given, use default configuration\n"); printf(" -k --keyword the keyword to search, if not given, all records will be written into output file\n"); } int main(int argc, char *argv[]) { char *file_name = NULL; char *output_name = NULL; char *config_name = NULL; char *keyword = NULL; const char *short_opts = "hf:o:c:k:"; const struct option long_opts[] = { {"help", no_argument, NULL, 'h'}, {"file", required_argument, NULL, 'f'}, {"output", required_argument, NULL, 'o'}, {"config", required_argument, NULL, 'c'}, {"keyword", required_argument, NULL, 'k'}, {0, 0, 0, 0} }; int hflag = 0; int c; opterr = 0; while ( (c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1 ) { switch ( c ) { case 'h' : hflag = 1; break; case 'f' : file_name = optarg; break; case 'o' : output_name = optarg; break; case 'c' : config_name = optarg; break; case 'k' : keyword = optarg; break; case '?' : if ( optopt == 'f' || optopt == 'o' || optopt == 'c' || optopt == 'k' ) printf("Error: option -%c requires an argument\n", optopt); else if ( isprint(optopt) ) printf("Error: unknown option '-%c'\n", optopt); else printf("Error: unknown option character '\\x%x'\n", optopt); return 1; default : abort(); } } if ( hflag || argc == 1 ) { print_usage(argv[0]); return 0; } if ( !file_name ) { printf("Error: file name must be specified\n"); return 1; } if ( !output_name ) { printf("Error: output name must be specified\n"); return 1; } // if not setting default, Linux OK, but SunOS core dump if ( !config_name ) config_name = "(null)"; if ( !keyword ) keyword = "(null)"; printf("Parameters got: file_name = %s, output_name = %s, config_name = %s, keyword = %s\n", file_name, output_name, config_name, keyword); return 0; } 另外一个区别是,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.h
/* $NetBSD: getopt.h,v 1.7 2005/02/03 04:39:32 perry Exp $ */ |