I know of the following:
getopt(3)
getopt_long
I have been developing and using libparamset that is a command-line parameter parsing library written in plain C. It provides:
I really like the TCLAP library, because it is very flexible and easy to use. It is also completely template-based, so it is a header-only library.
My mistake: you said C and I posted a C++ library...
GNU has gengetopt which generates code for an options data structure and the getopt_long
code to parse the command line and fill the structure.. It's fairly easy to learn and works well.
As a bonus you can pass the options structure around your code and avoid global storage if desired.
It provides GNU style semantics (obviously), and is small enough to simply include with the project for distribution if you're not sure of your audience's build environment.
As the saying goes, "standard is better than better". So I always use getopt_long() and anything that is non-GNOME/glibby, and the glib one on anything that does.
For the same reason I always use optparse in Python applications, even though it has a lot of missing features relative to getopt_long() ... but that's the Python standard.
Since I was looking for the same thing, I read the answers of this old topic. Finally I chose dropt which is mentioned in What parameter parser libraries are there for C++?. Actually it's implemented in C, so I think it's worth mentioning here as well. I haven't used the C++ helper, which wraps the C implementation.
Interesting facts about dropt:
It is limited though. For instance, I had to adapt my parameters syntax specifications a little; that was acceptable in this very case, but of course sometimes specifications are carved in stone.
As a conclusion I would recommend dropt at least for fast prototyping, tools development, and in-house projects.
popt
has been abandoned for a long time -- argument parsing was merged into glib
since version 2.6, three years ago.
I use glib
's parser, or Python's port of getopt
.