nimgrep: introduce the --rex option

This commit is contained in:
Andreas Rumpf 2019-07-25 12:59:09 +02:00
commit b9c15371a3

View file

@ -11,7 +11,7 @@ import
os, strutils, parseopt, pegs, re, terminal os, strutils, parseopt, pegs, re, terminal
const const
Version = "1.3" Version = "1.4"
Usage = "nimgrep - Nim Grep Utility Version " & Version & """ Usage = "nimgrep - Nim Grep Utility Version " & Version & """
(c) 2012 Andreas Rumpf (c) 2012 Andreas Rumpf
@ -21,8 +21,9 @@ Options:
--find, -f find the pattern (default) --find, -f find the pattern (default)
--replace, -r replace the pattern --replace, -r replace the pattern
--peg pattern is a peg --peg pattern is a peg
--re pattern is a regular expression (default); extended --re pattern is a regular expression (default)
syntax for the regular expression is always turned on --rex, -x use the "extended" syntax for the regular expression
so that whitespace is not significant
--recursive process directories recursively --recursive process directories recursively
--confirm confirm each occurrence/replacement; there is a chance --confirm confirm each occurrence/replacement; there is a chance
to abort any time without touching the file to abort any time without touching the file
@ -44,7 +45,8 @@ Options:
type type
TOption = enum TOption = enum
optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin, optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin,
optWord, optIgnoreCase, optIgnoreStyle, optVerbose, optFilenames optWord, optIgnoreCase, optIgnoreStyle, optVerbose, optFilenames,
optRex
TOptions = set[TOption] TOptions = set[TOption]
TConfirmEnum = enum TConfirmEnum = enum
ceAbort, ceYes, ceAll, ceNo, ceNone ceAbort, ceYes, ceAll, ceNo, ceNone
@ -290,6 +292,10 @@ for kind, key, val in getopt():
of "re": of "re":
incl(options, optRegex) incl(options, optRegex)
excl(options, optPeg) excl(options, optPeg)
of "rex", "x":
incl(options, optRex)
incl(options, optRegex)
excl(options, optPeg)
of "recursive": incl(options, optRecursive) of "recursive": incl(options, optRecursive)
of "confirm": incl(options, optConfirm) of "confirm": incl(options, optConfirm)
of "stdin": incl(options, optStdin) of "stdin": incl(options, optStdin)
@ -345,7 +351,8 @@ else:
pattern = r"\b(:?" & pattern & r")\b" pattern = r"\b(:?" & pattern & r")\b"
if {optIgnoreCase, optIgnoreStyle} * options != {}: if {optIgnoreCase, optIgnoreStyle} * options != {}:
reflags.incl reIgnoreCase reflags.incl reIgnoreCase
let rep = re(pattern, reflags) let rep = if optRex in options: rex(pattern, reflags)
else: re(pattern, reflags)
for f in items(filenames): for f in items(filenames):
walker(rep, f, counter) walker(rep, f, counter)
stdout.write($counter & " matches\n") stdout.write($counter & " matches\n")