Upgrade brename and breplace to use real argparse.

This commit is contained in:
Ethan Dalool 2019-04-16 18:39:33 -07:00
parent 0dbc0728c0
commit 71670f3c99
2 changed files with 44 additions and 11 deletions

View file

@ -19,7 +19,7 @@ import sys
from voussoirkit import safeprint from voussoirkit import safeprint
def brename(transformation): def brename(transformation, autoyes=False):
old = os.listdir() old = os.listdir()
new = [eval(transformation) for x in old] new = [eval(transformation) for x in old]
pairs = [] pairs = []
@ -27,13 +27,18 @@ def brename(transformation):
if x == y: if x == y:
continue continue
pairs.append((x, y)) pairs.append((x, y))
if not loop(pairs, dry=True): if not loop(pairs, dry=True):
print('Nothing to replace') print('Nothing to replace')
return return
print('Is this correct? y/n')
if input('>').lower() not in ('y', 'yes', 'yeehaw'): ok = autoyes
return if not ok:
loop(pairs, dry=False) print('Is this correct? y/n')
ok = input('>').lower() in ('y', 'yes', 'yeehaw')
if ok:
loop(pairs, dry=False)
def excise(s, mark_left, mark_right): def excise(s, mark_left, mark_right):
''' '''
@ -81,6 +86,20 @@ def title(text):
text = first + rest + extension text = first + rest + extension
return text return text
import argparse
import sys
def brename_argparse(args):
brename(args.transformation)
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('transformation', help='python command using x as variable name')
parser.set_defaults(func=brename_argparse)
args = parser.parse_args(argv)
args.func(args)
if __name__ == '__main__': if __name__ == '__main__':
transformation = sys.argv[1] raise SystemExit(main(sys.argv[1:]))
brename(transformation)

View file

@ -1,10 +1,24 @@
''' '''
Batch rename files by replacing the first argument with the second. Batch rename files by replacing the first argument with the second.
''' '''
import argparse
import brename import brename
import sys import sys
replace_from = sys.argv[1] def breplace_argparse(args):
replace_to = sys.argv[2] command = f'x.replace("{args.replace_from}", "{args.replace_to}")'
command = 'x.replace("{f}", "{t}")'.format(f=replace_from, t=replace_to) brename.brename(command, autoyes=args.autoyes)
brename.brename(command)
def main(argv):
parser = argparse.ArgumentParser(__doc__)
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.set_defaults(func=breplace_argparse)
args = parser.parse_args(argv)
args.func(args)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))