title: getopt() not enforcing required arguments?
body: <p>I'm having problems with this <code>getopt()</code> code in a script that I'm writing which does some simple file manipulation given 2 required parameters (input filename and output filename) and/or 2 optional/situational arguments (debug or help).</p>
  
  <p>Code is:</p>
  
  <pre><code>def main(argv):
      try:
          opts, args = getopt.getopt(argv, "i:o:dh", ["input-file=", "output-file=", "debug", "help"])
      except getopt.GetoptError:
          usage()
          sys.exit(2)
  
      for opt, arg in opts:
          if opt in ("-h", "--help"):
              usage()
              sys.exit()
          elif opt in ("-d", "--debug"):
              global _debug
              _debug = 1
          elif opt in ("-i", "--input-file"):
              u_input_file_name = arg
          elif opt in ("-o", "--output-file"):
              u_output_file_name = arg
  </code></pre>
  
  <p>According to the <code>getopt()</code> documentation:</p>
  
  <blockquote>
    <p>options that require an argument followed by a colon ('<code>:</code>'; i.e., the same format that Unix getopt() uses).</p>
  </blockquote>
  
  <p>The problem is that as I understand it, the variables/args followed by a <code>:</code> should be enforced as required ... but the options <code>i</code> and <code>o</code> are not being enforced.  Running this snippet garners an error about <code>u_input_file_name</code> being referenced before being assigned:</p>
  
  <pre><code>[tdelane@fbsd81-1 ~/python]$ ./inco_add_cm_mpscli.py -o google
  Traceback (most recent call last):
    File "./inco_add_cm_mpscli.py", line 57, in &lt;module&gt;
      main(sys.argv[1:])
    File "./inco_add_cm_mpscli.py", line 25, in main
      infile = open(u_input_file_name, 'r')
  UnboundLocalError: local variable 'u_input_file_name' referenced before assignment
  </code></pre>
  
  <p>What am I doing wrong?</p>