hiral wrote:
Hi,

I am using optparser to do following...

Command syntax:
myscript -o[exension] other_arguments
    where; extension can be 'exe', 'txt', 'pdf', 'ppt' etc.


Now to parse this, I am doing following...

parser.add_option("-oexe', dest=exe_file...)
parser.add_option("-otxt', dest=txt_file...)
parser.add_option("-opdf', dest=pdf_file...)
parser.add_option("-oppt', dest=ppt_file...)

The above way is the most simple way to parser options.
Can you please suggest any other best way / optimized way to parse
these kind of options.

Thank you in advance.

Here's a solution:

import optparse

class Process:
   PREFIX = 'dispatch_'
   @staticmethod
   def undef():
       print 'unsupported file type'
   @staticmethod
   def dispatch_exe():
       print 'Hello exe file !'

def dispatchFileType(option, opt, value, parser):
   """Called by the parser, -o option."""
   # call the corresponding method in the process method
   getattr(Process, Process.PREFIX + value, Process.undef)()
parser = optparse.OptionParser()
parser.add_option("-o", "--output-fileType", type="string", action="callback", callback=dispatchFileType)

options, args = parser.parse_args()


Cheers,

JM




--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to