O'Reilly logo

XML Processing with Python by Sean McGrath

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required