I know of the following:
- The venerable
getopt(3)
- The extended
getopt_long
- glibc's
argp
parser for Unix-style argument vectors - popt from the GNOME project (or its spiritual successor in Glib)
I'm sure there's more that I haven't used or even heard of; a quick Google search reveals Gopt, argtable, and Optlist.
Personally, I like argp
best, and every program I wrote using getopt
/getopt_long
(beyond a certain baseline of complexity) has been converted to use argp
. It's more widely available than popt
, more powerful than getopt_long
, well-documented, consistent with all the GNU-style conventions, and very flexible. On the downside, it's far from the easiest to use (thanks to being so flexible), and the code to support it is quite verbose (as are many things in C).
What do you use, and why?
Yes, I mean C rather than C++. There are a ton of C++ parsers, but I don't use C++.
John Millikin notes that popt
is no longer maintained. I list it because many programs still use it -- including AbiWord, rpm, rsync, and samba -- despite Gnome's efforts to migrate away. But I've added a link to Glib's argument parser now, too.
For C++ argument parsing, see the question What parameter parser libraries are there for C++?
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...
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.
I'm not too fond of getopt either (although it's pretty standard). One solution I've made is the function argopt(). It's C compatible, can be used to test whether flags are set as well as reading options with values. It only supports short options (e.g. -h) although writing a similar function for long options (e.g. --help) shouldn't be too difficult. See example:
Example from command line:
Disclaimer: I made this function for fun, intending for it to be short, C-compatible, easy to use, and have no dependencies. Here it is:
I have been developing and using libparamset that is a command-line parameter parsing library written in plain C. It provides:
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.
popt
has been abandoned for a long time -- argument parsing was merged intoglib
since version 2.6, three years ago.I use
glib
's parser, or Python's port ofgetopt
.