第一次测试
[root@localhost ~]# gcc test.c -o test
[root@localhost ~]# cat test.c
#include <stdio.h>
#include <getopt.h>
static struct option original_opts[] = {
{.name = "tcp", .has_arg = 1, .val = 't'},
{.name = "udp", .has_arg = 1, .val = 'u'},
{.name = "icmp", .has_arg = 1, .val = 'i'},
{NULL},
};
struct global_parameter
{
char name[10];
int number;
struct option *opts;
};
struct global_parameter globals = {
.name = "caijie",
.number = 10,
.opts = original_opts,
};
#define opts globals.opts
int main(int argc,char **argv)
{
int c = 0;
while ((c = getopt_long(argc, argv,"-t:u:i:",opts, NULL)) != -1) {
switch (c) {
case 't':
printf("tcp ---- opt = %s\n",optarg);
break;
case 'u':
printf("udp ---- opt = %s\n",optarg);
break;
case 'i':
printf("icmp ---- opt = %s\n",optarg);
break;
case 1:
printf("nothing ---- opt = %s\n",optarg);
break;
case '?':
printf("ukonwn\n");
}
}
return 0;
}
[root@localhost ~]# ./test -t tcp x -u udp ! -i icmp
tcp ---- opt = tcp
nothing ---- opt = x
udp ---- opt = udp
nothing ---- opt = !
icmp ---- opt = icmp
第二次测试
[root@localhost ~]# gcc test.c -o test
[root@localhost ~]# cat test.c
#include <stdio.h>
#include <getopt.h>
static struct option original_opts[] = {
{.name = "tcp", .has_arg = 1, .val = 't'},
{.name = "udp", .has_arg = 1, .val = 'u'},
{.name = "icmp", .has_arg = 1, .val = 'i'},
{NULL},
};
struct global_parameter
{
char name[10];
int number;
struct option *opts;
};
struct global_parameter globals = {
.name = "caijie",
.number = 10,
.opts = original_opts,
};
#define opts globals.opts
int main(int argc,char **argv)
{
int c = 0;
while ((c = getopt_long(argc, argv,"t:u:i:",opts, NULL)) != -1) {
switch (c) {
case 't':
printf("tcp ---- opt = %s\n",optarg);
break;
case 'u':
printf("udp ---- opt = %s\n",optarg);
break;
case 'i':
printf("icmp ---- opt = %s\n",optarg);
break;
case 1:
printf("nothing ---- opt = %s\n",optarg);
break;
case '?':
printf("ukonwn\n");
}
}
return 0;
}
[root@localhost ~]# ./test -t tcp x -u udp ! -i icmp
tcp ---- opt = tcp
udp ---- opt = udp
icmp ---- opt = icmp
阅读(1389) | 评论(0) | 转发(0) |