@@ -453,38 +453,46 @@ def main():
453453
454454 compresslevel = args .compresslevel or _COMPRESS_LEVEL_TRADEOFF
455455
456- if args .file is None or args .stdout :
457- out_fileobj = sys .stdout .buffer
456+ if args .output :
457+ out_filepath = args .output
458+ elif args .stdout :
459+ out_filepath = None # to stdout
460+ elif args .file is None :
461+ out_filepath = None # to stout
458462 else :
459- if args .output is None :
460- if args .compress :
461- out_filepath = args .file + ".gz"
462- else :
463- out_filepath , extension = os .path .splitext (args .file )
464- if extension != ".gz" and not args .stdout :
465- sys .exit (f"filename doesn't end in .gz: { args .file !r} . "
466- f"Cannot determine output filename." )
463+ if args .compress :
464+ out_filepath = args .file + ".gz"
467465 else :
468- out_filepath = args .output
469- if os .path .exists (out_filepath ) and not args .force :
466+ out_filepath , extension = os .path .splitext (args .file )
467+ if extension != ".gz" and not args .stdout :
468+ sys .exit (f"filename doesn't end in .gz: { args .file !r} . "
469+ f"Cannot determine output filename." )
470+ if out_filepath is not None and not args .force :
471+ if os .path .exists (out_filepath ):
470472 yes_or_no = input (f"{ out_filepath } already exists; "
471473 f"do you wish to overwrite (y/n)?" )
472474 if yes_or_no not in {"y" , "Y" , "yes" }:
473475 sys .exit ("not overwritten" )
474- out_fileobj = io .open (out_filepath , mode = "wb" )
475-
476- if args .file is None :
477- in_fileobj = sys .stdin .buffer
478- else :
479- in_fileobj = io .open (args .file , mode = "rb" )
480476
481477 if args .compress :
482- in_file = in_fileobj
483- out_file = IGzipFile (mode = "wb" , fileobj = out_fileobj ,
484- compresslevel = compresslevel )
478+ if args .file is None :
479+ in_file = sys .stdin .buffer
480+ else :
481+ in_file = io .open (args .file , mode = "rb" )
482+ if out_filepath is not None :
483+ out_file = open (out_filepath , "wb" , compresslevel = compresslevel )
484+ else :
485+ out_file = IGzipFile (mode = "wb" , fileobj = sys .stdout .buffer ,
486+ compresslevel = compresslevel )
485487 else :
486- in_file = IGzipFile (mode = "rb" , fileobj = in_fileobj )
487- out_file = out_fileobj
488+ if args .file :
489+ in_file = open (args .file , mode = "rb" )
490+ else :
491+ in_file = IGzipFile (mode = "rb" , fileobj = sys .stdin .buffer )
492+ if out_filepath is not None :
493+ out_file = io .open (out_filepath , mode = "wb" )
494+ else :
495+ out_file = sys .stdout .buffer
488496
489497 global READ_BUFFER_SIZE
490498 READ_BUFFER_SIZE = args .buffer_size
0 commit comments