生活随笔
收集整理的這篇文章主要介紹了
getopt和getopt_long函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
平時在寫程序時常常需要對命令行參數(shù)進行處理,當命令行參數(shù)個數(shù)較多時,如果按照順序一個一個定義參數(shù)含義很容易造成混亂,而且如果程序只按順序處理參數(shù)的話,一些“可選參數(shù)”的功能將很難實現(xiàn)。
在Linux中,我們可以使用getopt、getopt_long、getopt_long_only來對這個問題進行處理。
#include <unistd.h> int getopt (int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; #include <getopt.h> int getopt_long (int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only (int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
從最簡單的getopt講起,getopt函數(shù)的前兩個參數(shù),就是main函數(shù)的argc和argv,這兩者直接傳入即可,要考慮的就只剩下第三個參數(shù)。
optstring的格式舉例說明比較方便,例如:
? ? char *optstring = "abcd:";
上面這個optstring在傳入之后,getopt函數(shù)將依次檢查命令行是否指定了 -a, -b, -c及 -d(這需要多次調(diào)用getopt函數(shù),直到其返回-1),當檢查到上面某一個參數(shù)被指定時,函數(shù)會返回被指定的參數(shù)名稱(即該字母)
最后一個參數(shù)d后面帶有冒號,: 表示參數(shù)d是可以指定值的,如 -d 100 或 -d user。
optind表示的是下一個將被處理到的參數(shù)在argv中的下標值。
如果指定opterr = 0的話,在getopt、getopt_long、getopt_long_only遇到錯誤將不會輸出錯誤信息到標準輸出流。
#include <unistd.h> #include <stdlib.h> #include <stdio.h> int main (int argc, char *argv[]) { int opt; char *optstring = "a:b:c:d" ; while ((opt = getopt(argc, argv, optstring)) != -1 ) { printf ("opt = %c\n" , opt); printf ("optarg = %s\n" , optarg); printf ("optind = %d\n" , optind); printf ("argv[optind - 1] = %s\n\n" , argv[optind - 1 ]); } return 0 ; }
編譯上述程序并運行,有如下結(jié)果:
cashey@ubuntu:~/Desktop/getopt$ ./test_getopt -a 100 -b 200 -c admin -d opt = a optarg = 100 optind = 3 argv[optind - 1 ] = 100 opt = b optarg = 200 optind = 5 argv[optind - 1 ] = 200 opt = c optarg = admin optind = 7 argv[optind - 1 ] = admin opt = d optarg = (null) optind = 8 argv[optind - 1 ] = -d
下面來講getopt_long函數(shù),getopt_long函數(shù)包含了getopt函數(shù)的功能,并且還可以指定“長參數(shù)”(或者說長選項),與getopt函數(shù)對比,getopt_long比其多了兩個參數(shù):
? ? ? ?int getopt(int argc, char * const argv[], ? ? ? ? ? ? ? ? ? const char *optstring);
? ? ? ?int getopt_long(int argc, char * const argv[], ? ? ? ? ? ? ? ? ? const char *optstring,? ? ? ? ? ? ? ? ? const struct option *longopts, int *longindex );
在這里,longopts指向的是一個由option結(jié)構(gòu)體組成的數(shù)組,那個數(shù)組的每個元素,指明了一個“長參數(shù)”(即形如--name的參數(shù))名稱和性質(zhì):
? ? ? ? ? ?struct option { ? ? ? ? ? ? ? ?const char *name; ? ? ? ? ? ? ? ?int ? ? ? ? has_arg; ? ? ? ? ? ? ? ?int ? ? ? ?*flag; ? ? ? ? ? ? ? ?int ? ? ? ? val; ? ? ? ? ? ?}; ? ? ? ?name ?是參數(shù)的名稱 ? ? ? ?has_arg 指明是否帶參數(shù)值,其數(shù)值可選: ? ? ? ? ? ? ? no_argument (即 0) 表明這個長參數(shù)不帶參數(shù)(即不帶數(shù)值,如:--name) ? ? ? ? ? ? ? required_argument (即 1) 表明這個長參數(shù)必須帶參數(shù)(即必須帶數(shù)值,如:--name Bob) ? ? ? ? ? ? optional_argument(即2)表明這個長參數(shù)后面帶的參數(shù)是可選的,(即--name和--name Bob均可) ? ? ? ?flag ? 當這個指針為空的時候,函數(shù)直接將val的數(shù)值從getopt_long的返回值返回出去,當它非空時,val的值會被賦到flag指向的整型數(shù)中,而函數(shù)返回值為0 ? ? ? ?val ? ?用于指定函數(shù)找到該選項時的返回值,或者當flag非空時指定flag指向的數(shù)據(jù)的值。
另一個參數(shù)longindex,如果longindex非空,它指向的變量將記錄當前找到參數(shù)符合longopts里的第幾個元素的描述,即是longopts的下標值。
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <getopt.h> int main (int argc, char **argv) { int opt; int digit_optind = 0 ; int option_index = 0 ; char *optstring = "a:b:c:d" ; static struct option long_options [] = { {"reqarg" , required_argument, NULL , 'r' }, {"noarg" , no_argument, NULL , 'n' }, {"optarg" , optional_argument, NULL , 'o' }, {0 , 0 , 0 , 0 } }; while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1 ) { printf ("opt = %c\n" , opt); printf ("optarg = %s\n" , optarg); printf ("optind = %d\n" , optind); printf ("argv[optind - 1] = %s\n" , argv[optind - 1 ]); printf ("option_index = %d\n" , option_index); } return 0 ; }
編譯運行以上程序并運行,可以得到以下結(jié)果:
cashey@ubuntu:~/Desktop/getopt $ ./test_getopt_long -a 100 --reqarg 100 --nonarg opt = a optarg = 100 optind = 3 argv[optind - 1 ] = 100 option_index = 0 opt = r optarg = 100 optind = 5 argv[optind - 1 ] = 100 option_index = 0 ./test_getopt_long: unrecognized option '--nonarg' opt = ? optarg = (null) optind = 6 argv[optind - 1 ] = --nonarg option_index = 0
當所給的參數(shù)存在問題時,opt(即函數(shù)返回值是'?'),如:
cashey@ubuntu:~/Desktop/getopt $ ./test_getopt_long -a ./test_getopt_long: option requires an argument -- 'a' opt = ? optarg = (null) optind = 2 argv[optind - 1 ] = -a option_index = 0 cashey@ubuntu:~/Desktop/getopt $ ./test_getopt_long --reqarg ./test_getopt_long: option '--reqarg' requires an argument opt = ? optarg = (null) optind = 2 argv[optind - 1 ] = --reqarg
最后說說getopt_long_only函數(shù),它與getopt_long函數(shù)使用相同的參數(shù)表,在功能上基本一致,只是getopt_long只將--name當作長參數(shù),但getopt_long_only會將--name和-name兩種選項都當作長參數(shù)來匹配。在getopt_long在遇到-name時,會拆解成-n -a -m -e到optstring中進行匹配,而getopt_long_only只在-name不能在longopts中匹配時才將其拆解成-n -a -m -e這樣的參數(shù)到optstring中進行匹配。
總結(jié)
以上是生活随笔 為你收集整理的getopt和getopt_long函数 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。