Dear all,
I was trying out OptionParser() and didn't quite understand who to use it correctly. This is my sample script:

#!/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)]

def optMon():
    print "The month is: %s" % abbrMonth 

def optVos():
    print "My name is: %s" % options.vos

def optMonVos():
    print "I'm '%s' and this month is '%s'" % (options.vos,abbrMonth)


if options.mon:
    optMon()

if options.vos:
    optVos()

if options.mon and options.vos:
    optMonVos()

This is what I'm trying to do:

  1. run the script without any option, will return optMon() with default value
  2. run the script with -m option, with return optMon() with gaven value
  3. run the script with ONLY -v option, will ONLY return optVos() with given value
  4. run the script with -m and -v opting, will return optMonVos() with given values.

But this is what I get if I run the script:

# ./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'

The options.mon is always returned default value, regardless of what are the options are given. what am I doing worng? Or it;s not possible at all?

Thanks in advance. Any help would be greatly appreciated. Cheers!!

You could try

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, arguments = parser.parse_args()

if not options.vos and not options.mon:
    options.mon = strftime("%m")
if options.mon:
    abbrMonth = tuple(month_abbr)[int(options.mon)]

Thanks Gribouillis!
You did solve the main problem; it almost worked.
It just didn't work for the last option - if options.mon and options.vos: - in my original mail, until I changed the order of the if statements and modified the script a little bit like this:

def abbrMonth(m):
    mn = tuple(month_abbr)[int(m)]
    return mn

if options.mon and options.vos:
        thisMonth = abbrMonth(options.mon)
        print "I'm '%s' and this month is '%s'\n" % (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\n" % thisMonth

    if options.vos:
        print "My name is: %s\n" % options.vos

I worked just the way I wanted, although I'm not very impressed. Is this the only way of doing so? Cheers!!

Edited 5 Years Ago by MacUsers: n/a

Thanks Gribouillis!
You did solve the main problem; it almost worked.
It just didn't work for the last option - if options.mon and options.vos: - in my original mail, until I changed the order of the if statements and modified the script a little bit like this:

def abbrMonth(m):
    mn = tuple(month_abbr)[int(m)]
    return mn

if options.mon and options.vos:
        thisMonth = abbrMonth(options.mon)
        print "I'm '%s' and this month is '%s'\n" % (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\n" % thisMonth

    if options.vos:
        print "My name is: %s\n" % options.vos

I worked just the way I wanted, although I'm not very impressed. Is this the only way of doing so? Cheers!!

I'm not sure that there are other ways to do the same. You may find this link useful to understand optparse http://www.alexonlinux.com/pythons-optparse-for-human-beings . Also note that optparse was replaced by a new module called argparse with extended capabilities in recent versions of python, so if you are programming for the future, you should use argparse.

Edited 5 Years Ago by Gribouillis: n/a

Long story short: I've to stick to the v2.3 for a while on this particular machine where I'm trying this. I know it's a bad idea but there is no other way fpr me at the moment. But the good thing is it's working. Cheers!!

This question has already been answered. Start a new discussion instead.