CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Parsing command-line arguments with getopt

As programs become more complicated, they often accept many command switches to define and constrain their execution (something casually termed creeping featurism [1]).

In addition, command switches do not just indicate, or toggle, a Boolean attribute to the program, but often provide additional string values and numbers to further control the program.

The correct parsing of command switches can quickly become complicated!

To simplify the task of processing command switches in our programs, we'll use the function getopt().

getopt() is not a function in the Standard C library but, like the function strdup(), it is widely available and used. In fact, getopt() conforms to a different standard - an POSIX standard [2], which provides functions enabling operating system portability.

 

[1] UNIX Style, or cat -v Considered Harmful
[2] The IEEE Std 1003.1 (POSIX.1-2017)

 


CITS2002 Systems Programming, Lecture 18, p5, 2nd October 2023.