From 68dd1dc265ca4bb69bca1766584735a347ff9fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Tue, 9 Oct 2018 23:44:44 +0200 Subject: [PATCH 1/3] bpo-23596: Use argparse for the command line of gzip Co-authored-by: Antony Lee --- Lib/gzip.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index ddc7bda1fecbbd2..084571e05d030cf 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -532,18 +532,17 @@ def decompress(data): return f.read() -def _test(): - # Act like gzip; with -d, act like gunzip. - # The input file is not deleted, however, nor are any other gzip - # options or features supported. - args = sys.argv[1:] - decompress = args and args[0] == "-d" - if decompress: - args = args[1:] - if not args: - args = ["-"] - for arg in args: - if decompress: +def main(): + from argparse import ArgumentParser + parser = ArgumentParser(description= + "A simple command line interface for the gzip module: act like gzip, " + "but do not delete the input file.") + parser.add_argument("-d", "--decompress", action="store_true", + help="act like gunzip instead of gzip") + parser.add_argument("args", nargs="*", default=["-"]) + args = parser.parse_args() + for arg in args.args: + if args.decompress: if arg == "-": f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer) g = sys.stdout.buffer @@ -571,4 +570,4 @@ def _test(): f.close() if __name__ == '__main__': - _test() + main() From 253c538be9bd9e70a811e5db4e6e402795db947c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Tue, 9 Oct 2018 23:51:17 +0200 Subject: [PATCH 2/3] add News entry --- Misc/NEWS.d/next/Tests/2018-10-09-23-51-07.bpo-23596.rdnert.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2018-10-09-23-51-07.bpo-23596.rdnert.rst diff --git a/Misc/NEWS.d/next/Tests/2018-10-09-23-51-07.bpo-23596.rdnert.rst b/Misc/NEWS.d/next/Tests/2018-10-09-23-51-07.bpo-23596.rdnert.rst new file mode 100644 index 000000000000000..ef71720c56fbf54 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2018-10-09-23-51-07.bpo-23596.rdnert.rst @@ -0,0 +1 @@ +Use argparse for the command line of the gzip module. Patch by Antony Lee From 502cc6dd50057affa914c913612d7b564d3ed5b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 10 Oct 2018 00:01:19 +0200 Subject: [PATCH 3/3] show file instead of args when we execute the usage --- Lib/gzip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index 084571e05d030cf..a34d01ae36e1212 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -539,7 +539,7 @@ def main(): "but do not delete the input file.") parser.add_argument("-d", "--decompress", action="store_true", help="act like gunzip instead of gzip") - parser.add_argument("args", nargs="*", default=["-"]) + parser.add_argument("args", nargs="*", default=["-"], metavar='file') args = parser.parse_args() for arg in args.args: if args.decompress: