@@ -432,9 +432,10 @@ def _argument_parser():
432
432
dest = "compress" ,
433
433
const = False ,
434
434
help = "Decompress the file instead of compressing." )
435
- parser .add_argument ("-c" , "--stdout" , action = "store_true" ,
435
+ output_group = parser .add_mutually_exclusive_group ()
436
+ output_group .add_argument ("-c" , "--stdout" , action = "store_true" ,
436
437
help = "write on standard output" )
437
- parser .add_argument ("-o" , "--output" , help = "Write to this output file" )
438
+ output_group .add_argument ("-o" , "--output" , help = "Write to this output file" )
438
439
parser .add_argument ("-f" , "--force" , action = "store_true" ,
439
440
help = "Overwrite output without prompting" )
440
441
# -b flag not taken by either gzip or igzip. Hidden attribute. Above 32K
@@ -451,42 +452,38 @@ def main():
451
452
452
453
compresslevel = args .compresslevel or _COMPRESS_LEVEL_TRADEOFF
453
454
454
- # Determine input file
455
- if args .compress and args .file is None :
456
- in_file = sys .stdin .buffer
457
- elif args .compress and args .file is not None :
458
- in_file = io .open (args .file , mode = "rb" )
459
- elif not args .compress and args .file is None :
460
- in_file = IGzipFile (mode = "rb" , fileobj = sys .stdin .buffer )
461
- elif not args .compress and args .file is not None :
462
- base , extension = os .path .splitext (args .file )
463
- if extension != ".gz" and not args .stdout :
464
- sys .exit (f"filename doesn't end in .gz: { args .file !r} . "
465
- f"Cannot determine output filename." )
466
- in_file = open (args .file , "rb" )
467
-
468
- # Determine output file
469
- if args .output is not None :
470
- if os .path .exists (args .output ) and not args .force :
471
- response = input (f"{ args .output } already exists; "
472
- f"do you wish to overwrite (y/n)?" )
473
- if response not in {"y" , "Y" }:
474
- sys .exit ("not overwritten" )
475
- if args .compress :
476
- out_file = IGzipFile (args .output ,
477
- mode = "wb" , compresslevel = compresslevel )
455
+ if args .file is None or args .stdout :
456
+ out_fileobj = sys .stdout .buffer
457
+ else :
458
+ if args .output is None :
459
+ if args .compress :
460
+ out_filepath = args .file + ".gz"
461
+ else :
462
+ out_filepath , extension = os .path .splitext (args .file )
463
+ if extension != ".gz" and not args .stdout :
464
+ sys .exit (f"filename doesn't end in .gz: { args .file !r} . "
465
+ f"Cannot determine output filename." )
478
466
else :
479
- out_file = io .open (args .output , "wb" )
480
- elif args .compress and (args .file is None or args .stdout ):
481
- out_file = IGzipFile (mode = "wb" , compresslevel = compresslevel ,
482
- fileobj = sys .stdout .buffer )
483
- elif args .compress and args .file is not None :
484
- out_file = open (args .file + ".gz" , mode = "wb" ,
485
- compresslevel = compresslevel )
486
- elif not args .compress and (args .file is None or args .stdout ):
487
- out_file = sys .stdout .buffer
488
- elif not args .compress and args .file is not None :
489
- out_file = io .open (base , "wb" )
467
+ out_filepath = args .output
468
+ if os .path .exists (out_filepath ) and not args .force :
469
+ yes_or_no = input (f"{ out_filepath } already exists; "
470
+ f"do you wish to overwrite (y/n)?" )
471
+ if yes_or_no not in {"y" , "Y" , "yes" }:
472
+ sys .exit ("not overwritten" )
473
+ out_fileobj = io .open (out_filepath , mode = "wb" )
474
+
475
+ if args .file is None :
476
+ in_fileobj = sys .stdin .buffer
477
+ else :
478
+ in_fileobj = io .open (args .file , mode = "rb" )
479
+
480
+ if args .compress :
481
+ in_file = in_fileobj
482
+ out_file = IGzipFile (mode = "wb" , fileobj = out_fileobj ,
483
+ compresslevel = compresslevel )
484
+ else :
485
+ in_file = IGzipFile (mode = "rb" , fileobj = in_fileobj )
486
+ out_file = out_fileobj
490
487
491
488
global READ_BUFFER_SIZE
492
489
READ_BUFFER_SIZE = args .buffer_size
0 commit comments