CODE: ¨ 短形式:通常是由一個「-」加上一個字母組合而成。這種形式的好處是輸入快捷。 ¨ 長形式:通常是由兩個「-」加上一個單片語合而成。這種形式的好處是形象、好記、直觀。 |
CODE: 短形式 長形式 含義 -h ??help 列印幫助信息 -s ??server 伺服器IP地址 -b ??background 後台執行 |
CODE: const struct option long_options [] =={ {「help 」,0,NULL,『h 』 }}, {「server 」,1,NULL,『s 』 }}, {「background」,0,NULL,『b 』 }}, {NULL,0,NULL,0 } }; |
CODE: #include #include #include const char*program_name; void print_usage (FILE*stream,int exit_code) { fprintf (stream,「Usage:%s options [ inputfile ....] 」,program_name); fprintf (stream, 「 -h --help Display this usage information. 」 「 -s --server ip Set the server IP. 」 「 -b --background run in the backgound. 」); exit (exit_code); } int main (int argc,char*argv []) { int next_option; const char*const short_options =「hs:b 」; const struct option long_options [] =={ {「help 」,0,NULL,『h 』 }}, {「server」,1,NULL,『s 』 }}, {「background 」,0,NULL,『b 』 }}, {NULL,0,NULL,0 } }; const char*output_filename =NULL; int verbose =0; program_name =argv [0 ]; do { next_option =getopt_long (argc,argv,short_options, long_options,NULL); switch (next_option) { case 『h 』:/*-h or --help */ print_usage (stdout,0); case 『s 』:/*-o or --output */ //ip =optarg; break; case 『b 』:/*-v or --verbose */ break; case 『?』:/*The user specified an invalid option.*/ code one (indicating abnormal termination).*/ print_usage (stderr,1); case -1:/*Done with options.*/ break; default:/*Something else:unexpected.*/ abort (); } } while (next_option !=-1); /*The main program goes here.*/ return 0; } |
[火星人 ] Linux程序的命令行處理已經有265次圍觀