下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 检察机关以制度保规范
- 扫码登记制度规范标准
- 煤炭装车制度规范
- 放心粮油规范管理制度
- 工程规范书籍管理制度
- 设计服务质量规范制度
- 工地口罩佩戴制度规范
- 展示道具制作合同范本
- 工厂固废处理协议合同
- 店铺租赁公司合同范本
- 2026年寒假作业实施方案(第二版修订):骐骥驰骋势不可挡【课件】
- 2025年中国药科大学马克思主义基本原理概论期末考试笔试真题汇编
- 2026年辽宁现代服务职业技术学院单招职业倾向性测试题库附答案
- 2025教资国考真题试卷及答案
- 广东省汕头市金平区2024-2025学年九年级上学期期末物理试题(含答案)
- 临床用血技术规范2025年版与2000年版对照学习课件
- 自然资源执法考试试题及答案
- 梅毒检验报告课件
- 2025秋冀人版(新教材)小学科学三年级上册知识点及期末测试卷及答案
- 医院感染管理年度报告
- 骨科主任述职报告
评论
0/150
提交评论