命令行解析
1.getopt调用介绍
头文件:<unistd.h>
原型:int getopt(int argc, char * const argv[], const char *optstring);
argc
、argv
:由main函数的参数直接传递而来optstring
:一个包含所有合法可选字符组成的字符串
返回值:
- 返回这个选项字符(成功)
- 如果没有参数,则最后一次调用返回-1
- 如果有未知参数或选项未加参数的情况,返回字符
?
- 如果
optstring
的第一个字符为:
,则选项未加参数情况返回字符:
关于
optstring
:是一个由所有合法的可选字符所组成的字符串
单个字符,表示选项没有任何选项参数
单个字符后接一个冒号
:
,表示该选项后必须跟一个参数值(参数仅跟在选项后or以空格隔开)且该参数的指针赋给optarg
单个字符后接两个冒号
::
,表示该选项后可以带参数也可以不带(但参数必须紧紧跟在选项后不能以空格隔开),参数指针赋给optargv
2.getopt设置的全局变量
char *optarg; //指向当前选项参数(如果有)的指针
int optind; //再次调用getopt()时的下一个argv指针的索引号
int optopt; //最后一个未知选项
int opterr; //这个变量非零时,向stderr打印错误,默认为1
- 全局变量在头文件内都已定义,通过opt全局变量能获取的一些状态、错误及参数,以下是
man 3 getopt
手册中对全局变量的描述
The variable `optind` is the index of the next element to be processed in `argv`. The system initializes this value to 1. The caller can reset it to 1 to restart scanning of the same `argv`, or when scanning a new argument vector.
If `getopt()` finds another option character, it returns that character, updating the external variable `optind` and a static variable `nextchar` so that the next call to `getopt()` can resume the scan with the following option character or argv-element.
If there are no more option characters, `getopt()` returns -1. Then `optind` is the index in `argv` of the first argv-element that is not an option.
`optstring` is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so `getopt()` places a pointer to the following text in the same argv-element, or the text of the following argv-element, in `optarg`. Two colons mean an option takes an optional `arg`; if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, "`-oarg`"), then it is returned in `optarg`, otherwise `optarg` is set to zero. This is a GNU extension.
- 关于
optstring
中含有+
、-
的使用描述
By default, `getopt()` permutes the contents of `argv` as it scans, so that eventually all the nonoptions are at the end. Two other modes are also implemented. If the first character of `optstring` is '`+`' or the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a nonoption argument is encountered. If the first character of `optstring` is '`-`', then each nonoption argv-element is handled as if it were the argument of an option with character code 1. (This is used by programs that were written to expect options and other argv-elements in any order and that care about the ordering of the two.) The special argument "`--`" forces an end of option-scanning regardless of the scanning mode.
3.getopt的使用
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int argc, char **argv
int main(int argc, char *argv[]) {
char c;
while((c = getopt(argc, argv, ":ab:c::")) != -1) {
switch(c) {
case 'a':
printf("the option is [%c], the optind is [%d], the optarg is [%s]\n", c, optind, optarg);
break;
case 'b':
printf("the option is [%c], the optind is [%d], the optarg is [%s]\n", c, optind, optarg);
break;
case 'c':
printf("the option is [%c], the optind is [%d], the optarg is [%s]\n", c, optind, optarg);
break;
default:
printf("tht option is illegal [%c], the optarg [%s]\n", c, optarg);
}
}
return 0;
}
4.getopt参数排序
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int argc, char **argv
int main(int argc, char *argv[]) {
char c;
for (int i = 0; i < argc; ++i) printf("[%s]", argv[i]);
printf("\n");
while((c = getopt(argc, argv, ":ab:c::")) != -1) {
switch(c) {
case 'a':
printf("the option is [%c], the optind is [%d], the optarg is [%s]\n", c, optind, optarg);
break;
case 'b':
printf("the option is [%c], the optind is [%d], the optarg is [%s]\n", c, optind, optarg);
break;
case 'c':
printf("the option is [%c], the optind is [%d], the optarg is [%s]\n", c, optind, optarg);
break;
default:
printf("tht option is illegal [%c], the optarg [%s]\n", c, optarg);
}
}
for (int i = 0; i < argc; ++i) printf("[%s]", argv[i]);
printf("\n");
return 0;
}