parser.add_argument('-m','--match',help='Regex to indicate a valid/desired stanza to output',default='.', action='store',metavar="/r/")
parser.add_argument('-f','--file',help='File to operate on, defaults to stdin',metavar='FILE',required=False, default=None,dest='filename',action='store')
args = parser.parse_known_args(sys.argv[1:])
# Move namespace into separate var
cmd=args[0]
# Set up positional args
start=cmd.start
match=cmd.match
end=cmd.end
def debug(line):
# print line
pass
if cmd.debug:
print line
debug(pformat(args))
# Set up the file handle, <file> or stdin
if cmd.filename:
handle=open(cmd.filename)
else:
handle=sys.stdin
# Go over the handle, line by line
for line in handle:
# Start buffering if we hit the start regex
if re.search(start,line):
buffering=True
debug('Started buffering on '+start)
# Continue buffering if we've already started
if buffering:
output+=line
if re.search(match,line):
# If a line in the stanza matches, we output the stanza
if buffering and re.search(match,line):
matched=True
debug('Matched on '+match)
if re.search(end,line):
# Output/clear buffer when we encounter an end to the stanza