本文來自:http://blog.csdn.net/yui/article/details/5669922
函數說明
#include函數說明:函數getopt用來解析命令行參數。函數getopt_long支持長選項的命令行解析。函數原型:intgetopt_long(int argc, char* constargv[], const char*optstring, const struct option*longopts, int*longindex);參數:argc、argv直接從main函數中獲取。opting是選項參數組成的字符串,由下列元素組成:1.單個字符,表示選項,2.單個字符后接一個冒號:表示該選項后必須跟一個參數。參數緊跟在選項后或者以空格隔開。該參數的指針賦給optarg。3.單個字符后跟兩個冒號,表示該選項后可以有參數也可以沒有參數。如果有參數,參數必須緊跟在選項后不能以空格隔開。該參數的指針賦給optarg。(這個特性是GNU的擴張)。optstring是一個字符串,表示可以接受的參數。例如,"a:b:cd",表示可以接受的參數是a,b,c,d,其中,a和b參數后面跟有更多的參數值。(例如:-ahost -b name)longopts是一個結構的實例:structoption{ constchar *name; //name表示的是長參數名 inthas_arg; //has_arg有3個值,no_argument(或者是0),表示該參數后面不跟參數值 //required_argument(或者是1),表示該參數后面一定要跟個參數值 //optional_argument(或者是2),表示該參數后面可以跟,也可以不跟參數值 int*flag; //用來決定getopt_long()的返回值是什么。 //flag是null,則函數會返回與該項option匹配的val值。 int val; //和flag聯合決定返回值 };int *flag 如果這個指針為NULL,那么getopt_long()返回該結構val字段中的數值。如果該指針不NULL,getopt_long()會使得它所指向的變量中填入val字段中的數值,并且getopt_long()返回0。如果flag不是NULL,但未發現長選項,那么它所指向的變量的數值不變。int val 這個值是發現了長選項時的返回值,或者flag不是NULL時載入*flag中的值。典型情況下,若flag不是NULL,那么val是個真/假值,譬如1或0;另一方面,如果flag是NULL,那么val通常是字符常量,若長選項與短選項一致,那么該字符常量應該與optstring中出現的這個選項的參數相同。=========================================================================給個例子:struct option long_options[] ={ {"a123", required_argument,0, 'a'}, {"c123", no_argument, 0,'c'},};現在,如果命令行的參數是-a123,那么調用getopt_long()將返回字符'a',并且將字符串123由optarg返回(注意注意!字符串123由optarg帶回!optarg不需要定義,在getopt.h中已經有定義)。那么,如果命令行參數是-c,那么調用getopt_long()將返回字符'c',而此時,optarg是null。最后,當getopt_long()將命令行所有參數全部解析完成后,返回-1。===========================================================================以上來自網絡,便于學習記憶摘錄在這里,下面是自己的理解:函數原型:int getopt_long(intargc, char* const argv[], const char*optstring, const struct option*longopts, int*longindex);其中optstring為單個字符參數,稱為short_opts。而longopts為多個字符(即一個或多個單詞連接)參數,稱為long_opts。參數longindex為longopts數組中的索引返回值。具體用法參考suricata中main()函數中解析命令行參數://短字符參數charshort_opts[] = "c:TDhi:l:q:d:r:us:S:U:VF:";//長字符參數 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,必須有參數 {"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’,必須有參數 {"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}