Getopt tutorialAbstract
Getopt is the standard way in c and c++ to parse complex command lines on unix. This tutorial discusses how to use getopt, and provides some simple sample code for your enjoyment.
Requirements
Before you can use getopt, you'll need to include the right header. This isn't very hard. To quote the getopt man page, the required details are:
code2dbgetopt-man-simple
As we can see from this, we'll need to include the unistd.h header before things will compile.
The options string
Let's imagine that our program is going to have two command line options. The first will be the name of an input file (otherwise we might read from stdin or something else like that), and the other is an output file (or we'll use stdout). Additionally, there is an extra command line option -g, which turns gronk mode on. Valid command lines might look like (these aren't all the possible combinations):
fooapp
fooapp -i infile
fooapp -i infile -o outfile
fooapp -g
The way that we tell getopt about these three possible command line options is with the options string.
Options with an arguement
Options which have an arguement are represented in the options string by the letter is that the option, followed by a colon. For example:
i:o:
Options without an arguement
Options without an arguement are the same, except that they lack the colon. In our example:
g
Optional arguements
You can mark an optional arguement with two colons. There aren't any of these in my example, but if there was one, then it might look like:
m::
The complete options string
Therefore, our complete options string for this example is going to look something like:
i:o:g
So how do you use it?
Now that we have an options string, there isn't really all that much more to do.
code2dbexample.c
You'll note here that there is an extra command line option which we got for free, the ? command line option. This is used to display the usage message you see with many unix applications.
Proving this works
We should prove that this works for the sample command line arguements above.
[root@localhost howto-getopt]# fooapp
Command line parsed to: input = (null), output = (null), gronk = 0
[root@localhost howto-getopt]# fooapp -i infile
Command line parsed to: input = infile, output = (null), gronk = 0
[root@localhost howto-getopt]# fooapp -i infile -o outfile
Command line parsed to: input = infile, output = outfile, gronk = 0
[root@localhost howto-getopt]# fooapp -g
Command line parsed to: input = (null), output = (null), gronk = 1
[root@localhost howto-getopt]#
After getopt has finished
When getopt has returned -1, then it has finished finding arguements. At this point optind will be the index into argv of the first element on the command line which isn't an arguement. getopt will have magically moved all the non-arguements to the end of the command line.
Long options
You might have noticed that many unix applications now support long command line arguements, such as --with-gronk. To do this, you use a slightly different invokation of getopt. The first thing we need to discuss is how the longer arguements are specified.
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};