Command-line processing — optproc()

The command-line processing function is a bit more interesting. The majority of variables derived from the command line are called opt*, where * is the option letter. You'll often see code like this:

if (optv) {
    // do something when -v is present

By convention, the -v option controls verbosity; each -v increments optv. Some code looks at the value of optv to determine how much "stuff" to print; other code just treats optv as a Boolean. The -d option is often used to control debug output.

Command-line processing generally follows the POSIX convention: some options are just flags (like -v) and some have values. Flags are usually declared as int. Valued options are handled in the command-line handler's switch statement, including range checking.

One of the last things in most optproc() handlers is a final sanity check on the command-line options:

The last thing in optproc() is the command-line argument handling. POSIX says that all command-line options come first, followed by the argument list. An initial pass of command-line validation is done right in the switch statement after the getopt() call. Final validation is done after all of the parameters have been scanned from the command-line.