C/C++—— 分析命令行参数的getopt()函数使用介绍
2016-03-31 18:07
531 查看
函数介绍:
首先输入命令:man 3 getopt 查看getopt函数的man手册介绍#include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt;
函数说明 getopt()用来分析命令行参数。参数argc和argv分别代表参数个数和内容,跟main()函数的命令行参数是一样的。
下面用一个例子来输出main函数中的argc,argv[ ]内容:
#include <stdio.h> int main(int argc, char* argv[]) { int i = 0; printf("共%d个命令行参数\n", argc); for(i = 0; i < argc; ++i){ printf("第%d个参数是: %s\n", i+1, argv[i]); } return 0; }输出:
root@linux_ever:~/linux_ever/work_test# ./getopt 共1个命令行参数 第1个参数是: ./getopt root@linux_ever:~/linux_ever/work_test# ./getopt -a -h help 共4个命令行参数 第1个参数是: ./getopt 第2个参数是: -a 第3个参数是: -h 第4个参数是: help
参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,
如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。
如果在处理期间遇到了不符合optstring指定的其他选项getopt()将显示一个错误消息,并将全域变量optarg设为“?”字符,如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。
getopt() 所设置的全局变量包括:
optarg---指向当前选项的参数的指针(选项字母:后的参数)。
optind——再次调用 getopt() 时的下一个 argv 指针的索引。 optopt——最后一个未知选项。
返回值:
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 option 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.
大概意思就是:
如果一个选项成功被找到,则getopt返回该选项字符,比如a, b, c....
如果所有的选项都被找到,则getopt返回-1。
如果getopt函数遇到了一个在optstring参数中没有指定的选项,则返回'?'。
如果在-a之后忘了输出参数,则返回':'(此时optstring中有字符a:),如果不是a:,则返回'?'
optstring中的指定的内容的意义(例如getopt(argc, argv, "ab:c:de::");)
1.单个字符,表示选项,(如上例中的abcde各为一个选项)
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。(如上例中的b:c:)
3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。
该参数的指针赋给optarg。(如上例中的e::,如果没有跟参数,则optarg = NULL),但是在实例中测试却是::没什么作用。。
实例:
/************************************************************************* > File Name: getopt.c > Author: > Mail: > Created Time: 2016年03月31日 星期四 17时18分23秒 ************************************************************************/ #include <stdio.h> #include <unistd.h> int main(int argc, char* argv[]) { printf("*****************************************\n"); int i = 0; printf("共%d个命令行参数\n", argc); for(i = 0; i < argc; ++i){ printf("第%d个参数是: %s\n", i+1, argv[i]); } printf("*****************************************\n"); char ch; while((ch = getopt(argc, argv, "a:bc::h")) != -1){ //printf("optind = %d, optopt = %d\n", optind, optopt); switch(ch){ case 'a': printf("选项是%c, 选项内容: %s\n", ch, optarg); break; case 'b': printf("选项是%c, 选项内容: %s\n", ch, optarg); break; case 'c': printf("选项是%c, 选项内容: %s\n", ch, optarg); break; case 'h': printf("选项是%c\n", ch); printf("-a optstring\n"); printf("-b\n"); printf("-c optstring or NULL\n"); printf("-h"); break; default: printf("other option: %c\n", ch); break; } } return 0; }
输出:
root@linux_ever:~/linux_ever/work_test# ./getopt -h ***************************************** 共2个命令行参数 第1个参数是: ./getopt 第2个参数是: -h ***************************************** 选项是h -a optstring -b -c optstring or NULL
-hroot@linux_ever:~/linux_ever/work_test# ./getopt -a linux_ever ***************************************** 共3个命令行参数 第1个参数是: ./getopt 第2个参数是: -a 第3个参数是: linux_ever ***************************************** 选项是a, 选项内容: linux_ever
root@linux_ever:~/linux_ever/work_test# ./getopt -a linux_ever -b ***************************************** 共4个命令行参数 第1个参数是: ./getopt 第2个参数是: -a 第3个参数是: linux_ever 第4个参数是: -b ***************************************** 选项是a, 选项内容: linux_ever 选项是b, 选项内容: (null)
root@linux_ever:~/linux_ever/work_test# ./getopt -a linux_ever -b -c ever ***************************************** 共6个命令行参数 第1个参数是: ./getopt 第2个参数是: -a 第3个参数是: linux_ever 第4个参数是: -b 第5个参数是: -c 第6个参数是: ever ***************************************** 选项是a, 选项内容: linux_ever 选项是b, 选项内容: (null) 选项是c, 选项内容: (null)
相关文章推荐
- C++走向远洋——26(项目二,2,构造函数与析构函数)
- C++走向远洋——25(项目二,游戏类)
- C++上机实验2——正整数类
- C语言基础 字符串
- 响应VC++ 标题栏右边的关闭按钮“红叉”
- C++走向远洋——24(项目一,三角形,复制构造函数)
- C++11 function (2017-05-05 第2次修改)
- C语言基础 指针与数组
- c++实验2-标准体重
- C语言基础 函数
- C语言基础 一维数组
- C语言基础 switch、while、for循环
- C语言基础 运算符和表达式
- C语言基础 进制的转换
- C语言字符串strlen函数使用
- unix域socket绑定路径不创建文件问题[c语言]
- Android NDK编译C/C++小结
- libc++abi.dylib: terminate_handler unexpectedly threw an exception错误小结
- 《Effective C++》阅读笔记01
- C语言字符型数据(二)— 汉字的读写