命令行解析
命令解析常用的函数getopt、getopt_long、getopt_long_only能够解析长短命令和带参数的命令
getopt函数
定义
头文件
#include <unistd.h>
#include <getopt.h>
/*param
argc:main()函数传递过来的参数的个数
argv:main()函数传递过来的参数的字符串指针数组
optstring:选项字符串,告知 getopt()可以处理哪个选项以及哪个选项需要参数
return
如果选项成功找到,返回选项字母;如果所有命令行选项都解析完毕,返回 -1;如果遇到选项字符不在 optstring 中,返回字符 '?';如果遇到丢失参数,那么返回值依赖于 optstring 中第一个字符,如果第一个字符是 ':' 则返回':',否则返回'?'并提示出错误信息。
*/
int getopt(int argc, char * const argv[], const char *optstring);
//getopt是用来解析命令行选项参数的,但是只能解析短选项: -d 100,不能解析长选项:--prefix
optstring的格式意义
char*optstring = “x:b::d:j”;
单个字符j 表示选项j没有参数 格式:-j即可,不加参数
单字符加冒号x: 表示选项x有且必须加参数 格式:-x 100或-x100
单字符加2冒号b:: 表示选项b可以有,也可以无 仅有的格式:-b200
optarg —— 指向当前选项参数的指针。
optind —— argv未处理的下一个参数的索引,首次调用getopt,optind值为1
optopt —— getopt出现错误时,返回的错误选项字符
opterr —— 如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。
eg
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
int main(int argc, char *argv[])
{
char *string = "x:b::d:j";
int ret = 0;
if(argc <= 1)
printf("please input param\n");
while((ret = getopt(argc, argv, string)) != -1)
{
printf("ret = %c\t\t", ret);
printf("optarg = %s\t\t", optarg);
printf("optind = %d\t\t", optind);
printf("argv[optind] = %s\n", argv[optind]);
}
return 0;
}
测试:
ss@compile-etherLab:~/pass/c$ ./opt -x 200 -b100 -d 20 -j
ret = x optarg = 200 optind = 3 argv[optind] = -b100
ret = b optarg = 100 optind = 4 argv[optind] = -d
ret = d optarg = 20 optind = 6 argv[optind] = -j
ret = j optarg = (null) optind = 7 argv[optind] = (null)
getopt_long
getopt_long比起getopt除了能够解析长参数之外也能够解析短参数
#include <getopt.h>
struct option {
char *name; //选项参数的name
int has_arg; //选项是否带参 no_argument(or 0):选项不带参
required_argument:选项带一个参数
optional_argument:选项可带参数或者不带
int *flag; //决定getopt_long函数的返回值 NULL:返回val值
指向一个变量:返回0 指向的变量的值 = val
int val; //如果flag = NULL getopt_long返回val值
如果flag = &a getopt_long返回0 a = val
}
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
optstring: NULL:长参数选项
longopts: option 数组
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
eg
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
struct option demo_options[] = {
{"mon", required_argument, NULL, 'p'},
{"day", no_argument, NULL, 'n'},
{"month", optional_argument, NULL, 'd'},
{NULL, 0, NULL, 0},
};
int main(int argc, char *argv[])
{
char *string = ":x:b::d:j";
int ret = 0;
int index = 0;
if(argc <= 1)
printf("please input param\n");
while((ret = getopt_long(argc, argv, string, demo_options, &index)) != -1)
{
printf("ret = %c\t\t", ret);
printf("optarg = %s\t\t", optarg);
printf("optind = %d\t\t", optind);
printf("argv[optind] = %s\n", argv[optind]);
printf("optopt = %d\n", optopt);
}
return 0;
}
@compile-etherLab:~/pass/c$ ./getloog --mon 52 --day --month 45
ret = p optarg = 52 optind = 3 argv[optind] = --day
optopt = 0
ret = n optarg = (null) optind = 4 argv[optind] = --month
optopt = 0
ret = d optarg = (null) optind = 5 argv[optind] = 45
optopt = 0
用短参数也是可以的
@compile-etherLab:~/pass/c$ ./getloog -x 100 -b200 -d100 -j
ret = x optarg = 100 optind = 3 argv[optind] = -b200
optopt = 0
ret = b optarg = 200 optind = 4 argv[optind] = -d100
optopt = 0
ret = d optarg = 100 optind = 5 argv[optind] = -j
optopt = 0
ret = j optarg = (null) optind = 6 argv[optind] = (null)
optopt = 0