summary refs log tree commit diff stats
path: root/nimpretty/nimpretty.nim
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2019-01-08 01:38:34 -0800
committerAndreas Rumpf <rumpf_a@web.de>2019-01-08 10:38:34 +0100
commit821920aa391c1361ee17befedf4452edba47c87c (patch)
tree144c91c398eb699d6c500d4054a8d0f798961874 /nimpretty/nimpretty.nim
parent427435a814248b8b5778aa0d78f128a3f60978cd (diff)
downloadNim-821920aa391c1361ee17befedf4452edba47c87c.tar.gz
[nimpretty] fix #10211; fix #10199 (#10212)
* [nimpretty] fix #10211; fix #10199
* address comments
* un-document --backup and set its default to false
Diffstat (limited to 'nimpretty/nimpretty.nim')
-rw-r--r--nimpretty/nimpretty.nim24
1 files changed, 16 insertions, 8 deletions
diff --git a/nimpretty/nimpretty.nim b/nimpretty/nimpretty.nim
index 628bc163e..c6c558f92 100644
--- a/nimpretty/nimpretty.nim
+++ b/nimpretty/nimpretty.nim
@@ -25,11 +25,10 @@ const
 Usage:
   nimpretty [options] file.nim
 Options:
-  --backup:on|off     create a backup file before overwritting (default: ON)
-  --output:file       set the output file (default: overwrite the .nim file)
-  --indent:N          set the number of spaces that is used for indentation
-  --version           show the version
-  --help              show this help
+  --output:file         set the output file (default: overwrite the input file)
+  --indent:N[=2]        set the number of spaces that is used for indentation
+  --version             show the version
+  --help                show this help
 """
 
 proc writeHelp() =
@@ -62,7 +61,11 @@ proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
 
 proc main =
   var infile, outfile: string
-  var backup = true
+  var backup = false
+    # when `on`, create a backup file of input in case
+    # `prettyPrint` could over-write it (note that the backup may happen even
+    # if input is not actually over-written, when nimpretty is a noop).
+    # --backup was un-documented (rely on git instead).
   var opt: PrettyOptions
   for kind, key, val in getopt():
     case kind
@@ -79,9 +82,14 @@ proc main =
     of cmdEnd: assert(false) # cannot happen
   if infile.len == 0:
     quit "[Error] no input file."
+  if outfile.len == 0:
+    outfile = infile
+  if not existsFile(outfile) or not sameFile(infile, outfile):
+    backup = false # no backup needed since won't be over-written
   if backup:
-    os.copyFile(source=infile, dest=changeFileExt(infile, ".nim.backup"))
-  if outfile.len == 0: outfile = infile
+    let infileBackup = infile & ".backup" # works with .nim or .nims
+    echo "writing backup " & infile & " > " & infileBackup
+    os.copyFile(source = infile, dest = infileBackup)
   prettyPrint(infile, outfile, opt)
 
 main()