title: understanding OptionParser
body: <p>I was trying out <code>optparse</code> and this is my initial script.</p>
  
  <pre><code>#!/usr/bin/env python
  
  import os, sys
  from optparse import OptionParser
  
  parser = OptionParser()
  usage = "usage: %prog [options] arg1 arg2"
  
  parser.add_option("-d", "--dir", type="string",
                    help="List of directory",
                    dest="inDir", default=".")
  
  parser.add_option("-m", "--month", type="int",
                    help="Numeric value of the month", 
                    dest="mon")
  
  options, arguments = parser.parse_args()
  
  if options.inDir:
      print os.listdir(options.inDir)
  
  if options.mon:
      print options.mon
  
  def no_opt()
      print "No option has been given!!"
  </code></pre>
  
  <p>Now, this is what I'm trying to do:</p>
  
  <ol>
  <li>If no argument is given with the option, it will take the "default" value.
  i.e <code>myScript.py -d</code> will just list the present directory or <code>-m</code> without any argument will take the current month as an argument.</li>
  <li>For the "--month" only 01 to 12 are allowed as an argument</li>
  <li>Want to combine more than one option for performing different tasks i.e. <code>myScript.py -d this_dir -m 02</code> will do different thing than -d and -m as individual. </li>
  <li>It will print "<em>No option has been given!!</em>" ONLY if no option is supplied with the script.</li>
  </ol>
  
  <p>Are these doable? I did visit the doc.python.org site for possible answers, but as a python-beginner, I found myself lost in the pages. It's very much appreciated your help; thanks in advance. Cheers!!</p>
  
  <p><hr><strong>Update: 16/01/11</strong></p>
  
  <p>I think I'm still missing something. This is the thing in my script now.  </p>
  
  <pre><code>parser = OptionParser()
  usage = "usage: %prog [options] arg1 arg2"
  
  parser.add_option("-m", "--month", type="string",
                    help="select month from  01|02|...|12",
                    dest="mon", default=strftime("%m"))
  
  parser.add_option("-v", "--vo", type="string",
                    help="select one of the supported VOs",
                    dest="vos")
  
  options, arguments = parser.parse_args()
  </code></pre>
  
  <p>These are my goal:</p>
  
  <ol>
  <li>run the script without any option, will return <code>option.mon</code> [<em>working</em>]</li>
  <li>run the script with -m option, with return <code>option.mon</code> [<em>working</em>]</li>
  <li>run the script with ONLY -v option, will ONLY return <code>option.vos</code> [<em>not working at all</em>]</li>
  <li>run the script with -m and -v opting, will do different thing [<em>yet to get to the point</em>]</li>
  </ol>
  
  <p>When I run the script with only -m option, it's printing <code>option.mon</code> first and then <code>option.vos</code>, which I don't want at all. Really appreciate if anyone can put me in the right direction. Cheers!!</p>
  
  <p><hr><strong>3rd Update</strong></p>
  
  <pre><code>    #!/bin/env python
  
      from time import strftime
      from calendar import month_abbr
      from optparse import OptionParser
  
      # Set the CL options 
      parser = OptionParser()
      usage = "usage: %prog [options] arg1 arg2"
  
      parser.add_option("-m", "--month", type="string",
                        help="select month from  01|02|...|12", 
                dest="mon", default=strftime("%m"))
  
      parser.add_option("-u", "--user", type="string",
                        help="name of the user", 
                dest="vos")
  
      options, arguments = parser.parse_args()
  
      abbrMonth = tuple(month_abbr)[int(options.mon)]
  
      if options.mon:
          print "The month is: %s" % abbrMonth 
  
      if options.vos:
          print "My name is: %s" % options.vos 
  
      if options.mon and options.vos:
          print "I'm '%s' and this month is '%s'" % (options.vos,abbrMonth)
  </code></pre>
  
  <p>This is what the script returns when run with various options:   </p>
  
  <pre><code># ./test.py
  The month is: Feb
  #
  # ./test.py -m 12
  The month is: Dec
  #
  # ./test.py -m 3 -u Mac
  The month is: Mar
  My name is: Mac
  I'm 'Mac' and this month is 'Mar'
  #
  # ./test.py -u Mac
  The month is: Feb
  My name is: Mac
  I'm 'Mac' and this month is 'Feb'
  </code></pre>
  
  <p>I like to see only: </p>
  
  <pre><code> 1. `I'm 'Mac' and this month is 'Mar'` - as *result #3*  
   2. `My name is: Mac` - as *result #4*
  </code></pre>
  
  <p>what am I doing wrong? Cheers!!</p>
  
  <p><hr><strong>4th Update:</strong></p>
  
  <p>Answering to myself: this way I can get what I'm looking for but I'm still not impressed though.</p>
  
  <pre><code>#!/bin/env python
  
  import os, sys
  from time import strftime
  from calendar import month_abbr
  from optparse import OptionParser
  
  def abbrMonth(m):
      mn = tuple(month_abbr)[int(m)]
      return mn
  
  # Set the CL options 
  parser = OptionParser()
  usage = "usage: %prog [options] arg1 arg2"
  
  parser.add_option("-m", "--month", type="string",
                    help="select month from  01|02|...|12",
                    dest="mon")
  
  parser.add_option("-u", "--user", type="string",
                    help="name of the user",
                    dest="vos")
  
  (options, args) = parser.parse_args()
  
  if options.mon and options.vos:
      thisMonth = abbrMonth(options.mon)
      print "I'm '%s' and this month is '%s'" % (options.vos, thisMonth)
      sys.exit(0)
  
  if not options.mon and not options.vos:
      options.mon = strftime("%m")
  
  if options.mon:
      thisMonth = abbrMonth(options.mon)
      print "The month is: %s" % thisMonth
  
  if options.vos:
      print "My name is: %s" % options.vos
  </code></pre>
  
  <p>and now this gives me exactly what I was looking for:</p>
  
  <pre><code># ./test.py 
  The month is: Feb
  
  # ./test.py -m 09
  The month is: Sep
  
  # ./test.py -u Mac
  My name is: Mac
  
  # ./test.py -m 3 -u Mac
  I'm 'Mac' and this month is 'Mar'
  </code></pre>
  
  <p>Is this the only way of doing so? Doesn't look the "best way" to me. Cheers!!</p>