1

I am writing a command line tool in C++ that takes several arguments, some of them file names but also option parameters.

Is there a standard way to parse and implement these options? A simple way would be to clean the input and use a map to convert it to an integer and use a switch statement in the main.cpp. But that strikes me as leading to a bloated main.cpp. How do other command line tools, like vim, sed etc. deal with interpreting options?

I would be much grateful if you could point me to some examples or documentation.

Many thanks!

2

Is there a standard way to parse and implement these options?

Yes, there is. Along the Posix Standard there's the getopts() function you can use to do the parsing.

Regarding the c++ standard there aren't any libraries to support command line arguments parsing.
Though there's boost::program_options you can use to make it easier to define and parse any kind of program parameters in a typesafe manner.
This uses map like structures to store the parsed values.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.