Upgrade brename and breplace to use real argparse.
This commit is contained in:
parent
0dbc0728c0
commit
71670f3c99
2 changed files with 44 additions and 11 deletions
|
@ -19,7 +19,7 @@ import sys
|
|||
from voussoirkit import safeprint
|
||||
|
||||
|
||||
def brename(transformation):
|
||||
def brename(transformation, autoyes=False):
|
||||
old = os.listdir()
|
||||
new = [eval(transformation) for x in old]
|
||||
pairs = []
|
||||
|
@ -27,12 +27,17 @@ def brename(transformation):
|
|||
if x == y:
|
||||
continue
|
||||
pairs.append((x, y))
|
||||
|
||||
if not loop(pairs, dry=True):
|
||||
print('Nothing to replace')
|
||||
return
|
||||
|
||||
ok = autoyes
|
||||
if not ok:
|
||||
print('Is this correct? y/n')
|
||||
if input('>').lower() not in ('y', 'yes', 'yeehaw'):
|
||||
return
|
||||
ok = input('>').lower() in ('y', 'yes', 'yeehaw')
|
||||
|
||||
if ok:
|
||||
loop(pairs, dry=False)
|
||||
|
||||
def excise(s, mark_left, mark_right):
|
||||
|
@ -81,6 +86,20 @@ def title(text):
|
|||
text = first + rest + extension
|
||||
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__':
|
||||
transformation = sys.argv[1]
|
||||
brename(transformation)
|
||||
raise SystemExit(main(sys.argv[1:]))
|
||||
|
|
|
@ -1,10 +1,24 @@
|
|||
'''
|
||||
Batch rename files by replacing the first argument with the second.
|
||||
'''
|
||||
import argparse
|
||||
import brename
|
||||
import sys
|
||||
|
||||
replace_from = sys.argv[1]
|
||||
replace_to = sys.argv[2]
|
||||
command = 'x.replace("{f}", "{t}")'.format(f=replace_from, t=replace_to)
|
||||
brename.brename(command)
|
||||
def breplace_argparse(args):
|
||||
command = f'x.replace("{args.replace_from}", "{args.replace_to}")'
|
||||
brename.brename(command, autoyes=args.autoyes)
|
||||
|
||||
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:]))
|
||||
|
|
Loading…
Reference in a new issue