@@ 22,6 22,7 @@ On Debian-based systems, the prerequisit
Usage:
xgrep.py [-c] [--count]
+ [-C] [--force-color]
[-i] [--indent]
[-l] [--files-with-matches]
[-L] [--files-without-match]
@@ 42,7 43,11 @@ their filenames and the XPath expression
used in the XPath expression for matching regular expressions. The option `-m`
outputs only the matching parts, without filenames or XPath expressions. The
option `-i` indents the matching parts, and the option `-n` includes namespace
-declarations. The options `-c`, `-l`, `-L`, and `-q` mimic the behaviour of
+declarations. The `-C` option preserves color and formatting codes when piping
+output through [GNU less](http://www.gnu.org/software/less/) and similar
+programs.
+
+The options `-c`, `-l`, `-L`, and `-q` mimic the behaviour of
[GNU grep](http://www.gnu.org/software/grep/). The latter option suppresses any
output, but still returns the exit status (`0` if there are matches, `1` if
there are none, and `2` for errors).
@@ 1,6 1,6 @@
#! /usr/bin/python3
# xgrep.py -- search for elements in XML files, using XPath 1.0 expressions
-# Andreas Nolda 2019-08-27
+# Andreas Nolda 2019-10-07
import sys
import argparse
@@ 8,9 8,7 @@ import re
from blessings import Terminal
from lxml import etree
-version=2.4
-
-term = Terminal()
+version=2.5
parser = argparse.ArgumentParser()
parser.add_argument("expr",
@@ 19,6 17,8 @@ parser.add_argument("files", metavar="fi
help="XML file")
parser.add_argument("-c", "--count", action="store_true",
help="count matches")
+parser.add_argument("-C", "--force-color", action="store_true",
+ help="preserve color and formatting when piping output")
parser.add_argument("-i", "--indent", action="store_true",
help="indent matches")
parser.add_argument("-l", "--files-with-matches", action="store_true",
@@ 37,6 37,8 @@ parser.add_argument("-v", "--version", a
version="{0} {1}".format(parser.prog, version))
args = parser.parse_args()
+term = Terminal(force_styling=args.force_color)
+
def insert_default_ns(expr, nsmap):
if nsmap:
list = re.split("("