本文來(lái)自:http://blog.csdn.net/yui/article/details/5669922
函數(shù)說(shuō)明
#include函數(shù)說(shuō)明:函數(shù)getopt用來(lái)解析命令行參數(shù)。函數(shù)getopt_long支持長(zhǎng)選項(xiàng)的命令行解析。函數(shù)原型:intgetopt_long(int argc, char* constargv[],                      const char*optstring,                     const struct option*longopts,                      int*longindex);參數(shù):argc、argv直接從main函數(shù)中獲取。opting是選項(xiàng)參數(shù)組成的字符串,由下列元素組成:1.單個(gè)字符,表示選項(xiàng),2.單個(gè)字符后接一個(gè)冒號(hào):表示該選項(xiàng)后必須跟一個(gè)參數(shù)。參數(shù)緊跟在選項(xiàng)后或者以空格隔開(kāi)。該參數(shù)的指針賦給optarg。3.單個(gè)字符后跟兩個(gè)冒號(hào),表示該選項(xiàng)后可以有參數(shù)也可以沒(méi)有參數(shù)。如果有參數(shù),參數(shù)必須緊跟在選項(xiàng)后不能以空格隔開(kāi)。該參數(shù)的指針賦給optarg。(這個(gè)特性是GNU的擴(kuò)張)。optstring是一個(gè)字符串,表示可以接受的參數(shù)。例如,"a:b:cd",表示可以接受的參數(shù)是a,b,c,d,其中,a和b參數(shù)后面跟有更多的參數(shù)值。(例如:-ahost -b name)longopts是一個(gè)結(jié)構(gòu)的實(shí)例:structoption{   constchar *name; //name表示的是長(zhǎng)參數(shù)名   inthas_arg; //has_arg有3個(gè)值,no_argument(或者是0),表示該參數(shù)后面不跟參數(shù)值              //required_argument(或者是1),表示該參數(shù)后面一定要跟個(gè)參數(shù)值              //optional_argument(或者是2),表示該參數(shù)后面可以跟,也可以不跟參數(shù)值   int*flag;   //用來(lái)決定getopt_long()的返回值是什么。              //flag是null,則函數(shù)會(huì)返回與該項(xiàng)option匹配的val值。   int val;     //和flag聯(lián)合決定返回值 };int *flag 如果這個(gè)指針為NULL,那么getopt_long()返回該結(jié)構(gòu)val字段中的數(shù)值。如果該指針不NULL,getopt_long()會(huì)使得它所指向的變量中填入val字段中的數(shù)值,并且getopt_long()返回0。如果flag不是NULL,但未發(fā)現(xiàn)長(zhǎng)選項(xiàng),那么它所指向的變量的數(shù)值不變。int val 這個(gè)值是發(fā)現(xiàn)了長(zhǎng)選項(xiàng)時(shí)的返回值,或者flag不是NULL時(shí)載入*flag中的值。典型情況下,若flag不是NULL,那么val是個(gè)真/假值,譬如1或0;另一方面,如果flag是NULL,那么val通常是字符常量,若長(zhǎng)選項(xiàng)與短選項(xiàng)一致,那么該字符常量應(yīng)該與optstring中出現(xiàn)的這個(gè)選項(xiàng)的參數(shù)相同。=========================================================================給個(gè)例子:struct option long_options[] ={          {"a123", required_argument,0, 'a'},          {"c123", no_argument, 0,'c'},};現(xiàn)在,如果命令行的參數(shù)是-a123,那么調(diào)用getopt_long()將返回字符'a',并且將字符串123由optarg返回(注意注意!字符串123由optarg帶回!optarg不需要定義,在getopt.h中已經(jīng)有定義)。那么,如果命令行參數(shù)是-c,那么調(diào)用getopt_long()將返回字符'c',而此時(shí),optarg是null。最后,當(dāng)getopt_long()將命令行所有參數(shù)全部解析完成后,返回-1。===========================================================================以上來(lái)自網(wǎng)絡(luò),便于學(xué)習(xí)記憶摘錄在這里,下面是自己的理解:函數(shù)原型:int getopt_long(intargc, char* const argv[],                      const char*optstring,                     const struct option*longopts,                      int*longindex);其中optstring為單個(gè)字符參數(shù),稱為short_opts。而longopts為多個(gè)字符(即一個(gè)或多個(gè)單詞連接)參數(shù),稱為long_opts。參數(shù)longindex為longopts數(shù)組中的索引返回值。具體用法參考suricata中main()函數(shù)中解析命令行參數(shù)://短字符參數(shù)charshort_opts[] = "c:TDhi:l:q:d:r:us:S:U:VF:";//長(zhǎng)字符參數(shù) structoption long_opts[] = {       {"dump-config", 0, &dump_config, 1},  // getopt_long返回值為0,dump_config保存為1       {"pfring", optional_argument, 0, 0},  // getopt_long返回值為0       {"pfring-int", required_argument, 0, 0}, // getopt_long返回值為0,必須有參數(shù)       {"pfring-cluster-id", required_argument, 0,0},       {"pfring-cluster-type", required_argument, 0,0},       {"af-packet", optional_argument, 0, 0},       {"pcap", optional_argument, 0, 0},       {"pcap-buffer-size", required_argument, 0,0},       {"unittest-filter", required_argument, 0,'U'},// getopt_long返回值為‘U’,必須有參數(shù)       {"list-app-layer-PRotos", 0,&list_app_layer_protocols, 1},       {"list-unittests", 0, &list_unittests,1},       {"list-cuda-cards", 0, &list_cuda_cards,1},       {"list-runmodes", 0, &list_runmodes,1},       {"list-keyWords", optional_argument,&list_keywords, 1},       {"runmode", required_argument, NULL, 0},       {"engine-analysis", 0, &engine_analysis,1},       {"pidfile", required_argument, 0, 0},       {"init-errors-fatal", 0, 0, 0},       {"fatal-unittests", 0, 0, 0},       {"user", required_argument, 0, 0},       {"group", required_argument, 0, 0},       {"erf-in", required_argument, 0, 0},       {"dag", required_argument, 0, 0},       {"napatech", 0, 0, 0},       {"build-info", 0, &build_info, 1},       {NULL, 0, NULL, 0}