diff -r a1daf2d289ad Lib/zipfile.py --- a/Lib/zipfile.py Sat Sep 17 01:57:28 2016 -0500 +++ b/Lib/zipfile.py Sun Sep 18 12:41:45 2016 +0200 @@ -1950,71 +1950,54 @@ return (fname, archivename) -def main(args = None): - import textwrap - USAGE=textwrap.dedent("""\ - Usage: - zipfile.py -l zipfile.zip # Show listing of a zipfile - zipfile.py -t zipfile.zip # Test if a zipfile is valid - zipfile.py -e zipfile.zip target # Extract zipfile into target dir - zipfile.py -c zipfile.zip src ... # Create zipfile from sources - """) - if args is None: - args = sys.argv[1:] +def main(args=None): + import argparse + parser = argparse.ArgumentParser() + grp = parser.add_mutually_exclusive_group(required=True) + grp.add_argument('-l', action='store_true', help='Show listing of a zipfile') + grp.add_argument('-t', action='store_true', help='Test if a zipfile is valid') + grp.add_argument('-e', action='store_true', help='Extract zipfile into target dir') + grp.add_argument('-c', action='store_true', help='Create zipfile from sources') + parser.add_argument('zipfile', help='Zipfile name') + parser.add_argument('target', nargs='?', help='Target directory') + parser.add_argument('src', nargs='*', help='Source') - if not args or args[0] not in ('-l', '-c', '-e', '-t'): - print(USAGE, file=sys.stderr) - sys.exit(1) + args = parser.parse_args(args) - if args[0] == '-l': - if len(args) != 2: - print(USAGE, file=sys.stderr) - sys.exit(1) - with ZipFile(args[1], 'r') as zf: - zf.printdir() + if not args.c: + with ZipFile(args.zipfile) as zf: + if args.l: + return zf.printdir() + elif args.t: + msg = 'Done testing' + badfile = zf.testzip() + if badfile: + msg = f'The following enclosed file is corrupted: {badfile!r}' + return print(msg) + elif not args.target or os.path.isdir(args.target): + parser.error('target must be specified with -e option') + else: + return zf.extractall(args.target) + elif not args.src: + parser.error('at least one source needs to be specified with -c option') - elif args[0] == '-t': - if len(args) != 2: - print(USAGE, file=sys.stderr) - sys.exit(1) - with ZipFile(args[1], 'r') as zf: - badfile = zf.testzip() - if badfile: - print("The following enclosed file is corrupted: {!r}".format(badfile)) - print("Done testing") + def addToZip(zf, path, zippath): + if os.path.isfile(path): + zf.write(path, zippath, ZIP_DEFLATED) + elif os.path.isdir(path): + if zippath: + zf.write(path, zippath) + for nm in os.listdir(path): + addToZip(zf, os.path.join(path, nm), os.path.join(zippath, nm)) - elif args[0] == '-e': - if len(args) != 3: - print(USAGE, file=sys.stderr) - sys.exit(1) - - with ZipFile(args[1], 'r') as zf: - zf.extractall(args[2]) - - elif args[0] == '-c': - if len(args) < 3: - print(USAGE, file=sys.stderr) - sys.exit(1) - - def addToZip(zf, path, zippath): - if os.path.isfile(path): - zf.write(path, zippath, ZIP_DEFLATED) - elif os.path.isdir(path): - if zippath: - zf.write(path, zippath) - for nm in os.listdir(path): - addToZip(zf, - os.path.join(path, nm), os.path.join(zippath, nm)) - # else: ignore - - with ZipFile(args[1], 'w') as zf: - for path in args[2:]: - zippath = os.path.basename(path) - if not zippath: - zippath = os.path.basename(os.path.dirname(path)) - if zippath in ('', os.curdir, os.pardir): - zippath = '' - addToZip(zf, path, zippath) + with ZipFile(args.zipfile, 'w') as zf: + for path in args.src: + zippath = os.path.basename(path) + if not zippath: + zippath = os.path.basename(os.path.dirname(path)) + if zippath in (os.curdir, os.pardir): + zippath = '' + addToZip(zf, path, zippath) if __name__ == "__main__": main()