[Python-checkins] bpo-23596: Use argparse for the command line of gzip (GH-9781)
Julien Palard
webhook-mailer at python.org
Tue Oct 9 18:41:43 EDT 2018
https://github.com/python/cpython/commit/e8bbc52debfd1b28517946d65db257e6b6d92e29
commit: e8bbc52debfd1b28517946d65db257e6b6d92e29
branch: master
author: Stéphane Wirtel <stephane at wirtel.be>
committer: Julien Palard <julien at palard.fr>
date: 2018年10月10日T00:41:33+02:00
summary:
bpo-23596: Use argparse for the command line of gzip (GH-9781)
Co-authored-by: Antony Lee <anntzer.lee at gmail.com>
files:
A Misc/NEWS.d/next/Tests/2018-10-09-23-51-07.bpo-23596.rdnert.rst
M Lib/gzip.py
diff --git a/Lib/gzip.py b/Lib/gzip.py
index ddc7bda1fecb..a34d01ae36e1 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=["-"], metavar='file')
+ 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()
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 000000000000..ef71720c56fb
--- /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
More information about the Python-checkins
mailing list