![Command Line Options- getopt_第1頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/31/2ccd4487-556b-4033-b6e4-8ff7dd94c96b/2ccd4487-556b-4033-b6e4-8ff7dd94c96b1.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、command line options: getoptilude getopt .h tern char *optarg ; extern int optind , opterr , optopt ; int getopt_long (int argc , char * const argv , const char *optsing , const struct option *longopts , int *longindex optarg : if an option takes an argument, the pointer will point at that argument.
2、 optind : is the index of the next element to be process in argv . the system initializes this value to 1 . the caller can , the optind value could be equal to argc or than argc . if optind argc, means there are verbose arguments left cannot be parse by getopt (). opterr, optopt : if getopt () does
3、not recognize an option character, it prints an error message to stderr , stores the character in optopt , and returns '? '. the calling program may prevent the error message by setting opterr . parameters argc : the count of input parameters, which passed to the main() function on program i
4、nvoion. argv : the array of input parameters, which passed to the main() function on program invocation. optstring : a character string containing the val short options, each a single letter. an option that requires an argument is followed by a on. for your program, the string ho:v indicates that th
5、e valid options are -h, -o, and -v, with the second of these options followed by an argument. if a character is followed by a colon, means the option requires an argument. for example: ho:v , means option o requires an argument, used as: "-o arg ". if a character is followed by two colons,
6、 means the option takes an optional argument. for example: ho:v , means option o takes an optional argument, used as: "-oarg ", or "-o ". we should notice that if we use "-o arg " under the circumstances, "arg " will not be parsed as the argument of option &qu
7、ot;o ". longopts : a pointer to the first element of an array of struct option declared in getopt.h as struct option const char *name; int has_arg; int *flag; int val; ; the meanings of the erent fields are: name is the name of the long option. has_arg is: no_argument (or 0) if the option does
8、not take an argument; required_argument (or 1) if the option requires an argument; optional_argument (or 2) if the option takes an optional argument. flag specifies how relts are returned for a long option. if flag is null , then getopt_long() returns val. (for example, the calling program may set v
9、al to the equivalent short option character.) otherwise, getopt_long() returns 0, and flag points to a variable which is set to val if the option is found, but left unchanged if the option is not found. val is the value to return, or to load into the variable pointed to by flag. longindex : if it is
10、 not null , it points to a variable which is set to the index of the long option relative to longopts . return value if an option was successfully found, then getopt() returns the option character. if all command-line options have been parsed, then getopt() returns -1 . if getopt() encounters an opt
11、ion character that was not in optstring, then '?' is returned. if getopt() encounters an option with a missing argument, then the return value depends on the first character in optstring: if it is ':', then ':' is returned; otherwise '?' is returned. after all command
12、-line options have been parsed, the optind value could be equal to argc or less than argc . if optind argc, means there are verbose arguments left which cannot be parse by getopt (). under the circumstances, these verbose arguments can be feached from the changed(by getopt() ) array argv from argvin
13、d to argvargc-1 sample code note: the following code is written under the stardard of c99, use blow command to compile: 280047shni145 code $ gcc -wall -std=gnu99 -g getopt.c -o getopt include getopt.h include stdio.h include stdlib.h /* the name of this program. */ const char* program_name; /* print
14、s usage infoation for this program to stream (typically stdout or stderr), * and exit program with exit_code. does not return. */ void print_usage(file* stream, int exit_code, const char* error_message) if(error_message != null) fprintf(stream, "%s n", error_message); fprintf(stream, "
15、;usage: %s options input .n", program_name); fprintf(stream, " -h -help display this usage information. n" " -f file -output filename write output to file. n" " -o arg -optional print optional argument. n"); exit(exit_code); void print_argument(const int option, co
16、nst char* argument) if(argument != null) printf("%c = %s n", option, argument); void print_verbose_arguments(const int argc, char* const* argv) for(int i=optind; i argc; i+) printf("verbose argument: %sn", argvi); void reset_optind(void) optind = 1; void parse_parameters(const in
17、t argc, char* const* argv, const char* const optstring, const struct option* longopts) int next_option = 0; if(argc = 1) print_usage(stdout, 0, null); ee reset_optind(); while(next_option = getopt_long(argc, argv, optstring, longopts, null), next_option != -1) if(next_option = 'h') print_usa
18、ge(stdout, 0, null); else if(next_option = 'f') print_argument(next_option, optarg); else if(next_option = 'o') print_argument(next_option, optarg); else if(next_option = '?') print_usage(stderr, 1, null); print_verbose_arguments(argc, argv); int main(int argc, char* argv) /*
19、 a string listing valid short options letters. */ const char* const short_options = "hf:o:" /* an array descri valid long options. */ const struct option long_options = .name="help", .has_arg=no_argument, .flag=null, .val='h' , .name="output", .has_arg=required_
20、argument, .flag=null, .val='f' , .name="option", .has_arg=optional_argument, .flag=null, .val='o' , .name=null, .has_arg=no_argument, .flag=null, .val=0 ; /* remember the name of the program, to incorporate in messages. * the name is stored in argv0. */ program_name = argv0
21、; parse_parameters(argc, argv, short_options, long_options); return 0; test 280047shni145 code $ ./getopt -h usage: ./getopt options inputfile . -h -help display this usage information. -f file -output filename write output to file. -o arg -optional print optional argument. 280047shni145 code $ ./getopt -f file f = file 280047shni145 code $ ./getopt -oarg o = arg 280047shni145 code $ ./getopt -o arg verbose argument: arg 280047shni145 code $ ./getopt 11 22 33 44 -f 55 -o
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 租房合租房合同范本04
- 項目委托合同
- 合作社向個人借款合同范本
- 煙霧探測器與噴淋系統(tǒng)
- 滅火器材的創(chuàng)新與發(fā)展趨勢
- 半年工作總結(jié)報告范文11篇
- 生態(tài)產(chǎn)品價值實現(xiàn)的研究熱點與展望
- 嬰幼兒、成人和老年皮膚結(jié)構(gòu)特點研究進展
- 基于情感認知理論的智能教育裝備CMF設(shè)計探析
- 密集雜波環(huán)境紅外目標檢測關(guān)鍵技術(shù)研究
- 公眾聚集場所消防技術(shù)標準要點
- 幼兒園員工手冊與規(guī)章制度
- 社團活動經(jīng)費預(yù)算申請表
- 經(jīng)營范圍登記規(guī)范表述目錄(試行)(V1.0.2版)
- 2023年山東省威海市中考物理真題(附答案詳解)
- 第八講 發(fā)展全過程人民民主PPT習(xí)概論2023優(yōu)化版教學(xué)課件
- 王崧舟:學(xué)習(xí)任務(wù)群與課堂教學(xué)變革 2022版新課程標準解讀解析資料 57
- 招投標現(xiàn)場項目經(jīng)理答辯(完整版)資料
- 運動競賽學(xué)課件
- 2022年上海市初中畢業(yè)數(shù)學(xué)課程終結(jié)性評價指南
- 高考作文備考-議論文對比論證 課件14張
評論
0/150
提交評論