|
发表于 2003-11-12 17:13:11
|
显示全部楼层
用getopt或getopt_long
#include <getopt.h>
extern int optind;
extern char *optarg;
int main(int argc, char **argv)
{
const char delim[]="b:d:x:";
char ch;
while((ch=getopt(argc,argv,delim)!=-1)
{
switch(ch)
{
case 'w':
/*
* do your operation
*/
break;
case 'x':
/*
* do your operation
*/
break;
case 'b':
/*
* do your operation
* optarg指向选项的参数
*/
break;
default:
/*
* ch='?', 无法识别的选项
*/
break;
}
}
/*
* optind指向第一个参数的下标,通过argv[optind]可以获取
*/
return 0;
} |
|