nimgrep: add --filenames option

This commit is contained in:
Andreas Rumpf 2018-04-23 10:37:19 +02:00
commit 64908bf171

View file

@ -11,7 +11,7 @@ import
os, strutils, parseopt, pegs, re, terminal os, strutils, parseopt, pegs, re, terminal
const const
Version = "1.1" Version = "1.2"
Usage = "nimgrep - Nim Grep Utility Version " & Version & """ Usage = "nimgrep - Nim Grep Utility Version " & Version & """
(c) 2012 Andreas Rumpf (c) 2012 Andreas Rumpf
@ -35,6 +35,8 @@ Options:
--nocolor output will be given without any colours. --nocolor output will be given without any colours.
--oneline show file on each matched line --oneline show file on each matched line
--verbose be verbose: list every processed file --verbose be verbose: list every processed file
--filenames find the pattern in the filenames, not in the contents
of the file
--help, -h shows this help --help, -h shows this help
--version, -v shows the version --version, -v shows the version
""" """
@ -42,7 +44,7 @@ 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 optWord, optIgnoreCase, optIgnoreStyle, optVerbose, optFilenames
TOptions = set[TOption] TOptions = set[TOption]
TConfirmEnum = enum TConfirmEnum = enum
ceAbort, ceYes, ceAll, ceNo, ceNone ceAbort, ceYes, ceAll, ceNo, ceNone
@ -135,11 +137,14 @@ proc processFile(pattern; filename: string) =
filenameShown = true filenameShown = true
var buffer: string var buffer: string
try: if optFilenames in options:
buffer = system.readFile(filename) buffer = filename
except IOError: else:
echo "cannot open file: ", filename try:
return buffer = system.readFile(filename)
except IOError:
echo "cannot open file: ", filename
return
if optVerbose in options: if optVerbose in options:
stdout.writeLine(filename) stdout.writeLine(filename)
stdout.flushFile() stdout.flushFile()
@ -293,6 +298,7 @@ for kind, key, val in getopt():
of "nocolor": useWriteStyled = false of "nocolor": useWriteStyled = false
of "oneline": oneline = true of "oneline": oneline = true
of "verbose": incl(options, optVerbose) of "verbose": incl(options, optVerbose)
of "filenames": incl(options, optFilenames)
of "help", "h": writeHelp() of "help", "h": writeHelp()
of "version", "v": writeVersion() of "version", "v": writeVersion()
else: writeHelp() else: writeHelp()
@ -304,6 +310,7 @@ when defined(posix):
checkOptions({optFind, optReplace}, "find", "replace") checkOptions({optFind, optReplace}, "find", "replace")
checkOptions({optPeg, optRegex}, "peg", "re") checkOptions({optPeg, optRegex}, "peg", "re")
checkOptions({optIgnoreCase, optIgnoreStyle}, "ignore_case", "ignore_style") checkOptions({optIgnoreCase, optIgnoreStyle}, "ignore_case", "ignore_style")
checkOptions({optFilenames, optReplace}, "filenames", "replace")
if optStdin in options: if optStdin in options:
pattern = ask("pattern [ENTER to exit]: ") pattern = ask("pattern [ENTER to exit]: ")