Add clip_prompt feature to contentreplace.

Useful when contentreplacing strings that are difficult or
impossible to provide via the command line -- you can enable
clip prompt to read those strings off of your clipboard.
This commit is contained in:
Ethan Dalool 2019-05-27 16:10:56 -07:00
parent 49d5ebab67
commit cf28ba5541

View file

@ -2,6 +2,8 @@ import argparse
import codecs
import glob
import sys
import pyperclip
def contentreplace(filename, replace_from, replace_to, autoyes=False):
f = open(filename, 'r', encoding='utf-8')
@ -26,11 +28,23 @@ def contentreplace(filename, replace_from, replace_to, autoyes=False):
def contentreplace_argparse(args):
filenames = glob.glob(args.filename_glob)
if args.clip_prompt:
replace_from = input('Ready from')
if not replace_from:
replace_from = pyperclip.paste()
replace_to = input('Ready to')
if not replace_to:
replace_to = pyperclip.paste()
else:
replace_from = codecs.decode(args.replace_from, 'unicode_escape')
replace_to = codecs.decode(args.replace_to, 'unicode_escape')
for filename in filenames:
contentreplace(
filename,
codecs.decode(args.replace_from, 'unicode_escape'),
codecs.decode(args.replace_to, 'unicode_escape'),
replace_from,
replace_to,
autoyes=args.autoyes,
)
@ -41,6 +55,7 @@ def main(argv):
parser.add_argument('replace_from')
parser.add_argument('replace_to')
parser.add_argument('-y', '--yes', dest='autoyes', action='store_true', help='accept results without confirming')
parser.add_argument('--clip_prompt', dest='clip_prompt', action='store_true')
parser.set_defaults(func=contentreplace_argparse)
args = parser.parse_args(argv)