Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Example B-1 demonstrates the use of getopt() to parse the command line for two options: the -x option, which doesn’t expect an argument, and the -p option which does expect an argument. This program suppresses error messages from getopt() by specifying a colon (:) as the first character in optstring.
To allow us to observe the operation of getopt(), we include some printf() calls to display the information returned by each getopt() call. On completion, the program prints some summary information about the specified options and also displays the next nonoption word on the command line, if there is one. The following shell session log shows the results when we run this program with different command-line arguments: