nimgrep: support for multiple input files/dirs
This commit is contained in:
parent
437eb2c37e
commit
5e657a4b34
1 changed files with 8 additions and 6 deletions
|
|
@ -16,7 +16,7 @@ const
|
||||||
|
|
||||||
(c) 2011 Andreas Rumpf
|
(c) 2011 Andreas Rumpf
|
||||||
Usage:
|
Usage:
|
||||||
nimgrep [options] [pattern] [files/directory]
|
nimgrep [options] [pattern] (file/directory)*
|
||||||
Options:
|
Options:
|
||||||
--find, -f find the pattern (default)
|
--find, -f find the pattern (default)
|
||||||
--replace, -r replace the pattern
|
--replace, -r replace the pattern
|
||||||
|
|
@ -44,7 +44,7 @@ type
|
||||||
ceAbort, ceYes, ceAll, ceNo, ceNone
|
ceAbort, ceYes, ceAll, ceNo, ceNone
|
||||||
|
|
||||||
var
|
var
|
||||||
filename = ""
|
filenames: seq[string] = @[]
|
||||||
pattern = ""
|
pattern = ""
|
||||||
replacement = ""
|
replacement = ""
|
||||||
options: TOptions
|
options: TOptions
|
||||||
|
|
@ -107,6 +107,7 @@ proc highlight(s, match, repl: string, t: tuple[first, last: int],
|
||||||
proc processFile(filename: string) =
|
proc processFile(filename: string) =
|
||||||
var buffer = system.readFile(filename)
|
var buffer = system.readFile(filename)
|
||||||
if isNil(buffer): quit("cannot open file: " & filename)
|
if isNil(buffer): quit("cannot open file: " & filename)
|
||||||
|
stdout.writeln(filename)
|
||||||
var pegp: TPeg
|
var pegp: TPeg
|
||||||
var rep: TRegex
|
var rep: TRegex
|
||||||
var result: string
|
var result: string
|
||||||
|
|
@ -204,13 +205,13 @@ for kind, key, val in getopt():
|
||||||
case kind
|
case kind
|
||||||
of cmdArgument:
|
of cmdArgument:
|
||||||
if options.contains(optStdIn):
|
if options.contains(optStdIn):
|
||||||
filename = key
|
filenames.add(key)
|
||||||
elif pattern.len == 0:
|
elif pattern.len == 0:
|
||||||
pattern = key
|
pattern = key
|
||||||
elif options.contains(optReplace) and replacement.len == 0:
|
elif options.contains(optReplace) and replacement.len == 0:
|
||||||
replacement = key
|
replacement = key
|
||||||
else:
|
else:
|
||||||
filename = key
|
filenames.add(key)
|
||||||
of cmdLongOption, cmdShortOption:
|
of cmdLongOption, cmdShortOption:
|
||||||
case normalize(key)
|
case normalize(key)
|
||||||
of "find", "f": incl(options, optFind)
|
of "find", "f": incl(options, optFind)
|
||||||
|
|
@ -242,7 +243,7 @@ if optStdin in options:
|
||||||
if pattern.len == 0:
|
if pattern.len == 0:
|
||||||
writeHelp()
|
writeHelp()
|
||||||
else:
|
else:
|
||||||
if filename.len == 0: filename = os.getCurrentDir()
|
if filenames.len == 0: filenames.add(os.getCurrentDir())
|
||||||
if optRegex notin options:
|
if optRegex notin options:
|
||||||
if optIgnoreStyle in options:
|
if optIgnoreStyle in options:
|
||||||
pattern = "\\y " & pattern
|
pattern = "\\y " & pattern
|
||||||
|
|
@ -255,5 +256,6 @@ else:
|
||||||
quit "ignorestyle not supported for regular expressions"
|
quit "ignorestyle not supported for regular expressions"
|
||||||
if optWord in options:
|
if optWord in options:
|
||||||
pattern = r"\b (:?" & pattern & r") \b"
|
pattern = r"\b (:?" & pattern & r") \b"
|
||||||
walker(filename)
|
for f in items(filenames):
|
||||||
|
walker(f)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue