在c程序應(yīng)用程序執(zhí)行的時,常常配有一些參數(shù),如果參數(shù)少我們可以使用arvg,arvc來實現(xiàn),如果參數(shù)很多并且需要傳入?yún)?shù)復(fù)雜我們可以使用strcut option 來實現(xiàn)。
1. struct option介紹
struct option {
const char *name; //name表示的是長參數(shù)名
int has_arg;
//has_arg有3個值,no_argument(或者是0),表示該參數(shù)后面不跟參數(shù)值
// required_argument(或者是1),表示該參數(shù)后面一定要跟個參數(shù)值
// optional_argument(或者是2),表示該參數(shù)后面可以跟,也可以不跟參數(shù)值
int *flag;
//用來決定,getopt_long()的返回值到底是什么。如果這個指針為NULL,那么getopt_long()返回該結(jié)構(gòu)val字段中的數(shù)值。如果該指針不為NULL,getopt_long()會使得它所指向的變量中填入val字段中的數(shù)值,并且getopt_long()返回0。如果flag不是NULL,但未發(fā)現(xiàn)長選項,那么它所指向的變量的數(shù)值不變。
int val;
//和flag聯(lián)合決定返回值 這個值是發(fā)現(xiàn)了長選項時的返回值,或者flag不是 NULL時載入*flag中的值。典型情況下,若flag不是NULL,那么val是個真/假值,譬如1 或0;另一方面,如 果flag是NULL,那么val通常是字符常量,若長選項與短選項一致,那么該字符常量應(yīng)該與optstring中出現(xiàn)的這個選項的參數(shù)相同。
}
2. getopt_long 介紹
1、getopt_long 函數(shù)解析命令行參數(shù),argc、argv是調(diào)用main函數(shù)時傳入的參數(shù)。傳入的'-'開始的字符被解析為選項,getopt_long 一次執(zhí)行解析出一個option,如果循環(huán)執(zhí)行,可以將argv中的全部option解析出來;
2、在getopt_long 的執(zhí)行中,每次進入都會更新getopt_long變量,該變量指向下一個argv參數(shù);
3、如getopt_long 返回-1,表示argv[]中的所有選項被解析出,optind指向第一個非選項的argument元素;這里要注意,在getopt_long 執(zhí)行過程中會將單獨的argument交換到argv數(shù)組的后面,option選項提前,如:cmd -a file1 -b file2,如果a/b均為不帶參數(shù)的選項,這最終argv數(shù)組變?yōu)椋篶md -a -b file1 file2;
4、optstring指定選項合法的選項,一個字符代表一個選項,在字符后面加一個':'表示該選項帶一個參數(shù),字符后帶兩個':'表示該選項帶可選參數(shù)(參數(shù)可有可無),若有參數(shù),optarg指向該該參數(shù),否則optarg為0;
5、前面說了getopt_long 會進行argv順序的調(diào)整,但也可以通過設(shè)置optstring改變它的方式,這里有兩種:
1) 如果optstring的第一個參數(shù)是'+'或者POSIXLY_CORRECT被設(shè)置,則getopt_long 在原argv的順序上遇到第一個非選項就返回-1;
2) 如果optstring的第一個參數(shù)是'-',則會將所有的非選項當(dāng)選項處理,并且返回1,用字符代碼1表示該選項;
6、如果getopt_long 不能識別一個選項字符,它會打印一個錯誤消息到stderr上,并將該字符存放到optopt中,返回'?';調(diào)用程序可以設(shè)置opterr=0設(shè)置不打印錯誤信息;注意:要使能打印錯誤信息,optstring的第一個字符(或者在第一個字符是+/-之后)不能是':',否則也不會打印錯誤;
7、如果optstring中指定了option需要參數(shù),但在命令行沒有參數(shù),那么getopt_long 將返回'?',如果在optstring的第一個字符(或者在第一個字符是+/-之后)是':',那么將返回':';
3. 代碼示例
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <linux/fb.h>
#include <getopt.h>
#include <signal.h>
#include <errno.h>
#include "client.h"
#include "server.h"
int FRAME_PER_PACKET = 0;
static struct option long_options[] = {
{ "server", 0, 0, 's'},
{ "client_self", 0, 0, 'c'},
{ "client_send", 0, 0, 'd'},
{ "client_recv", 0, 0, 'v'},
{ "PER_FREAM_PACKET", 1, 0, 'p'},
{ "auto test from 1 to 10", 0, 0, 'A'},
{0, 0, 0, 0},
};
static char optstring[] = "scdvp:A";
#define print_opt_help(opt_index, help_str) \
do { \
printf("\t---%s\t\t-%c\t%s", long_options[opt_index].name, (char)long_options[opt_index].val, help_str); \
} while (0)
void usage() {
printf("\nUsage:\n");
print_opt_help(0, "action server\n");
print_opt_help(1, "action client self send and self recv\n");
print_opt_help(2, "action client send\n");
print_opt_help(3, "action client recv\n");
print_opt_help(4, "set PER_FREAM_PACKET num\n");
print_opt_help(5, "auto test from 1 to 10\n");
printf("\n\nExamples:\n");
printf("\t./net-test -s\t---\taction server\n");
printf("\t./net-test -c -p 10\t---\taction client PER_FREAM_PACKET is 10\n");
}
int main(int argc, char *argv[]){
int c = 0;
client_act client_act_t;
while(1){
c = getopt_long(argc, argv, optstring, long_options, NULL);
switch (c){
case 's':
printf("start server\n");
server_fun();
break;
case 'c':
printf("start client self send and recv test\n");
client_act_t = RECV_AND_SEND;
client_fun(client_act_t);
break;
case 'd':
printf("start client send test\n");
client_act_t = SEND_ONLY;
client_fun(client_act_t);
break;
case 'v':
printf("start client recv test\n");
client_act_t = RECV_ONLY;
client_fun(client_act_t);
break;
case 'A':
printf("start client auto test\n");
client_fun(client_act_t);
break;
case 'p':
FRAME_PER_PACKET = atoi(optarg);
printf("set PER_FREAM_PACKET is %d\n", FRAME_PER_PACKET);
break;
default :
usage();
return -1;
}
}
return 0;
}