aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2014-08-13 19:43:11 +0200
committerLaurent Bachelier <laurent@bachelier.name>2014-08-13 19:43:11 +0200
commit026f0b5929d264e2ff094d31599440e7f18c2a48 (patch)
treeba8fb76241f00e4b721f349dcb4fcf5214bcd0a1
parentAdd README (diff)
downloadrencon-026f0b5929d264e2ff094d31599440e7f18c2a48.tar.xz
Better argparse help
-rwxr-xr-xrencon24
1 files changed, 13 insertions, 11 deletions
diff --git a/rencon b/rencon
index f36bba1..c9e4806 100755
--- a/rencon
+++ b/rencon
@@ -1,27 +1,29 @@
#!/usr/bin/env python
+import argparse
+import hashlib
import os
import sys
from string import Template
-import argparse
-import hashlib
-if __name__ == "__main__":
- parser = argparse.ArgumentParser(description='Rename files based on content.')
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(
+ description="Rename files based on their content.",
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('files', metavar='FILE', type=str, nargs='+',
- help='Files to rename')
+ help="files to rename")
parser.add_argument('-m', '--mask', nargs='?',
- help='File destination mask', default='${hash}.${ext}')
+ help="file destination mask", default='${hash}.${ext}')
parser.add_argument('-p', '--pretend', action='store_true',
- help='Do not rename, just print')
+ help="do not rename, just print")
args = parser.parse_args()
- print 'Renaming with mask: %s' % args.mask
+ print "Renaming with mask: %s" % args.mask
mask = Template(args.mask)
for f in args.files:
if not os.path.exists(f):
- print >>sys.stderr, 'File %s does not exists.' % f
+ print >>sys.stderr, "File %s does not exists." % f
else:
with open(f) as fp:
h = hashlib.sha1(fp.read()).hexdigest()
@@ -29,10 +31,10 @@ if __name__ == "__main__":
name = mask.substitute(hash=h, ext=ext)
dest = os.path.join(os.path.dirname(f), name)
if os.path.basename(f) == name:
- print 'OK %s' % name
+ print "OK %s" % name
else:
print "`%s' -> `%s'" % (f, dest)
if os.path.exists(dest):
- print >>sys.stderr, 'Destination %s already exists.' % dest
+ print >>sys.stderr, "Destination %s already exists." % dest
elif not args.pretend:
os.rename(f, dest)