getopt(分析命令行參數)
定義函數
函數說明
返回值
#include <stdio.h>
#include <unistd.h>
int main(int argc, int *argv[])
{
int ch;
opterr = 0;
while ((ch = getopt(argc,argv,"a:bcde"))!=-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);
}
執行過程以及結果展示$ ./getopt -a
other option :?
optopt +a
$ ./getopt -b
option b :b
optopt +
$ ./getopt -c
other option :c
optopt +
$ ./getopt -d
other option :d
optopt +
$ ./getopt -abcd
option a:'bcd'
optopt +
$ ./getopt -bcd
option b :b
other option :c
other option :d
optopt +
$ ./getopt -bcde
option b :b
other option :c
other option :d
other option :e
optopt +
$ ./getopt -bcdef
option b :b
other option :c
other option :d
other option :e
other option :?
optopt +f