Writing a program that parses command-line options
In this recipe, we will create a more advanced program—one that parses command-line options. In the previous recipe, we wrote a program that parsed arguments using
argv. We will use those variables here as well, but for options. Options are the hyphenated letters, such as
This program is similar to the previous one, with the difference that this program can both this;
-s for "sum" and
-m for "multiply."
Almost all programs in Linux take different options. Knowing how to parse options to the programs you create is a must; that is how the user changes the behavior of your program.
All you need is a text editor, the GCC compiler, and Make.
How to do it…
Since this source code will be a bit longer, it will be broken up into three pieces. The entire code goes into the same file, though. The complete program can be downloaded from GitHub at https://github...