命令行参数
17.1 短命令行参数
函数getopt()用来分析命令行参数,其函数原型和相关变量声明如下:
#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
optind: 初始化值为1,下一次调用getopt时,从optind存储的位置重新开始检查选项,也就是从下一个'-'的选项 开始。
opterr: 初始化值为1,当opterr=0时,getopt不向stderr输出错误信息。
optopt: 当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中,getopt返 回’?’。
optarg和optind是两个最重要的external变量。optarg是指向参数的指针(当然这只针 对有参数的选项); optind是argv[]数组的索引,众所周知,argv[0]是函数名称,所有参数从 argv[1]开始,所以optind被初始化设置指为1每调用一次getopt()函数, 返回一个选项,如果 该选项有参数,则optarg指向该参数。 在命令行选项参数再也检查不到optstring中包含的 选项时,返回-1。
函数getopt()有三个参数,argc和argv[]应该不需要多说,下面说一下字符串optstring, 它是作为选项的字符串的列表。
函数getopt()认为optstring中,以’-’开头的字符.optstring中的格式规范如下: 1.单个字符,表示选项. 2.单个字符后接一个冒号’:’,表示该选项后必须跟一个参数值。参数紧跟在选项后或
者以空格隔开。该参数的指针赋给optarg。 3.单个字符后跟两个冒号’:’,表示该选项后必须跟一个参数。 参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的
扩展)。 示例:
214 第17章 命令行参数
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void display_usage(void) {
printf("please usage\n");
printf("./a.out -f -t time -n num -ddate\n"); }
int main(int argc, char *argv[]) {
char *optstring = "ft:n:d::?"; int opt;
int flag = 0;
int num = 0;
int time = 0; int date= 0;
while ((opt = getopt(argc, argv, optstring)) != -1) { switch (opt) {
case 'f':flag = 1; break;
case 'n':num = atoi(optarg);break; case 't':time = atoi(optarg);break; case 'd':date = atoi(optarg);break; case '?':display_usage();exit(0); default:display_usage();exit(0);
} }
printf("flag = %d\tnum=%d\ttime=%d\tdate=%d\n", flag, num, time, date);
return 0;
}
17.2 长命令参数
#include <getopt.h>
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
struct option {
const char *name;
int int int
has_arg; *flag;
val;
};
name 是参数的名称
示例:
17.2节 长命令参数 215
has_arg 指明是否带参数值,其数值可选:
no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name itcast) optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name itcast均可)
flag 当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时, val的值会被赋到flag指向的整型数中,而函数返回值为0
val 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
longindex 如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是 longopts的下标值。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
void display_usage(void) {
printf("please usage\n");
printf("./a.out -f -t time -n num -ddate --month --year year\n"); }
int main(int argc, char *argv[]) {
char *optstring = "ft:n:d::?"; int opt, longindex;
int flag = 0;
int num = 0;
int time = 0; int date = 21; int month = 12; int year = 2014;
struct option longopts[] = {
{"flag", no_argument, NULL, 'f'}, {"time", required_argument, NULL, 't'}, {"num", required_argument, NULL, 'f'}, {"date", required_argument, NULL, 'f'}, {"month", required_argument, NULL, 0}, {"year", required_argument, NULL, 0},
};
while ((opt = getopt_long(argc, argv, optstring, longopts, &longindex)) != -1) { switch (opt) {
case 'f':flag = 1; break;
case 'n':num = atoi(optarg);break; case 't':time = atoi(optarg);break; case 'd':date = atoi(optarg);break; case 0:
216 第17章 命令行参数
if (strcmp("month", longopts[longindex].name) == 0) { month = atoi(optarg);
break;
}
if (strcmp("year", longopts[longindex].name) == 0) {
year = atoi(optarg);
break; }
case '?':display_usage();exit(0);
default:display_usage();exit(0); }
}
printf("flag = %d \tnum= %d \ttime= %d \tdate= %d \tmonth= %d \tyear= %d \n", flag, num, time, date, month, year);
return 0; }