views:

2015

answers:

4

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?

Edit

Yes, I mean C rather than C++. There's 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.

+1  A: 

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.

John Millikin
I'd like to know why you use glib's argument parser -- what makes it better for your uses than alternatives?
ephemient
For no other reason than being bundled with glib. I can learn how to use another parser, or just use the one everybody else does.
John Millikin
Just commenting that the question does ask "What do you use, and why?"... Glib's parser does look pretty nice, though, so thanks for the answer.
ephemient
Not true. It is maintained - http://rpm5.org/files/popt/
Andrejs Cainikovs
A: 

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.

You can find it here: http://tclap.sourceforge.net/

[edit] my mistake you said C and I posted a C++ library..

ChalkTrauma
+4  A: 

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.

dmckee
Ah, solving problems by introducing another level of indirection :) +1, thanks!
ephemient
+2  A: 

As the saying goes, "std. 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 std.

James Antill
I've never used a system without glibc, but you do have a good point that getopt_long is even more widespread. Now I'm curious as to why both you and John mentioned Python? :)
ephemient
I can't speak for them but generally I consider witting direct C programs waist of time. I write program in python and if necessary libraries in C.
Luka Marinko