import string

if __name__ == "__main__":
    import sys

    template_file = sys.argv[1]

    with open(template_file) as f:
        data = f.read()

    template = string.Template(data)
    template_mapping = {}

    for item in sys.argv[2:]:
        # item is in the following form:  KEY=VALUE
        print("-> Current replacer %s" % item)
        key, value = item.split("=", 1)
        template_mapping[key] = value

    print("-> Using mapping: %s" % str(template_mapping))
    result = template.substitute(template_mapping)
    print("-----\n")
    print(result)

import string
import os


if __name__ == "__main__":
    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option("-t", "--template", dest="template_file",
                      help="Input template file")

    (options, args) = parser.parse_args()

    if not os.path.isfile(options.template_file):
        sys.stderr.write("Invalid input template file")
        exit(1)

    with open(options.template_file) as f:
        data = f.read()

    template = string.Template(data)
    template_mapping = {}

    for item in args:
        # item is in the following form:  KEY=VALUE
        print("-> Current replacer %s" % item)
        key, value = item.split("=", 1)
        template_mapping[key] = value

    print("-> Using mapping: %s" % str(template_mapping))
    result = template.substitute(template_mapping)
    print("-----\n")
    print(result)

Use OptionParser instead of simple sys.argv.
Use OptionParser instead of simple sys.argv.

  import string
+ import os
+ 
  
  if __name__ == "__main__":
-     import sys
+     from optparse import OptionParser
  
-     template_file = sys.argv[1]
+     parser = OptionParser()
+     parser.add_option("-t", "--template", dest="template_file",
+                       help="Input template file")
  
+     (options, args) = parser.parse_args()
+ 
+     if not os.path.isfile(options.template_file):
+         sys.stderr.write("Invalid input template file")
+         exit(1)
+ 
-     with open(template_file) as f:
+     with open(options.template_file) as f:
          data = f.read()
  
      template = string.Template(data)
      template_mapping = {}
  
-     for item in sys.argv[2:]:
+     for item in args:
          # item is in the following form:  KEY=VALUE
          print("-> Current replacer %s" % item)
          key, value = item.split("=", 1)
          template_mapping[key] = value
  
      print("-> Using mapping: %s" % str(template_mapping))
      result = template.substitute(template_mapping)
      print("-----\n")
      print(result)
  
Use OptionParser instead of simple sys.argv.
## Code Before:
import string

if __name__ == "__main__":
    import sys

    template_file = sys.argv[1]

    with open(template_file) as f:
        data = f.read()

    template = string.Template(data)
    template_mapping = {}

    for item in sys.argv[2:]:
        # item is in the following form:  KEY=VALUE
        print("-> Current replacer %s" % item)
        key, value = item.split("=", 1)
        template_mapping[key] = value

    print("-> Using mapping: %s" % str(template_mapping))
    result = template.substitute(template_mapping)
    print("-----\n")
    print(result)

## Instruction:
Use OptionParser instead of simple sys.argv.
## Code After:
import string
import os


if __name__ == "__main__":
    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option("-t", "--template", dest="template_file",
                      help="Input template file")

    (options, args) = parser.parse_args()

    if not os.path.isfile(options.template_file):
        sys.stderr.write("Invalid input template file")
        exit(1)

    with open(options.template_file) as f:
        data = f.read()

    template = string.Template(data)
    template_mapping = {}

    for item in args:
        # item is in the following form:  KEY=VALUE
        print("-> Current replacer %s" % item)
        key, value = item.split("=", 1)
        template_mapping[key] = value

    print("-> Using mapping: %s" % str(template_mapping))
    result = template.substitute(template_mapping)
    print("-----\n")
    print(result)
