What is the best way of parsing command-line arguments in C++ if the program is specified to be run like this:
prog [-abc] [input [output]]
Is there some way of doing this built into the standard library, or do I need to write my own code?
Related:
Parsing command line arguments in a unicode C++ application
42 Answers 42
The suggestions for boost::program_options and GNU getopt are good ones.
However, for simple command line options I tend to use std::find
For example, to read the name of a file after a -f command line argument. You can also just detect if a single-word option has been passed in like -h for help.
On thing to look out for with this approach you must use std::strings as the value for std::find otherwise the equality check is performed on the pointer values.
I hope it is okay to edit this response instead adding a new one, as this is based on the original answer. I re-wrote the functions slightly and encapsulated them in a class, so here is the code. I thought it might be practical to use it that way as well: