眾所周知,C程序的主函數(shù)有兩個參數(shù),其中,第一個參數(shù)是整型,可以獲得包括程序名字的參數(shù)個數(shù),第二個參數(shù)是字符數(shù)組指針或字符指針的指針,可以按順序獲得命令行上各個字符串參數(shù)。其原形是:
int main(int argc, char *argv[]);
或者
int main(int argc, char **argv);
如果有一個解析CDR的程序,名叫 destroy,負(fù)責(zé)將一個二進制格式的CDR文件轉(zhuǎn)換為文本文件,輸出的文本的樣式由另外一個描述文件定義,那么,命令行要求輸入的參數(shù)就有三個:CDR文件名、輸出文件名和描述文件名。其中,前兩個參數(shù)是必須輸入的,第三個的描述文件名可以不輸入,程序會自動采用默認(rèn)的輸出樣式。很自然,主函數(shù)的三個參數(shù)就應(yīng)該這樣排列:
./destroy cdr cdr.txt [cdr.desc]
這樣做在一般情況下不會有太大問題,問題來源于擴展性的需求。如果有一天,用戶要求解析程序能夠按關(guān)鍵字解析,只有含有關(guān)鍵字的CDR才能夠輸出。解決方法很簡單,只要在參數(shù)列表的最后,加上它就可以了。不過,這樣就使得原本可選的描述文件名變?yōu)楸仨気斎耄?/span>
./destroy cdr cdr.txt cdr.desc [keyword]
因為不改的話,你就不知道,第三個參數(shù)究竟是描述文件名,還是關(guān)鍵字?,F(xiàn)在還算好辦,如果以后陸續(xù)有增加參數(shù)的需求,關(guān)鍵字也變成必須輸入了,這個時候,如果要查找全部CDR,你還得定義一個“特殊的關(guān)鍵字”,告訴程序,把數(shù)據(jù)統(tǒng)統(tǒng)給我撈出來……
有鑒于此,在Unix/Linux的正式的項目上,程序員通常會使用getopt()或者getopt_long()來獲得輸入的參數(shù)。兩者的一個區(qū)別在于getopt()只支持短格式參數(shù),而getopt_long()既支持短格式參數(shù),又支持長格式參數(shù)。
短格式:./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()的項目,設(shè)計者可以按需要,方便地增加參數(shù),或者隨意地放置參數(shù)的先后次序,只需要在程序中判斷,哪些參數(shù)是必須的就可以了。關(guān)于這兩個函數(shù)的用法,大家可以上網(wǎng)搜索一下,不再累述。附件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 filen"); 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 decodedn"); printf(" -o --output the output file in plain text formatn"); printf(" -c --config the description file of the CDR file, if not given, use default configurationn"); printf(" -k --keyword the keyword to search, if not given, all records will be written into output filen"); } 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 argumentn", 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 specifiedn"); return 1; } if ( !output_name ) { printf("Error: output name must be specifiedn"); 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 = %sn", file_name, output_name, config_name, keyword); return 0; }