summaryrefslogtreecommitdiff
path: root/replace
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2020-04-20 21:26:46 -0700
committerBen Sima <ben@bsima.me>2020-04-20 21:26:46 -0700
commita77270f11a7b49bbd290f48095ae32f62ad96648 (patch)
tree54cb97ac40d4b0ce1a8b37bab68037cd0e030302 /replace
parent842c8fe008364c22f8880cf4c1309a34143c4a11 (diff)
Add --dry to replace script
Diffstat (limited to 'replace')
-rwxr-xr-xreplace12
1 files changed, 8 insertions, 4 deletions
diff --git a/replace b/replace
index 7900bcf..ebbc4e6 100755
--- a/replace
+++ b/replace
@@ -9,7 +9,10 @@ import sys
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument(
- "-d", "--diff", action="store_true", help="print diffs for every replacement"
+ "-v", "--verbose", action="store_true", help="print diffs for every replacement"
+)
+cli.add_argument(
+ "-d", "--dry", action="store_true", help="just print what would have happened"
)
cli.add_argument("old", type=str, help="target string")
cli.add_argument("new", type=str, help="replacement string")
@@ -20,10 +23,11 @@ args = cli.parse_args()
differ = difflib.Differ()
-for line in fileinput.input(files=args.files, inplace=True):
+for line in fileinput.input(files=args.files, inplace=not args.dry):
replacement = line.replace(args.old, args.new)
- if args.diff and args.old in line:
+ if (args.verbose or args.dry) and args.old in line:
diff = list(differ.compare([line], [replacement]))
sys.stderr.write(f"{fileinput.filename()}:{fileinput.lineno()}:\n")
sys.stderr.writelines(diff)
- print(replacement, end="")
+ if not args.dry:
+ print(replacement, end="")