一、命令行参数
命令行参数可以分为两类,一类是短选项,一类是长选项,短选项在参数前加一杠"-",长选项在参数前连续加两杠"–",如下表(ls 命令参数)所示,其中-a,-A,-b都表示短选项,–all,–almost-all, --author都表示长选项。他们两者后面都可选择性添加额外参数。比如–block-size=SIZE,SIZE便是额外的参数。
短参数与后面的值可以加空格,也可以不加空格。如-a 1 和 -a1是相同的。
长参数与后面的值可以加空格,也可以用=号连接。如–byte 3 和–byte=3是相同的。
man ls看一下ls的官方介绍文档:
二、<getpot.h>头文件实现参数解析
<getpot.h>头文件主要提供了:
一个结构体:
struct option
{
const char *name;
int has_arg;
int *flag;
int val;
};
name:是参数的名称
has_arg:指明是否带参数值,其数值可选:
no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob)
optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
flag :当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,
当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0
val : 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
三个函数:
1.int getopt (int argc, char *const *argv, const char *shortopts)
- argc和argv就是从main函数中传递过来的。
- 如果shortopts="abcd:",getopt函数将依次检查命令行是否指定了 -a, -b, -c及 -d
(这需要多次调用getopt函数,直到其返回-1),当检查到上面某一个参数被指定时,函数会返回
被指定的参数名称(即该字母)。最后一个参数d后面带有冒号,: 表示参数d是可以指定值的,
如 -d 100 或 -d user.
2.int getopt_long (int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind)
3.int getopt_long_only (int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind)
- longopts指向的是一个由option结构体组成的数组。
- longind记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。
四个全局变量:
char *optarg; 指向当前解析的参数。
int optind; 下一个将被处理到的参数在argv中的下标值。
int opterr; 如果opterr = 0,在getopt、getopt_long、getopt_long_only遇到错
误将不会输出错误信息到标准输出流。 opterr在非0时,向屏幕输出错误。
int optopt; 当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,
该选项存储在optopt中,getopt返回'?’
返回值:
(1)如果短选项找到,那么将返回短选项对应的字符。
(2)如果长选项找到,如果flag为NULL,返回val。如果flag不为空,返回0
(3)如果遇到一个选项没有在短字符、长字符里面。或者在长字符里面存在二义性的,返回“?”
(4)如果解析完所有字符没有找到(一般是输入命令参数格式错误,eg: 连横杠都没有加的选项),返回“-1”
(5)如果选项需要参数,忘了添加参数。返回值取决于optstring,如果其第一个字符是“:”,则返回“:”,
否则返回“?”。
注意:
(1)longopts的最后一个元素必须是全0填充,否则会报段错误
(2)短选项中每个选项都是唯一的。而长选项如果简写,也需要保持唯一性。
解释:
1、argc和argv和main函数的两个参数一致。
2、optstring: 表示短选项字符串。
形式如a:b::cd:别表示程序支持的命令行短选项有-a、-b、-c、-d,冒号含义如下:
> (1)只有一个字符,不带冒号——只表示选项, 如-c
> (2)一个字符,后接一个冒号——表示选项后面带一个参数,如-a 100
> (3)一个字符,后接两个冒号——表示选项后面带一个可选参数,即参数可有可无,
> 如果带参数,则选项与参数直接不能有空格,形式应该如-b200
(1)name:表示选项的名称,比如daemon,dir,out等。
(2)has_arg:表示选项后面是否携带参数。该参数有三个不同值,如下:
a: no_argument(或者是0)时 ——参数后面不跟参数值,eg: --version,–help
b: required_argument(或者是1)时 ——参数输入格式为:–参数 值 或者 --参数=值。
eg:–dir=/home
c: optional_argument(或者是2)时 ——参数输入格式只能为:–参数=值
(3)flag:这个参数有两个意思,空或者非空。a:如果参数为空NULL,那么当选中某个长选项的时候,getopt_long将返回val值。
eg,可执行程序 --help,getopt_long的返回值为h.
b:如果参数不为空,那么当选中某个长选项的时候,getopt_long将返回0,并且将flag指针参数指向val值。eg: 可执行程序 --http-proxy=127.0.0.1:80 那么getopt_long返回值为0,并且lopt值为1。
(4)val:表示指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值val。
三、用例
1.官方用例
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>
int
main(int argc, char **argv)
{
int c;
int digit_optind = 0;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"add", required_argument, 0, 0 },
{"append", no_argument, 0, 0 },
{"delete", required_argument, 0, 0 },
{"verbose", no_argument, 0, 0 },
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value '%s'\n", optarg);
break;
case 'd':
printf("option d with value '%s'\n", optarg);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
exit(EXIT_SUCCESS);
}
2.个人用例
#include <stdio.h>
#include <getopt.h>
char *l_opt_arg;
char* const short_options = "nbl:";
struct option long_options[] = {
{ "name", 0, NULL, 'n' },
{ "bf_name", 0, NULL, 'b' },
{ "love", 1, NULL, 'l' },
{ 0, 0, 0, 0},
};
int main(int argc, char *argv[])
{
int c;
while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
{
switch (c)
{
case 'n':
printf("My name is XL./n");
break;
case 'b':
printf("His name is ST./n");
break;
case 'l':
l_opt_arg = optarg;
printf("Our love is %s!/n", l_opt_arg);
break;
}
}
return 0;
}
编译并运行:
[root@localhost liuxltest]# gcc -o getopt getopt.c
[root@localhost liuxltest]# ./getopt -n -b -l forever
My name is XL.
His name is ST.
Our love is forever!
[root@localhost liuxltest]#
[root@localhost liuxltest]# ./getopt -nb -l forever
My name is XL.
His name is ST.
Our love is forever!
[root@localhost liuxltest]# ./getopt -nbl forever
My name is XL.
His name is ST.
Our love is forever!
参考资料:
https://blog.csdn.net/qq_33850438/article/details/80172275