例子1:
- /*
-
* The following trivial example program uses getopt() to handle two
-
* program options: -n, with no associated value;
-
* and -t val, which expects an associated value.
-
*/
-
#include <unistd.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
int main(int argc, char *argv[])
-
{
-
int flags, opt;
-
int nsecs, tfnd;
-
-
nsecs = 0;
-
tfnd = 0;
-
flags = 0;
-
while ((opt = getopt(argc, argv, "nt:")) != -1) {
-
switch (opt) {
-
case 'n':
-
flags = 1;
-
break;
-
case 't':
-
nsecs = atoi(optarg);
-
tfnd = 1;
-
break;
-
default: /* '?' */
-
fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", argv[0]);
-
exit(EXIT_FAILURE);
-
}
-
}
-
printf("flags=%d; tfnd=%d; nsecs=%d; optind=%d\n", flags, tfnd, nsecs, optind);
-
if (optind >= argc) {
-
fprintf(stderr, "Expected argument after options\n");
-
exit(EXIT_FAILURE);
-
}
-
printf("name argument = %s\n", argv[optind]);
-
/* Other code omitted */
-
exit(EXIT_SUCCESS);
-
}
编译:gcc -Wall -o getopt getopt.c
运行:./getopt -n -t 120 nessecory_arg
结果:
flags=1; tfnd=1; nsecs=120; optind=4
name argument = nessecory_arg
注意:当我们将执行命令改为:./getopt -n -t 120
输出的结果为:
flags=1; tfnd=1; nsecs=120; optind=4
Expected argument after options
这是因为,当getopt()函数解析完了参数时,optind应该是argv指针数组中第一个非可选参数的索引。现在没有“非可选参数”,所以条件“optind >= argc”成立了,所以输出了“Expected argument after options”
例子2:
- #include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <fcntl.h>
-
int main(int argc, char *argv[])
-
{
-
int c;
-
-
// opterr = 0;
-
while((c = getopt(argc, argv, "iu:z:")) != -1){
- switch(c){
-
case 'i':
- printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",
-
(optind-1), (optind-1), argv[optind-1]);
- printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
-
(optind), (optind), argv[optind]);
- printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
- break;
-
-
case 'u':
- printf("current option index:(optind-2)=%d, argv[optind-2]=argv[%d]=%s\n",
-
(optind-2), (optind-2), argv[optind-2]);
- printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
-
(optind), (optind), argv[optind]);
- printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
- break;
-
- case 'z':
- printf("current option index:(optind-1)=%d, argv[optind-1]=argv[%d]=%s\n",
-
(optind-1), (optind-1), argv[optind-1]);
- printf("next option index:(optind)=%d, argv[optind]=argv[%d]=%s\n",
-
(optind), (optind), argv[optind]);
- printf("optarg=%s, opterr=%d, optopt=%d\n\n", optarg, opterr, optopt);
- break;
-
- case '?':
- fprintf(stderr, "Usage: %s [-i] [-u username] [-z filename]\n", argv[0]);
- break;
-
}
-
}
-
exit(0);
-
}
编译:gcc -Wall -o getopt2 getopt2.c
运行:./getopt2 -i -u username -z filename
结果:
current option index:(optind-1)=1, argv[optind-1]=argv[1]=-i
next option index:(optind)=2, argv[optind]=argv[2]=-u
optarg=(null), opterr=1, optopt=0
current option index:(optind-2)=2, argv[optind-2]=argv[2]=-u
next option index:(optind)=4, argv[optind]=argv[4]=-z
optarg=username, opterr=1, optopt=0
current option index:(optind-1)=5, argv[optind-1]=argv[5]=filename
next option index:(optind)=6, argv[optind]=argv[6]=(null)
optarg=filename, opterr=1, optopt=0
例子3:
- #include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <fcntl.h>
-
-
int main(int argc, char *argv[])
-
{
-
int c;
-
-
opterr = 0;
-
while((c = getopt(argc, argv, "Oo:W:all")) != -1){
-
printf("option char: %c\n", c);
-
switch(c){
-
case 'O':
-
printf("optimization flag is open.\n\n");
-
break;
-
case 'o':
-
printf("the obj is: %s\n\n", optarg);
-
break;
-
case 'W':
-
printf("optarg: %s\n\n", optarg);
-
break;
-
case '?':
-
fprintf(stderr, "Usage: %s [-Wall] [-O] [-o arg] arg\n", argv[0]);
-
break;
-
case ':':
-
fprintf(stderr, "miss option char in optstring.\n");
-
break;
-
}
-
}
-
exit(0);
-
}
编译:gcc -Wall -o mygcc gcc.c
运行:./mygcc -Wall -O -o mygcc
结果:
option char: W
optarg: all
option char: O
optimization flag is open.
option char: o
the obj is: mygcc
这个例子演示了如何利用getopt()函数来解析形如:
gcc -Wall -O -o hello hello.c 这样的参数。