nimgrep --ext support

This commit is contained in:
Araq
2011-02-02 23:03:42 +01:00
parent 5e657a4b34
commit 1eba1063bd
3 changed files with 33 additions and 8 deletions

View File

@@ -1165,7 +1165,7 @@ when defined(macosx):
proc getExecPath2(c: cstring, size: var int32): bool {.
importc: "_NSGetExecutablePath", header: "<mach-o/dyld.h>".}
proc getApplicationFilename*(): string {.rtl, extern: "nos$1".} =
proc getAppFilename*(): string {.rtl, extern: "nos$1".} =
## Returns the filename of the application's executable.
# Linux: /proc/<pid>/exe
@@ -1202,9 +1202,21 @@ proc getApplicationFilename*(): string {.rtl, extern: "nos$1".} =
var x = joinPath(p, result)
if ExistsFile(x): return x
proc getApplicationDir*(): string {.rtl, extern: "nos$1".} =
proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} =
## Returns the filename of the application's executable.
## **Deprecated since version 0.8.12**: use ``getAppFilename``
## instead.
result = getAppFilename()
proc getApplicationDir*(): string {.rtl, extern: "nos$1", deprecated.} =
## Returns the directory of the application's executable.
result = splitFile(getApplicationFilename()).dir
## **Deprecated since version 0.8.12**: use ``getAppDir``
## instead.
result = splitFile(getAppFilename()).dir
proc getAppDir*(): string {.rtl, extern: "nos$1".} =
## Returns the directory of the application's executable.
result = splitFile(getAppFilename()).dir
proc sleep*(milsecs: int) {.rtl, extern: "nos$1".} =
## sleeps `milsecs` milliseconds.

View File

@@ -16,7 +16,7 @@ const
(c) 2011 Andreas Rumpf
Usage:
nimgrep [options] [pattern] (file/directory)*
nimgrep [options] [pattern] [replacement] (file/directory)*
Options:
--find, -f find the pattern (default)
--replace, -r replace the pattern
@@ -29,8 +29,9 @@ Options:
--stdin read pattern from stdin (to avoid the shell's confusing
quoting rules)
--word, -w the pattern should have word boundaries
--ignore_case, -i be case insensitive
--ignore_style, -y be style insensitive
--ignoreCase, -i be case insensitive
--ignoreStyle, -y be style insensitive
--ext:EX1|EX2|... only search the files with the given extension(s)
--help, -h shows this help
--version, -v shows the version
"""
@@ -47,6 +48,7 @@ var
filenames: seq[string] = @[]
pattern = ""
replacement = ""
extensions: seq[string] = @[]
options: TOptions
proc ask(msg: string): string =
@@ -180,6 +182,10 @@ proc processFile(filename: string) =
else:
quit "cannot open file for overwriting: " & filename
proc hasRightExt(filename: string, exts: seq[string]): bool =
var y = splitFile(filename).ext.copy(1) # skip leading '.'
for x in items(exts):
if os.cmpPaths(x, y) == 0: return true
proc walker(dir: string) =
var isDir = false
@@ -187,7 +193,8 @@ proc walker(dir: string) =
isDir = true
case kind
of pcFile:
processFile(path)
if extensions.len == 0 or path.hasRightExt(extensions):
processFile(path)
of pcDir:
if optRecursive in options:
walker(path)
@@ -224,6 +231,7 @@ for kind, key, val in getopt():
of "word", "w": incl(options, optWord)
of "ignorecase", "i": incl(options, optIgnoreCase)
of "ignorestyle", "y": incl(options, optIgnoreStyle)
of "ext": extensions = val.split('|')
of "help", "h": writeHelp()
of "version", "v": writeVersion()
else: writeHelp()
@@ -243,7 +251,8 @@ if optStdin in options:
if pattern.len == 0:
writeHelp()
else:
if filenames.len == 0: filenames.add(os.getCurrentDir())
if filenames.len == 0:
filenames.add(os.getCurrentDir())
if optRegex notin options:
if optIgnoreStyle in options:
pattern = "\\y " & pattern

View File

@@ -28,6 +28,10 @@ Changes affecting backwards compatibility
- Operators starting with ``^`` are now right-associative and have the highest
priority.
- Deprecated ``os.getApplicationFilename``: Use ``os.getAppFilename``
instead.
- Deprecated ``os.getApplicationDir``: Use ``os.getAppDir``
instead.
Additions