
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 辛集中学高一上学期第三次阶段考试语文试题
- 干冰加水物理课件
- 献县第一中学语文复习每日悦读4
- 陕西中医药大学《中国现当代文学IV》2023-2024学年第二学期期末试卷
- 陕西咸阳武功县普集高级中学2025年高三高考模拟训练评估卷(4)数学试题含解析
- 安全用电小知识小学生
- 陕西汉中市汉台区县2025年高三下学期专项练习数学试题含解析
- 陕西省五校2025年高三年级下学期第二次月考试题含解析
- 陕西省实验中学2024-2025学年高三数学试题下学期期末考试试题(A卷)含解析
- 陕西省渭南市尚德中学2024-2025学年高三下学期物理试题试卷含解析
- 《心理B证论文:浅谈小学生自我监控能力的培养》3100字
- 切口引流管非计划拔管不良事件根本原因RCA分析
- 人工智能导论(天津大学)知到智慧树章节答案
- 产能提升改善报告
- 别墅电梯井的施工方案
- 《招商谈判技巧》课件
- 史宁中:义务教育数学课标(2022年版)解读
- 承包商管理制度
- 电动车二轮转让协议书范文范本
- 部编人教版小学4四年级《道德与法治》下册全册教案
- 新疆2022年中考数学试卷(含答案)
评论
0/150
提交评论