Suggestions for command-line argument processing in C++ efficiently:
Note: Windows specific only
1: #include <iostream.h>
2: int main(int argc, char **argv)
Instead of, for example:
if ( argc != 3 ) {
....
}
Regards
Suggestions for command-line argument processing in C++ efficiently:
Note: Windows specific only
1: #include <iostream.h>
2: int main(int argc, char **argv)
Instead of, for example:
if ( argc != 3 ) {
....
}
Regards
I'd suggest using a library. There's the classic and venerable getopt and I'm sure others.
If you just want to process command line options yourself, the easiest way is to put:
vector<string> args(argv + 1, argv + argc);
at the top of your main()
. This copies all command-line arguments into a vector of std::string
s. Then you can use ==
to compare strings easily, instead of endless strcmp()
calls. For example:
int main(int argc, char **argv) {
vector<string> args(argv + 1, argv + argc);
string infname, outfname;
// Loop over command-line args
// (Actually I usually use an ordinary integer loop variable and compare
// args[i] instead of *i -- don't tell anyone! ;)
for (vector<string>::iterator i = args.begin(); i != args.end(); ++i) {
if (*i == "-h" || *i == "--help") {
cout << "Syntax: foomatic -i <infile> -o <outfile>" << endl;
return 0;
} else if (*i == "-i") {
infname = *++i;
} else if (*i == "-o") {
outfname = *++i;
}
}
}
[EDIT: I realised I was copying argv[0]
, the name of the program, into args
-- fixed.]
There are a number of good libraries available.
Boost Program Options is a fairly heavyweight solution, both because adding it to your project requires you to build boost, and the syntax is somewhat confusing (in my opinion). However, it can do pretty much everything including having the command line options override those set in configuration files.
SimpleOpt is a fairly comprehensive but simple command line processor. It is a single file and has a simple structure, but only handles the parsing of the command line into options, you have to do all of the type and range checking. It is good for both Windows and Unix and comes with a version of glob for Windows too.
getopt is available on Windows. It is the same as on Unix machines, but it is often a GPL library.
This is my favourite way of doing the command line, especially, but definitely not only when efficiency is an issue. It might seem overkill, but I think there are few disadvantages to this overkill.
Use gperf for efficient C/C++ command line processing
Disadvantages:
Advantages:
Using an IDE like eclipse you can probably automate the process of running gperf, so the only thing you would have to do is add an option to the config file and to your switch statement and press build...
I used a batch file to run gperf and do some cleanup and add include guards with sed (on the gperf generated .hpp file)...
So, extremely concise and clean code within your software and one auto-generated hash table file that you don't really need to change manually. I doubt if boost::program_options actually would beat that even without efficiency as a priority.
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.