您的位置:首页 > 运维架构 > Linux

linux 中解析命令行参数 (getopt_long用法)

2011-05-30 16:20 288 查看
#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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: