getopt是linux下获取程序启动参数的函数
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main(int argc, char** argv)
{
int opt = 0;
while(-1 != (opt = getopt(argc, argv, "n:t:kl")))
{
switch(opt)
{
case 'n':
fprintf(stdout, "n:%s\n", optarg);
break;
case 't':
fprintf(stdout, "t:%s\n", optarg);
break;
case 'k':
fprintf(stdout, "k option get\n");
break;
case 'l':
fprintf(stdout, "l option get\n");
break;
}
}
return 0;
}
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t vi -k -l
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t -k -l
n:100
t:-k
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n
tg: option requires an argument -- n
smi /project/bm3.5/smi/test/zd/self>tg -n -t
n:-t
tg: invalid option -- o
n:100