支持短选项和长选项!

  • 短选项:形如-a-v,以-加单个字符组成。-叫做选项标识符,选项标识符后面可以紧跟一个字符,这个字符叫做选项字符。选项字符后面可以紧跟一个或多个字符,这些字符叫做选项参数。如-lpthread选项字符为l,参数为pthread
  • 长选项:形如--all--version,以--加一个单词组成,用=跟参数。如--data=format选项为data,参数为format
1
2
-lpthread -d format
--data=format

短选项解析 getopt()

1
2
3
4
5
6
7
#include <unistd.h>

int getopt(int argc, char * const argv[],
const char *optstring);

extern char *optarg;
extern int optind, opterr, optopt;
  • argc, argvmain函数参数中的argc, argv,代表命令行参数!
  • optstring
  • optindargv中下一个待处理的选项下标。默认值为1,当然如果要重新解析argv,可以手动重置其为1。

长命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <getopt.h>
#include <unistd.h>

static void
parse_client_options(int argc, char *argv[])
{
enum {
OPT_SRCIP = 256,
OPT_DSTIP,
OPT_SRCPORT,
OPT_DSTPORT
};

// : 选项后面必须有参数
static char *short_options = "hp:t:s:n:i:b:";
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"srcip", required_argument, NULL, OPT_SRCIP },
{"dstip", required_argument, NULL, OPT_DSTIP },
{"srcport", required_argument, NULL, OPT_SRCPORT },
{"dstport", required_argument, NULL, OPT_DSTPORT },
{"proto", required_argument, NULL, 'p'},
{"tos", required_argument, NULL, 't'},
{"pktsize", required_argument, NULL, 's'},
{"interval", required_argument, NULL, 'i'},
{"bandwidth", required_argument, NULL, 'b'},
{"pktnums", required_argument, NULL, 'n'},
{0, 0, 0, 0 }
};

int opt;
while((opt = getopt_long_only(
argc, argv, short_options, long_options, NULL)) != -1) {

switch(opt) {
case 'h': client_help_info(); exit(EXIT_SUCCESS);
case 'p':
pkts.proto = atoi(optarg);
if (pkts.proto != TCP && pkts.proto != UDP) {
printf("参数错误: %d,只能为TCP(6)/UDP(17)\n", pkts.proto);
exit(EXIT_FAILURE);
}
break;
case 't': pkts.tos = atoi(optarg); break;
case 's':
pkts.pktsize = atoi(optarg);
if (pkts.pktsize < 64 || pkts.pktsize > 1500) {
printf("数据包大小应该在[64, 1500]的范围内\n");
exit(EXIT_FAILURE);
}
break;
case 'n': pkts.pktnums = atoi(optarg); break;
case 'i': pkts.interval = atoi(optarg); break;
case 'b': pkts.bandwidth = atoi(optarg); break;
case OPT_SRCIP: strncpy(pkts.srcip, optarg, 16); break;
case OPT_DSTIP: strncpy(pkts.dstip, optarg, 16); break;
case OPT_SRCPORT: pkts.srcport = atoi(optarg); break;
case OPT_DSTPORT: pkts.dstport = atoi(optarg); break;
default:
client_help_info();
exit(EXIT_FAILURE);
}
}
}

短命令 only

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* 解析命令行参数处理函数
*/
void parse_args(int argc, char **argv) {
int opt;

mdelay = 1000000; // 默认刷新率为1秒
x1 = 0; // 默认区域为全屏
x2 = vinfo.xres - 1;
y1 = 0;
y2 = vinfo.yres - 1;

while ((opt = getopt(argc, argv, "hrsd:m:x:y:")) != -1) {
switch (opt) {
case 'r': // 全屏刷新标志
rect = true;
break;
case 's':
show_fps = true;
break;
case 'd': // 帧间隔 ms
mdelay = atoi(optarg) * 1000;
break;
case 'm':
mode = atoi(optarg);
if (mode > 1 || mode < 0) {
printf("mode must be 0 or 1\n\n");
help_info();
exit(EXIT_FAILURE);
}
break;
case 'x': // 指定x区域
sscanf(optarg, "%d:%d", &x1, &x2);
break;
case 'y': // 指定y区域
sscanf(optarg, "%d:%d", &y1, &y2);
printf("y1, y2: %d, %d\n", y1, y2);
break;
case 'h': // FALLTHROUGH
default: /* '?' */
help_info();
exit(EXIT_FAILURE);
}
}
}

其他资料