mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 13:30:33 +00:00
nimgrep: introduce the --rex option
This commit is contained in:
@@ -11,7 +11,7 @@ import
|
||||
os, strutils, parseopt, pegs, re, terminal
|
||||
|
||||
const
|
||||
Version = "1.3"
|
||||
Version = "1.4"
|
||||
Usage = "nimgrep - Nim Grep Utility Version " & Version & """
|
||||
|
||||
(c) 2012 Andreas Rumpf
|
||||
@@ -21,8 +21,9 @@ Options:
|
||||
--find, -f find the pattern (default)
|
||||
--replace, -r replace the pattern
|
||||
--peg pattern is a peg
|
||||
--re pattern is a regular expression (default); extended
|
||||
syntax for the regular expression is always turned on
|
||||
--re pattern is a regular expression (default)
|
||||
--rex, -x use the "extended" syntax for the regular expression
|
||||
so that whitespace is not significant
|
||||
--recursive process directories recursively
|
||||
--confirm confirm each occurrence/replacement; there is a chance
|
||||
to abort any time without touching the file
|
||||
@@ -44,7 +45,8 @@ Options:
|
||||
type
|
||||
TOption = enum
|
||||
optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin,
|
||||
optWord, optIgnoreCase, optIgnoreStyle, optVerbose, optFilenames
|
||||
optWord, optIgnoreCase, optIgnoreStyle, optVerbose, optFilenames,
|
||||
optRex
|
||||
TOptions = set[TOption]
|
||||
TConfirmEnum = enum
|
||||
ceAbort, ceYes, ceAll, ceNo, ceNone
|
||||
@@ -290,6 +292,10 @@ for kind, key, val in getopt():
|
||||
of "re":
|
||||
incl(options, optRegex)
|
||||
excl(options, optPeg)
|
||||
of "rex", "x":
|
||||
incl(options, optRex)
|
||||
incl(options, optRegex)
|
||||
excl(options, optPeg)
|
||||
of "recursive": incl(options, optRecursive)
|
||||
of "confirm": incl(options, optConfirm)
|
||||
of "stdin": incl(options, optStdin)
|
||||
@@ -345,7 +351,8 @@ else:
|
||||
pattern = r"\b(:?" & pattern & r")\b"
|
||||
if {optIgnoreCase, optIgnoreStyle} * options != {}:
|
||||
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):
|
||||
walker(rep, f, counter)
|
||||
stdout.write($counter & " matches\n")
|
||||
|
||||
Reference in New Issue
Block a user