I have a program that parses command line arguments using a while loop. Simply, while iterating through the length of argc, if an argument matches a flag than the next argument is taken as a variable. Now in my assignment we are asked to do this in a way that spaces between flags and integer arguments are optional.
For example if i input -k1 it is the same as -k 1 and 1 is the value stored. I can't find anything that allows this. The only thing I can think is that if argc is an even number it means that there are no pals between a set of argument and i could use scanf("-k%d",key).
Any helpful pointers for me?
-. Get the next character. That is the option flag. If there is anything after that character then the rest is the option value. Do not usescanffor command line args. Use the the standardargcandargvpassed tomain.sscanf(argv[arg], "-%c%d", &flag, &value)(withargbeing the argument number you're checking) and if it returns 2, you have the flag inflagand the value invalue... if it returns 1, you have just the flag inflagand can read the next argument to get the value. If it returns anything else, you have a non-matching argument or an error. This would treat something like "-kabc" the same as just "-k", though.argventry.program -c 1 -d2etc, where the option string needs to bec:d:to say "this program takes options-cand-d, and both of them expect an argument. The code is pretty short; it isn't all that complex. Current versions ofgetopt()tend to be longer, but they're not much more powerful (getopt_long()is a different story).