一、案例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";
char ch;
while ((ch = getopt(argc, argv, optstr)) != -1) {
switch (ch) {
case 'c':
printf("have option: -c\n");
printf("the argument of -c is %s\n", optarg);
break;
}
}
}
四、案例4
void opt_example4(int argc, char **argv) {
const char *optstr = "cd";
char ch;
while ((ch = getopt(argc, argv, optstr)) != -1) {
switch (ch) {
case 'c':
printf("have option: -c\n");
printf("the argument of -c is %s\n", optarg);
break;
case 'd':
printf("have option: -d\n");
printf("the argument of -d is %s\n", optarg);
break;
}
}
}
五、案例5
void opt_example5(int argc, char** argv)
{
printf("original argv is:");
for (int i = 0; i < argc; i++)
{
printf(" %s", argv[i]);
}
printf("\n");
char ch;
const char* optstr = "a:b:c:";
while( (ch = getopt(argc, argv, optstr)) != -1 )
{
switch(ch)
{
case 'a':
break;
case 'b':
break;
}
}
printf("after getopt, argv is:");
for (int i = 0; i < argc; i++)
{
printf(" %s", argv[i]);
}
printf("\n");
printf("and now, optind = %d, argv[%d] = %s\n",
optind, optind, argv[optind]);
}
六、案例6
void opt_example6(int argc, char **argv) {
char ch;
const char *optstr = "a:b:c:";
while ((ch = getopt(argc, argv, optstr)) != -1) {
switch (ch) {
case 'a':
break;
case 'b':
break;
}
}
printf("optind = %d, argv[%d] = %s\n", optind, optind, argv[optind]);
if (optind < argc) {
printf("you have entered extra arguments:");
for (int i = optind; i < argc; i++) {
printf(" %s", argv[i]);
}
printf("\n");
}
}
七、案例7
void opt_example7(int argc, char **argv) {
char ch;
const char *optstr = "a:b:c:";
while ((ch = getopt(argc, argv, optstr)) != -1) {
switch (ch) {
case 'a':
break;
case 'b':
break;
case '?':
printf("Usage: %s -a <a_arg> -b <b_arg>\n", argv[0]);
}
}
}
八、案例8
void help_and_die(char *argv0) {
printf("Usage: %s [-hv] -s <num> -E <num> -b <num> -t <file>\n", argv0);
printf("Options:\n");
printf(" -h Print this help message.\n");
printf(" -v Optional verbose flag.\n");
printf(" -s <num> Number of set index bits.\n");
printf(" -E <num> Number of lines per set.\n");
printf(" -b <num> Number of block offset bits.\n");
printf(" -t <file> Trace file.\n");
printf("\n");
printf("Examples:\n");
printf(" linux> %s -s 4 -E 1 -b 4 -t traces/yi.trace\n", argv0);
printf(" linux> %s -v -s 8 -E 2 -b 4 -t traces/yi.trace\n", argv0);
exit(0);
}
void opt_example8(int argc, char *argv[]) {
const char *optstr = "hvs:E:b:t:";
char ch;
bool verbose = false;
int s = -1;
int E = -1;
int b = -1;
int t = -1;
while ((ch = getopt(argc, argv, optstr)) != -1) {
switch (ch) {
case 'h':
help_and_die(argv[0]);
exit(0);
break;
case 'v':
verbose = true;
break;
case 's':
s = atoi(optarg);
break;
case 'E':
E = atoi(optarg);
break;
case 'b':
b = atoi(optarg);
break;
case 't':
t = atoi(optarg);
break;
case '?':
help_and_die(argv[0]);
break;
}
}
if (optind == 1) {
printf("%s: Missing required command line argument\n", argv[0]);
help_and_die(argv[0]);
}
}