I tried optparse and this is my original script.
#!/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!!"
Now this is what I am trying to do:
- If the argument is not specified with the parameter, it will take the default value. ie
myScript.py -d will simply list the current directory or -m without any argument, the current month will be taken as an argument. - For "-month" only 01 to 12 is allowed as an argument
- Want to combine several options to perform different tasks, i.e.
myScript.py -d this_dir -m 02 will do something different than -d and -m as an individual. - It will print "No option is given !!" ONLY if the script option is missing.
Are they doable? I visited doc.python.org for possible answers, but as a beginner python, I lost myself in the pages. He greatly appreciated your help; thanks in advance. Hooray!!
Update: 16/01/11I think I'm still missing something. This is now in my script.
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()
It's my goal:
- run the script without any option, will return
option.mon [working] - run the script with the -m option, returning
option.mon [working] - run the script with the ONLY -v option, ONLY return
option.vos [does not work at all] - run the script with -m and -v, do another thing [before reaching the point]
When I run the script with only the -m option, option.mon is printed option.mon , and then option.vos , which I don't want at all. I really appreciate if someone can put me in the right direction. Hooray!!
3rd update #!/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)
This is what the script returns when launched with various parameters:
I like only:
1. `I'm 'Mac' and this month is 'Mar'` - as *result
what am I doing wrong? Hooray!!
4th update:Answering myself: in this way, I can get what I'm looking for, but I'm still not impressed.
#!/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
and now this gives me exactly what I was looking for:
Is this the only way to do this? Doesn't look like the βbest wayβ to me. Hooray!!