一、案例1
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void opt_example1(int argc, char **argv) {
const char *optstr = "a:";
char ch;
while ((ch = getopt(argc, argv, optstr)) != -1) {
switch (ch) {
case 'a':
printf("have option: -a\n");
printf("the argument of -a is %s\n", optarg);
break;
}
}
}
二、案例2
void opt_example2(int argc, char **argv) {
const char *optstr = "b::";
char ch;
while ((ch = getopt(argc, argv, optstr)) != -1) {
switch (ch) {
case 'b':
printf("have option: -b\n");
printf("the argument of -b is %s\n", optarg);
break;
}
}
}
三、案例3
void opt_example3(int argc, char **argv) {
const char *optstr = "c";<