Parsing the Command Line

Aside from reading some configuration file, the last way to influence program behavior is through command-line arguments. This is also the most common way to control program behavior for command-line programs.

Recall earlier that the choose behavior based on program name version of upcase looked at the first element of argv to get the program name. The argv array in fact has all of the program’s arguments, living at argv indexes greater than zero. Recall that argc is the number of arguments on the command line, including the program name.

Example 4.2 is a quick little program that prints out command-line arguments:

Example 4.2. dumpargs.m

/​/​ ​d​u​m​p​a​r​g​s​.​m​ ​-​-​ ​s​h​o​w​ ​p​r​o​g​r​a​m​ ​a​r​g​u​m​e​n​t​s​ ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.