diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index ae10f1a3c7..0128bd6499 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -26,6 +26,7 @@ struct FormatOptions { is_noerror bool is_verify bool // exit(1) if the file is not vfmt'ed is_worker bool // true *only* in the worker processes. NB: workers can crash. + is_backup bool // make a `file.v.bak` copy *before* overwriting a `file.v` in place with `-w` } const ( @@ -53,6 +54,7 @@ fn main() { is_debug: '-debug' in args is_noerror: '-noerror' in args is_verify: '-verify' in args + is_backup: '-backup' in args } if term_colors { os.setenv('VCOLORS', 'always', true) @@ -249,6 +251,10 @@ fn (foptions &FormatOptions) post_process_file(file string, formatted_file_path } if foptions.is_w { if is_formatted_different { + if foptions.is_backup { + file_bak := '${file}.bak' + os.cp(file, file_bak) or {} + } os.mv_by_cp(formatted_file_path, file) or { panic(err) } eprintln('Reformatted file: $file') } else { diff --git a/cmd/v/help/fmt.txt b/cmd/v/help/fmt.txt index cdfc8f4edd..e9d1bf6d01 100644 --- a/cmd/v/help/fmt.txt +++ b/cmd/v/help/fmt.txt @@ -18,6 +18,9 @@ Options: -l List files whose formatting differs from vfmt. -w Write result to (source) file(s) instead of to stdout. + + -backup In combination with `-w`, copy the original `file.v` to a `file.v.bak` backup, + before overwriting the original source file. -debug Print the kinds of encountered AST statements/expressions on stderr.