From: Oleg Broytman Date: Thu, 30 Jan 2014 18:30:38 +0000 (+0400) Subject: Add option -o output_file X-Git-Tag: v2.3.2~24 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=e817999a63e6187e7c1dbf04a05cb83db4be0505;p=mimedecode.git Add option -o output_file Useful to set the output file in case of redirected stdin: mimedecode.py -o output_file < input_file cat input_file | mimedecode.py -o output_file --- diff --git a/mimedecode.docbook b/mimedecode.docbook index cc8ee2b..29a02e0 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -38,6 +38,9 @@ + + + input_file output_file @@ -237,6 +240,17 @@ flushed to stdout. + + + -o output_file + + + Useful to set the output file in case of redirected stdin: + mimedecode.py -o output_file < input_file +cat input_file | mimedecode.py -o output_file + + + diff --git a/mimedecode.py b/mimedecode.py index 131d551..36468ec 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -30,7 +30,7 @@ Broytman mimedecode.py version %s, %s def usage(code=0): 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) sys.exit(code) @@ -299,6 +299,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 +309,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 +345,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,18 +358,31 @@ 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: # Too many output filenames + usage(1) if (arguments[1] == '-'): + gopts.output_filename = '-' outfile = sys.stdout else: + gopts.output_filename = arguments[1] outfile = open(arguments[1], 'w') else: usage(1)