
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 预备役介绍课件
- 健康饮食产业园项目运营管理方案(范文模板)
- 城市污水管网建设工程初步设计(参考模板)
- 2025年交流调频调压牵引装置项目建议书
- 五年级上册音乐教案(全集)
- 现场施工事故应急救援预案制度
- 2025年新型节能水泵、风机和压缩机项目建议书
- 2025年防沉剂项目发展计划
- 2025年高纯工艺系统设备分条机项目建议书
- 针对2025年工业污染源全面达标排放计划的实施方案的环境风险评估与管理001
- 紧急疏散培训课件
- 2024年石嘴山市直机关遴选和选调公务员笔试真题
- 17.2 直角三角形 课件
- 2025年安全教育培训考试应急救援知识题库高频试题
- 高等数学导数及其应用教案
- 2025年人教版小学六年级科学(下册)期末考卷附答案
- 高纯气体不锈钢管道施工方案
- 2008-2024年河南省信阳市固始县事业单位考试《公共基础知识》试卷及答案
- 商业联盟策划方案(3篇)
- 火力发电厂项目工程建设规范标准强制性条文执行表格电气分册
- 2025届高考英语新课标新增词汇+(共100个)+清单
评论
0/150
提交评论