# HG changeset patch # User Andreas Nolda # Date 1587901479 -7200 # Sun Apr 26 13:44:39 2020 +0200 # Node ID 254dec1347bf8be5a621307b2eb6bd7aba7b8473 # Parent e7ca88397ceaef67399335c29d3f5cbf7b1a0e99 add option "-a" ("--abbreviate") diff --git a/ChangeLog.md b/ChangeLog.md --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,8 +1,15 @@ +2020-04-26: + +* add option `-a` (`--abbreviate`) +* bump version to 2.9 + + 2020-04-25: * add option `-M` (`--files-and-matches`) * bump version to 2.8 + 2020-04-24: * rename option `-n` (`--declare-ns`) to `-N` diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ Usage: - xgrep.py [-c] [--count] + xgrep.py [-a] [--abbreviate] + [-c] [--count] [-C] [--force-color] [-i] [--indent] [-l] [--files-with-matches] @@ -46,8 +47,9 @@ [EXSLT function `:test()`](http://exslt.org/regexp/functions/test/) can be used in the XPath expression for matching regular expressions. The option `-i` indents the matching parts, and the option `-N` includes namespace declarations. -The `-C` option preserves color and formatting codes when piping output through -[GNU less](http://www.gnu.org/software/less/) and similar programs. +Matching parts can be abbreviated to their first line by means of the option +`-a`. The `-C` option preserves color and formatting codes when piping output +through [GNU less](http://www.gnu.org/software/less/) or similar programs. The options `-c`, `-l`, `-L`, `-n`, and `-q` mimic the behaviour of [GNU grep](http://www.gnu.org/software/grep/). The latter option suppresses any diff --git a/xgrep.py b/xgrep.py --- a/xgrep.py +++ b/xgrep.py @@ -1,6 +1,6 @@ #! /usr/bin/python3 # xgrep.py -- search for elements in XML files, using XPath 1.0 expressions -# Andreas Nolda 2020-04-25 +# Andreas Nolda 2020-04-26 import sys import argparse @@ -8,13 +8,15 @@ from blessings import Terminal from lxml import etree -version=2.8 +version=2.9 parser = argparse.ArgumentParser() parser.add_argument("expr", help="XPath 1.0 expression") parser.add_argument("files", metavar="file", nargs="+", help="XML file") +parser.add_argument("-a", "--abbreviate", action="store_true", + help="abbreviate matches") parser.add_argument("-c", "--count", action="store_true", help="count matches") parser.add_argument("-C", "--force-color", action="store_true", @@ -78,6 +80,11 @@ pretty_print=args.indent).decode() if not args.indent: string = re.sub("\n\s+", "\n", string) + if args.abbreviate: + lines = string.splitlines() + string = lines[0] + if len(lines) > 1: + string += " ..." if string.endswith("\n"): string = string[:-1] return string