X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=mimedecode.py;h=34c495f1cb9be2c9f1f4063a9465730aa47c8b80;hb=967e8b86e8f38ce3d551f2160eaf325253ac10c1;hp=131d551b6f8c3271e102a03f0991052fc00ca708;hpb=dafc33620aac47c26b3997a10aa4bcf72059373a;p=mimedecode.git diff --git a/mimedecode.py b/mimedecode.py index 131d551..34c495f 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -27,11 +27,13 @@ Broytman mimedecode.py version %s, %s if exit: sys.exit(0) -def usage(code=0): +def usage(code=0, errormsg=''): version(0) sys.stdout.write("""\ -Usage: %s [-h|--help] [-V|--version] [-cCDP] [-f charset] [-d header] [-p header:param] [-beit mask] [input_file [output_file]] +Usage: %s [-h|--help] [-V|--version] [-cCDP] [-f charset] [-d header] [-p header:param] [-beit mask] [-o output_file] [input_file [output_file]] """ % me) + if errormsg: + sys.stderr.write(errormsg + '\n') sys.exit(code) @@ -299,6 +301,9 @@ class GlobalOptions: ignore_mask = [] # Ignore (skip, do not decode and do not include into output) error_mask = [] # Raise error if encounter one of these + input_filename = None + output_filename = None + gopts = GlobalOptions @@ -306,7 +311,7 @@ def get_opt(): from getopt import getopt, GetoptError try: - options, arguments = getopt(sys.argv[1:], 'hVcCDPf:d:p:b:e:i:t:', + options, arguments = getopt(sys.argv[1:], 'hVcCDPf:d:p:b:e:i:t:o:', ['help', 'version']) except GetoptError: usage(1) @@ -342,6 +347,8 @@ def get_opt(): gopts.ignore_mask.append(value) elif option == '-e': gopts.error_mask.append(value) + elif option == '-o': + gopts.output_filename = value else: usage(1) @@ -353,21 +360,38 @@ if __name__ == "__main__": la = len(arguments) if la == 0: + gopts.input_filename = '-' + gopts.output_filename = '-' infile = sys.stdin + outfile = sys.stdout elif la in (1, 2): if (arguments[0] == '-'): + gopts.input_filename = '-' infile = sys.stdin else: + gopts.input_filename = arguments[0] infile = open(arguments[0], 'r') if la == 1: - outfile = sys.stdout + if gopts.output_filename: + outfile = open(gopts.output_filename, 'w') + else: + gopts.output_filename = '-' + outfile = sys.stdout elif la == 2: + if gopts.output_filename: + usage(1, 'Too many output filenames') if (arguments[1] == '-'): + gopts.output_filename = '-' outfile = sys.stdout else: + gopts.output_filename = arguments[1] outfile = open(arguments[1], 'w') else: - usage(1) + usage(1, 'Too many arguments') + + if (infile is sys.stdin) and (outfile is sys.stdout) and \ + sys.stdin.isatty() and sys.stdout.isatty(): + usage(1, 'Filtering from console to console is forbidden') gopts.outfile = outfile decode_file(infile)