]> git.phdru.name Git - mimedecode.git/commitdiff
Add an optional output file argument
authorOleg Broytman <phd@phdru.name>
Thu, 30 Jan 2014 18:15:06 +0000 (22:15 +0400)
committerOleg Broytman <phd@phdru.name>
Thu, 30 Jan 2014 18:15:06 +0000 (22:15 +0400)
mimedecode.docbook
mimedecode.py

index 2b51ec02984f10de7c088f729a3f146f48398160..cc8ee2b7a8e5e4609b35acf64f6ae01aecf38d66 100644 (file)
@@ -38,7 +38,9 @@
       <arg choice="opt">
          <option>-beit mask</option>
       </arg>
-      <arg choice="opt">filename</arg>
+      <arg choice="opt">input_file
+        <arg choice="opt">output_file</arg>
+      </arg>
    </cmdsynopsis>
 </refsynopsisdiv>
 
index 96c3ea57c1132229977f63ee85ed95e7db4a3c7d..131d551b6f8c3271e102a03f0991052fc00ca708 100755 (executable)
@@ -30,21 +30,21 @@ 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] [filename]
+Usage: %s [-h|--help] [-V|--version] [-cCDP] [-f charset] [-d header] [-p header:param] [-beit mask] [input_file [output_file]]
 """ % me)
     sys.exit(code)
 
 
-def output(s, outfile=sys.stdout):
-    outfile.write(s)
+def output(s):
+    gopts.outfile.write(s)
 
-def output_headers(msg, outfile=sys.stdout):
+def output_headers(msg):
     unix_from = msg.get_unixfrom()
     if unix_from:
         output(unix_from + '\n')
     for key, value in msg.items():
-        output("%s: %s\n" % (key, value), outfile)
-    output("\n", outfile) # End of headers
+        output("%s: %s\n" % (key, value))
+    output("\n") # End of headers
 
 
 def recode(s, charset):
@@ -302,7 +302,7 @@ class GlobalOptions:
 gopts = GlobalOptions
 
 
-def init():
+def get_opt():
     from getopt import getopt, GetoptError
 
     try:
@@ -349,14 +349,28 @@ def init():
 
 
 if __name__ == "__main__":
-    arguments = init()
+    arguments = get_opt()
 
     la = len(arguments)
-    if la >= 2:
-        usage(1)
-    if (la == 0) or (arguments[0] == '-'):
+    if la == 0:
         infile = sys.stdin
+    elif la in (1, 2):
+        if (arguments[0] == '-'):
+            infile = sys.stdin
+        else:
+            infile = open(arguments[0], 'r')
+        if la == 1:
+            outfile = sys.stdout
+        elif la == 2:
+            if (arguments[1] == '-'):
+                outfile = sys.stdout
+            else:
+                outfile = open(arguments[1], 'w')
     else:
-        infile = open(arguments[0], 'r')
+        usage(1)
 
+    gopts.outfile = outfile
     decode_file(infile)
+
+    infile.close()
+    outfile.close()