7.5. Parsing Command-Line Options

The time has come to start thinking about how we will pass the pattern we are searching for into xgrep to replace the dummy pattern “Sean” we have been carrying around to date.

One reasonable approach would be to add a -p option. The overall command syntax looks like this:

CD-ROM reference=7029.txt
xgrep.py -p pattern <file>...<file>

Python provides a module known as getopt for handling command-line options that begin with a minus sign. This module contains a function also known as getopt that generally takes two parameters. The first parameter specifies the argument list to be processed—typically, sys.argv[1:]. The second parameter is a string specifying the option names you wish to parse for. A colon character ...

Get XML Processing with Python now with O’Reilly online learning.

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