8.6. Using “Bit Twiddling” to Handle the Many Options Available

Perhaps the most striking change in the code is the handling of all the command-line options in the code. There are nine on/off options to xgrep at this point. We could have separate variables for each one, like this.

CD-ROM reference=8009.txt
MarkHits = 0
UseEventStyle = 0
UseTreeStyle = 0
Validate = 0
MatchAttributeName = 0
MatchAttributeValue = 0
MatchProcessingInstruction = 0
MatchElementTypeName = 0
MatchCharacterData = 0

We could then set these to 1 as required when processing the results from getopt. We could then pass them into XMLGrepEvents, where they would be needed to decide how to perform matches.

An alternative approach has been used that keeps all the boolean variables ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.