optparse: best way
Michele Simionato
michele.simionato at gmail.com
Tue Jun 8 06:20:06 EDT 2010
On Jun 8, 10:38 am, hiral <hiralsmaill... at gmail.com> 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.
Use plac: http://pypi.python.org/pypi/plac
Here is an example:
import plac
EXTENSIONS = ('exe', 'txt', 'pdf', 'ppt')
@plac.annotations(
ext=('a valid extension', 'option', 'o', None, EXTENSIONS))
def main(ext, *args):
"Do something"
if __name__ == '__main__':
plac.call(main)
$ python myscript.py -h
usage: myscript.py [-h] [-o {exe,txt,pdf,ppt}] [args [args ...]]
Do something
positional arguments:
args
optional arguments:
-h, --help show this help message and exit
-o, --ext {exe,txt,pdf,ppt}
a valid extension
More information about the Python-list
mailing list