Argument-parsing helpers for C/Unix Argument-parsing helpers for C/Unix c c

Argument-parsing helpers for C/Unix


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:

  • Lightweight
  • Depends only on standard libs
  • No steep learning curve
  • Sufficient for basic arg parsing, plus a couple of powerful features
  • Easy to extend
  • Unrestrictive license (zlib-ish)

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.