On Jun 8, 3:03 pm, Jean-Michel Pichavant <jeanmic...@sequans.com> wrote: > hiralwrote: > > 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- Hide quoted text - > > - Show quoted text -
Hi JM, Here it gives... $ python above_script.py -oexe abc Hello exe file ! {'output_fileType': None} # print options ['abc'] # print args In my case I require to have 'options' to consume 'abc' like... {'output_fileType': 'abc'} Thank you. -- http://mail.python.org/mailman/listinfo/python-list