[ACCEPTED]-Argument-parsing helpers for C/Unix-parsing

Accepted answer
Score: 16

GNU has gengetopt which generates code for an options 10 data structure and the getopt_long code to parse the 9 command line and fill the structure.. It's 8 fairly easy to learn and works well.

As a 7 bonus you can pass the options structure 6 around your code and avoid global storage 5 if desired.

It provides GNU style semantics 4 (obviously), and is small enough to simply 3 include with the project for distribution 2 if you're not sure of your audience's build 1 environment.

Score: 5

As the saying goes, "standard is better 7 than better". So I always use getopt_long() and 6 anything that is non-GNOME/glibby, and the 5 glib one on anything that does.

For the same 4 reason I always use optparse in Python applications, even 3 though it has a lot of missing features 2 relative to getopt_long() ... but that's 1 the Python standard.

Score: 4

Since I was looking for the same thing, I 13 read the answers of this old topic. Finally 12 I chose dropt which is mentioned in What parameter parser libraries are there for C++?. Actually 11 it's implemented in C, so I think it's worth 10 mentioning here as well. I haven't used 9 the C++ helper, which wraps the C implementation.

Interesting 8 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 7 instance, I had to adapt my parameters syntax 6 specifications a little; that was acceptable 5 in this very case, but of course sometimes 4 specifications are carved in stone.

As a 3 conclusion I would recommend dropt at least 2 for fast prototyping, tools development, and 1 in-house projects.

Score: 3

popt has been abandoned for a long time -- argument 3 parsing was merged into glib since version 2.6, three 2 years ago.

I use glib's parser, or Python's port 1 of getopt.

Score: 2

I'm not too fond of getopt either (although 11 it's pretty standard). One solution I've 10 made is the function argopt(). It's C compatible, can 9 be used to test whether flags are set as 8 well as reading options with values. It 7 only supports short options (e.g. -h) although 6 writing a similar function for long options 5 (e.g. --help) shouldn't be too difficult. See 4 example:

int main(int argc, char **argv){

    if(argopt(argc, argv, 'p')) printf("-p is set\n");
    if(argopt(argc, argv, 'q')) printf("-q is set\n");

    const char *f = argopt(argc, argv, 'f');
    if(f) printf("-f is %s\n",f);

    return 0;

Example from command line:

$./main -f input.txt -rq
-q is set
-f is input.txt

Disclaimer: I 3 made this function for fun, intending for 2 it to be short, C-compatible, easy to use, and 1 have no dependencies. Here it is:

const char* argopt(int argc, const char *const *argv, char key){

    for(int i=1; i<argc; i++){
        const char *c = argv[i];
        if(*c!='-') continue;
        while(*++c) if(*c==key) return argv[(i+1)%argc];

    return 0;
Score: 0

I have been developing and using libparamset that is 2 a command-line parameter parsing library 1 written in plain C. It provides:

  • Cross-platform functionality (Linux, OS X, Windows).
  • Configurable parser where each parameter can act differently (powerful and flexible feature).
  • Auto-generated messages for typos, unknown parameters or invalid parameters.
  • Extra functionality to design command-line user interface.
  • Good documentation.

More Related questions