Chinaunix

标题: getopt函数请教 [打印本页]

作者: anonyaniu    时间: 2008-05-10 09:59
标题: getopt函数请教
while((ch = getopt(argc,argv,"a:bcde"))!= -1)

while((ch = getopt(argc,argv,"a:b:c:d:e"))!= -1)
的区别是?
作者: xi2008wang    时间: 2008-05-10 10:32
如果一个选项字符后面有一个冒号,则这个选项要求参数.

a:bcde --> 选项a要有参数
a:b:c:d:e  --> 选项a, b, c, d, 都要有参数
作者: anonyaniu    时间: 2008-05-10 13:59
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,"a:b:c:d:e"))!= -1)
switch(ch)
{
        case 'a':
                printf("option a:'%s'\n",optarg);
                break;
        case 'b':
                printf("option b :b\n");
                break;
        default:
                printf("other option :%c\n",ch);
        }
        printf("optopt +%c\n",optopt);
}

while((ch = getopt(argc,argv,"a:bcde"))!= -1)
./test -bc
option b :b
other option :c
optopt +


while((ch = getopt(argc,argv,"a:b:c:d:e"))!= -1)
./test -bc
option b :b
optopt +
-bc这种情况下为什么没有取到c呢?好像应该打印出
./test -bc
option b:'c'
optopt +
作者: xi2008wang    时间: 2008-05-10 14:20
printf("option b :b\n");  --> printf("option b:'%s'\n",optarg);
作者: anonyaniu    时间: 2008-05-10 21:48
十分感谢!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2