#include <unistd.h>
int getopt(int argc, char *const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
01 #include <stdio.h>
02 #include <unistd.h>
03
04 int main(int argc, char *argv[])
05 {
06 getopt(argc, argv, ":if:");
07
08 printf("%s\n", optarg);
09
10 return 0;
11 }
[root@localhost C]# ./getopt-2.exe -f write.c
write.c
c:write.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int oc;
char *b_opt_arg;
while ((oc = getopt(argc, argv, ":if:")) != -1) {
switch (oc) {
case 'i':
printf(" i arg\n");
break;
case 'f':
printf("%s\n", optarg);
break;
case ':':
fprintf(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt);
break;
case '?':
fprintf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt);
break;
}
}
exit(0);
}
[root@localhost C]# ./write.exe -f
./write.exe: option '-f' requires an argument
[root@localhost C]# ./write.exe -l
./write.exe: option '-l' is invalid: ignored
01 #include <stdio.h>
02 #include <unistd.h>
03
04 int main(int argc, char *argv[])
05 {
06 int opt;
07
08 opt = getopt(argc, argv, ":ijklmnf:");
09 printf("%d\n",opt);
10
11
12 opt = getopt(argc, argv, ":ijklmnf:");
13 printf("%d\n",opt);
14
15
16 opt = getopt(argc, argv, ":ijklmnf:");
17 printf("%d\n",opt);
18
19
20 return 0;
21 }
[root@localhost C]# ./getopt-2.exe -i -k
105
107
-1
[root@localhost C]# ./getopt-2.exe -i -n
105
110
-1
01 #include <stdio.h>
02 #include <unistd.h>
03
04 int main(int argc, char *argv[])
05 {
06 int opt;
07
08 while( (opt = getopt(argc, argv, ":ijklmnf:")) != -1 ){
09
10 printf("%c\n",opt);
11 }
12
13 return 0;
14 }
[root@localhost C]# ./write-2.exe -j -k -- -f write.c
j
k
[root@localhost C]# ./write-2.exe -j -- -k
j
01 #include <stdio.h>
02 #include <unistd.h>
03
04 int main(int argc, char *argv[])
05 {
06 int opt;
07
08 while( (opt = getopt(argc, argv, ":ijklmnf:")) != -1 ){
09
10 printf("%c\n",opt);
11 printf("%c\n",optopt);
12 }
13
14 return 0;
15 }
[root@localhost C]# ./getopt-2.exe -z
?
z
[root@localhost C]# ./getopt-2.exe -f
:
f
./getopt-2.exe: option requires an argument -- f
?
f
01 #include <stdio.h>
02 #include <unistd.h>
03
04 int main(int argc, char *argv[])
05 {
06 int opt;
07
08 while( (opt = getopt(argc, argv, ":ijklmnf:")) != -1 ){
09
10 printf("%c,%d\n",opt,optind);
11
12 }
13
14 return 0;
15 }
[root@localhost C]# ./getopt-2.exe -i -j -m
i,2
j,3
m,4
01 int main(int argc, char *argv[])
02 {
03 int opt;
04
05 while( (opt = getopt(argc, argv, ":ijklmnf:")) != -1 ){
06
07 printf("%c,%d\n",opt,optind);
08
09 }
10
11 for(; optind < argc; optind++){
12 printf("%d\n");
13 printf("remain in argv-array argument%d: %s\n", optind,argv[optind]);
14 }
15 return 0;
16 }
[root@localhost C]# ./getopt-2.exe -i 'good' -k 'yes' -j 'ok' -l
i,2
k,4
j,6
l,8
5
remain in argv-array argument5: good
6
remain in argv-array argument6: yes
7
remain in argv-array argument7: ok
01 #include <stdio.h>
02 #include <unistd.h>
03 #include <stdlib.h>
04
05 int main(int argc, char *argv[])
06 {
07 int opt;
08
09 while((opt = getopt(argc, argv, ":if:lr")) != -1) {
10 switch(opt) {
11 case 'i':
12 case 'l':
13 case 'r':
14 printf("option: %c\\n", opt);
15 break;
16 case 'f':
17 printf("filename: %s\\n", optarg);
18 break;
19
20 case ':':
21 printf("option needs a value\\n");
22 break;
23
24 case '?':
25 printf("unknown option: %c\\n", optopt);
26 break;
27 }
28 }
29
30 for(; optind < argc; optind++)
31 printf("argument: %s\\n", argv[optind]);
32 exit(0);
33 }
[root@localhost C]# ./getopt.exe -i -lr 'hi there' -f write.c -q
option: i
option: l
option: r
filename: write.c
unknown option: q
argument: hi there
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int c;
while(( c = getopt(argc, argv, "ab::")) != -1){
switch(c) {
case 'a':
printf("option: %c\\n", c);
break;
case 'b':
printf("option: %c\\n", c);
printf("argument: %s\\n", optarg);
break;
}
}
return 0;
}
[root@localhost C]# ./write-3.exe -b
option: b
argument: (null)
[root@localhost C]# ./write-3.exe -b YANKEES
option: b
argument: (null)
[root@localhost C]# ./write-3.exe '-b YANKEES'
option: b
argument: YANKEES
[root@localhost C]# ./write-3.exe "-b YANKEES"
option: b
argument: YANKEES
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |