Programming with the APR - Using apr_getopt

1 minute read

This article will show you how to use the built in command line parser that comes with the APR. There will be two source files, one showing you how to use apr_getopt just to parse simple, single options and the other peace of source will show you how to use it with so called long options. You can download the single option parser here and the long options parser here.
X NOTE: The program supplied should actually use apr_app_initialize(&argc, &argv, NULL); since apr_initialize() is intended for library use only.

Prerequisites

Like everytime, you need to have APR libraries installed in order to be able to use them. You’ll additionally need the header files too, so if you’re installing from some distribution specific mechanism, be sure to have the “-dev” packages installed as well.

Explanation

There is really not that much to explain this time. We first initialize APR, create a pool, then initialize the getopt parser and then we’re walking through the options specified on the command line. The most notable part here is the line: cmdLineArgs->interleave = 1;. Setting the interleave to one allows us to have options and final arguments specified “wildly” on the command line. That means that the order of argument is not relevant. You can specify ./aprGetoptLong --choose what -h lalalal or you can type: ./aprGetoptLong --choose what lalala -h which, in this case would lead to the same result.

Simple options code

Now that you have seen how it works with simple options, long options is no more magic. You just have to additionally initialize an array basically, but see for yourself.

Long options code

Hope you enjoyed it.

Updated: