There is a C function for that: getopt()
c - comparing argv to string literals more elegantly
-
01-07-2022 - |
Question
my code looks like this:
if((strcmp(argv[1], "-a") == 0) && (argc == 3)) {
/* my code */
} else if((strcmp(argv[1], "-b") == 0) && (argc == 3)) {
/* my code */
} ...
And so on. Is this the only way to work around comparing args? I read someplace that it can be done using a loop, but I can't seem to figure out how. Right now, my code is kind of saturated with these if statements.
Thank you, this is my first question, I won't be asking one again, I like to figure things out on my own. But right now, time is of the essence.
So, is there a crispier way to do this?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow