From: W. Trevor King Date: Mon, 31 Dec 2012 12:06:20 +0000 (-0500) Subject: mkogg.py: Upgrade from optparse to argparse X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=19119afac2000f4918c4a6caa4c633ab37d1f23c;p=blog.git mkogg.py: Upgrade from optparse to argparse --- diff --git a/posts/mkogg/mkogg.py b/posts/mkogg/mkogg.py index 581631a..f2fc1f8 100755 --- a/posts/mkogg/mkogg.py +++ b/posts/mkogg/mkogg.py @@ -462,43 +462,62 @@ def test(): if __name__ == '__main__': - import optparse + import argparse import sys - usage = '%prog [options] source-dir target-dir' - epilog = __doc__ - p = optparse.OptionParser(usage=usage, epilog=epilog) - p.format_epilog = lambda formatter: epilog+'\n' - p.add_option('-t', '--target-extension', dest='ext', - default='ogg', metavar='EXT', - help='Conversion target type (e.g. flac, mp3) (%default)') - p.add_option('-c', '--cache', dest='cache', metavar='PATH', - help=('Save conversion hashes in a cache file to avoid ' - 'repeated previous conversions.')) - p.add_option('-n', '--no-hash', dest='hash', action='store_false', - default=True, - help=("Don't hash files. Just assume matching names would " - 'have matching hashes.')) - p.add_option('-i', '--ignore', dest='ignore', metavar='REGEXP', - help=('Ignore source paths matching REGEXP.')) - p.add_option('--test', dest='test', action='store_true', default=False, - help='Run internal tests and exit') - - options,args = p.parse_args() - - if options.test: + class Formatter (argparse.RawDescriptionHelpFormatter, + argparse.ArgumentDefaultsHelpFormatter): + pass + + p = argparse.ArgumentParser( + description=__doc__.splitlines()[0], + epilog='\n'.join(__doc__.splitlines()[2:]), + formatter_class=Formatter) + p.add_argument( + '-v', '--version', action='version', + version='%(prog)s {}'.format(__version__)) + p.add_argument( + '-t', '--target-extension', dest='ext', metavar='EXT', + default='ogg', choices=['flac', 'mp3', 'ogg', 'wav'], + help='Conversion target type') + p.add_argument( + '-c', '--cache', dest='cache', metavar='PATH', + help=('Save conversion hashes in a cache file to avoid ' + 'repeated previous conversions.')) + p.add_argument( + '-n', '--no-hash', dest='hash', + default=True, action='store_const', const=False, + help=("Don't hash files. Just assume matching names would " + 'have matching hashes.')) + p.add_argument( + '-i', '--ignore', dest='ignore', metavar='REGEXP', + help='Ignore source paths matching REGEXP.') + p.add_argument( + '--test', dest='test', + default=False, action='store_const', const=True, + help='Run internal tests and exit') + p.add_argument( + 'source_dir', metavar='SOURCE', default='.', + help='Source directory') + p.add_argument( + 'target_dir', metavar='TARGET', default='.', + help='Target directory') + + args = p.parse_args() + + if args.test: sys.exit(test()) - if options.ignore is not None: - ignore_regexp = _re.compile(options.ignore) + if args.ignore: + ignore_regexp = _re.compile(args.ignore) ignore_function = ignore_regexp.match else: ignore_function = None - source_dir,target_dir = args - c = Converter(source_dir, target_dir, target_extension=options.ext, - cache_file=options.cache, hash=options.hash, - ignore_function=ignore_function) + c = Converter( + args.source_dir, args.target_dir, target_extension=args.ext, + cache_file=args.cache, hash=args.hash, + ignore_function=ignore_function) try: c.run() finally: