bpo-23596: Use argparse for the command line of gzip (GH-9781) · python/cpython@e8bbc52 (original) (raw)

`@@ -532,18 +532,17 @@ def decompress(data):

`

532

532

`return f.read()

`

533

533

``

534

534

``

535

``

`-

def _test():

`

536

``

`-

Act like gzip; with -d, act like gunzip.

`

537

``

`-

The input file is not deleted, however, nor are any other gzip

`

538

``

`-

options or features supported.

`

539

``

`-

args = sys.argv[1:]

`

540

``

`-

decompress = args and args[0] == "-d"

`

541

``

`-

if decompress:

`

542

``

`-

args = args[1:]

`

543

``

`-

if not args:

`

544

``

`-

args = ["-"]

`

545

``

`-

for arg in args:

`

546

``

`-

if decompress:

`

``

535

`+

def main():

`

``

536

`+

from argparse import ArgumentParser

`

``

537

`+

parser = ArgumentParser(description=

`

``

538

`+

"A simple command line interface for the gzip module: act like gzip, "

`

``

539

`+

"but do not delete the input file.")

`

``

540

`+

parser.add_argument("-d", "--decompress", action="store_true",

`

``

541

`+

help="act like gunzip instead of gzip")

`

``

542

`+

parser.add_argument("args", nargs="*", default=["-"], metavar='file')

`

``

543

`+

args = parser.parse_args()

`

``

544

`+

for arg in args.args:

`

``

545

`+

if args.decompress:

`

547

546

`if arg == "-":

`

548

547

`f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer)

`

549

548

`g = sys.stdout.buffer

`

`@@ -571,4 +570,4 @@ def _test():

`

571

570

`f.close()

`

572

571

``

573

572

`if name == 'main':

`

574

``

`-

_test()

`

``

573

`+

main()

`