[Linux] GET OPT_Long

Use of command-line parameter functions under linux

struct option member analysis

< pre class="cpp">//{option name, whether parameters are required, if it is NULL, getopt_long returns val (usually set to short option) //If it is not NULL, getopt_long returns 0, and flag points to val //{ “get”,no_argument,&method,METHOD_GET}, ==” When the get option is matched, getopt_long returns 0, method=METHOD_GET //}

getopt_long parameter analysis

//912Vfr ?h==> -9 -1 -2 -V -f -r //t: p: c:==> -t 100 should be followed by the option A parameter//If it is a letter followed by two colons, it means that the parameter after the option is optional
#include #include #include < unistd.h>#include static void usage(void){ fprintf(stderr, "webbench [option]... URL\n" "-f|--force Don't wait for reply from server. \n" "-r|--reload Send reload request-Pragma: no-cache.\n"" -t|--time  Run benchmark for  seconds. Default 30.\n" "-p |--proxy  Use proxy server for request.\n" "-c|--clients  Run  HTTP clients at once. Default one.\n"" -9|--http09 Use HTTP/0.9 style requests.\n" "-1|--http10 Use HTTP/1.0 protocol.\n" "-2|--http11 Use HTTP/1.1 protocol.\n"" --get Use GET request method.\n" "--head Use HEAD request method.\n""- -options Use OPTIONS request method.\n" "--trace Use TRACE request method.\n"" -?|-h|--help This information.\n" "-V|--version Display program version.\ n" );}/* values ​​*/volatile int timerexpired=0;int speed=0;int failed=0;int bytes=0;/* globals */int http10=1; /* 0-http/0.9, 1 -http/1.0, 2-http/1.1 *//* Allow: GET, HEAD, OPTIONS, TRACE */#define METHOD_GET 0#define METHOD_HEAD 1#define METHOD_OPTIONS 2#define METHOD_TRACE 3#define PROGRAM_VERSION "1.5"in t method=METHOD_GET;int clients=1;int force=0;int force_reload=0;int proxyport=80;char *proxyhost=NULL;int benchtime=30;static const struct option long_options[]={ {"force", no_argument,&force,1}, {"reload",no_argument,&force_reload,1}, {"time",required_argument,NULL,'t'}, {"help",no_argument,NULL,'?'}, {"http09 ",no_argument,NULL,'9'}, {"http10",no_argument,NULL,'1'}, {"http11",no_argument,NULL,'2'}, {"get",no_argument,&method,METHOD_GET} , {"head",no_argument,&method,METHOD_HEAD}, {"options",no_argument,&method,METHOD_OPTIONS}, {"trace",no_argument,&method,METHOD_TRACE}, {"version",no_argument,NULL,'V'} , {"proxy",required_argument,NULL,'p'}, {"clients",required_argument,NULL,'c'}, {NULL,0,NULL,0}};int main(int argc,char *argv[ ]){ if(argc==1)//Without parameters {usage(); return 2;} int opt; int options_index=0; while((opt=getopt_long(argc,argv,"912Vfrt:p:c: ?h",long_options,&options_index))!=EOF) {switch(op t) {case 0: break; case'f': printf("f\n");break; case'r': printf("r\n");break; case '9': printf("9\ n");break; case '1': printf("1\n");break; case '2': printf("2\n");break; case'V': printf("V\n" );exit(0); case't': printf("t\n");break; case'p': printf("p\n");break; case':': case'h': case '?': usage();return 2;break; case'c': printf("c\n");break;}} return 0;}

share picture

share picture

Leave a Comment

Your email address will not be published.