How to parse program options in C++ using getopt_long

Any C or C++ program worth its salt will take some options and arguments from the user at the shell. Option is typically of the form -h (short) or --help (long). Options can also have a following argument, for example -o someFile.txt or --open someFile.txt. All these types of options and arguments can be processed easily using the getopt_long function in the GNU C library.

An example that illustrates its usage is given below:

Reference: Sec 2.1.3 of Advanced Linux Programming by CodeSourcery LLC and Mark Mitchell

Tried with: GCC 4.9.2 and Ubuntu 14.04

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s