Last active
March 13, 2022 10:36
-
-
Save silviaclaire/9e6c6eac08f448e03e05fb3342aab1df to your computer and use it in GitHub Desktop.
CL options with multiple arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <getopt.h> | |
void get_short_option(int argc, char **argv) | |
{ | |
int c; | |
int i; | |
opterr = 0; | |
for (i = 0; i < argc; i++) | |
{ | |
// check short option format (with length of 2) | |
if (strstr(argv[i], "-") != NULL && strlen(argv[i]) != 2) | |
{ | |
puts("wrong option format"); | |
return; | |
} | |
} | |
// multiple short options | |
while(1) | |
{ | |
c = getopt(argc, argv, "hs:m:"); | |
if (c == -1) | |
{ | |
break; | |
} | |
switch (c) | |
{ | |
// allow no argument | |
case 'h': | |
printf("option %c\n", c); | |
break; | |
// allow single argument | |
case 's': | |
printf("option %c with arg '%s'\n", c, optarg); | |
break; | |
// allow unlimitted arguments | |
case 'm': | |
optind--; | |
while (optind < argc) | |
{ | |
if (strstr(argv[optind], "-") != NULL) | |
{ | |
break; | |
} | |
printf("option %c with arg '%s'\n", c, argv[optind++]); | |
} | |
break; | |
case '?': | |
puts("invalid option"); | |
return; | |
default: | |
printf("?? getopt returned character code 0%o ??\n", c); | |
} | |
} | |
if (optind < argc) | |
{ | |
printf("non-option ARGV-elements: "); | |
while (optind < argc) | |
{ | |
printf("%s ", argv[optind++]); | |
} | |
printf("\n"); | |
} | |
return; | |
} | |
void get_long_option(int argc, char **argv) | |
{ | |
int c; | |
int i; | |
int index = 0; | |
int do_add, do_append, do_delete, flag; /* flag variables */ | |
// struct option { | |
// const char *name; | |
// int has_arg; | |
// int *flag; | |
// int val; | |
// }; | |
struct option long_options[] = { | |
{"add", no_argument, &do_add, 1 }, // set do_add to 1 | |
{"append", required_argument, &do_append, 1 }, // set do_append to 1 | |
{"delete", optional_argument, &do_delete, 1 }, // set do_delete to 1 | |
{"setflag", no_argument, &flag, 1 }, // set flag to 1 | |
{"clrfalg", no_argument, &flag, 0 }, // set flag to 0 | |
{"verbose", no_argument, 0, 'v' }, // "--verbose" returns "-v" | |
{"file", required_argument, 0, 'f' }, // "--file" returns "-f" | |
{0, 0, 0, 0 } // null termination | |
}; | |
opterr = 0; | |
for (i = 0; i < argc; i++) | |
{ | |
// check short option format (with length of 2) | |
if (strstr(argv[i], "--") == NULL && strstr(argv[i], "-") != NULL && strlen(argv[i]) != 2) | |
{ | |
puts("wrong option format"); | |
return; | |
} | |
} | |
while(1) | |
{ | |
c = getopt_long(argc, argv, ":v:f:", long_options, &index); | |
if (c == -1) | |
{ | |
break; | |
} | |
switch (c) | |
{ | |
case 0: | |
printf("option %s", long_options[index].name); | |
if (optarg) | |
{ | |
printf(" with arg:"); | |
optind--; | |
while (optind < argc) | |
{ | |
if (strstr(argv[optind], "-") != NULL) | |
{ | |
break; | |
} | |
printf(" %s", argv[optind++]); | |
} | |
} | |
if (long_options[index].flag != NULL) | |
{ | |
printf(" (flag is set to %d)", long_options[index].val); | |
} | |
printf("\n"); | |
break; | |
case 'v': | |
case 'f': | |
printf("option %c", c); | |
if (optarg) | |
{ | |
printf(" with arg:"); | |
optind--; | |
while (optind < argc) | |
{ | |
if (strstr(argv[optind], "-") != NULL) | |
{ | |
break; | |
} | |
printf(" %s", argv[optind++]); | |
} | |
} | |
printf("\n"); | |
break; | |
case ':': | |
puts("missing argument"); | |
return; | |
case '?': | |
puts("invalid option"); | |
return; | |
default: | |
printf("?? getopt returned character code 0%o ??\n", c); | |
} | |
} | |
if (optind < argc) | |
{ | |
printf("non-option ARGV-elements: "); | |
while (optind < argc) | |
{ | |
printf("%s ", argv[optind++]); | |
} | |
printf("\n"); | |
} | |
return; | |
} | |
int main(int argc, char **argv) | |
{ | |
get_short_option(argc, argv); | |
// get_long_option(argc, argv); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment