I'd like to pass parameters to my C++ program in the following manner:
./myprog --setting=value
Are there any libraries which will help me to do this easily?
I'd like to pass parameters to my C++ program in the following manner:
./myprog --setting=value
Are there any libraries which will help me to do this easily?
Boost.Program_options
GNU GetOpt.
A simple example using GetOpt:
You can also use optarg if you have options that accept arguments.
There are these tools in the GNU C Library, which includes GetOpt.
If you are using Qt and like the GetOpt interface, froglogic has published a nice interface here.
Tooting my own horn if I may, I'd also like to suggest taking a look at an option parsing library that I've written: dropt.
One feature that it offers that many others don't is the ability to override earlier options. For example, if you have a shell alias:
and you want to use
bar
but with--flag1
disabled, it allows you to do:Try CLPP library. It's simple and flexible library for command line parameters parsing. Header-only and cross-platform. Uses ISO C++ and Boost C++ libraries only. IMHO it is easier than Boost.Program_options.
Library: http://sourceforge.net/projects/clp-parser/
26 October 2010 - new release 2.0rc. Many bugs fixed, full refactoring of the source code, documentation, examples and comments have been corrected.
I think that GNU GetOpt is not too immediate to use.
Qt and Boost could be a solution, but you need to download and compile a lot of code.
So I implemented a parser by myself that produces a std::map<std::string, std::string> of parameters.
For example, calling:
map will be:
Usage is:
MainOptions.h
MainOptions.cpp