Using optarg.h to get shorthands that are longer than 1 character and filenames

Viewed 10

Say you have a C++ program that runs as such:

./someProgram -abc fileName1 fileName2 fileName3 --output someFileName

Where -abc is a "shorthand" argument that doesn't have a "full name" (i.e. "--someArg"), fileName1, fileName2, and fileName3 are arguments used by the program but aren't related to a specific flag, and --output someFileName works as the name suggests.

Can all of this be done with the getopt.h library in C++? Specifically, I would like to know how to:

  • Create arguments such as "-g3" as used in g++
  • Make any argument that isn't a flag (such as filename) be treated correctly
  • Still recognize invalid flags (i.e. "--nonsenseArgument" shouldn't be read as a filename, but instead as an argument)

Would this be possible with getopt.h, or should I be writing this functionality myself?

0 Answers
Related