optparse: best way

Thomas Jollans thomas at jollans.com
Tue Jun 8 05:03:33 EDT 2010


On 2010-06-08 10:38, 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 format of options you're using here is totally non-standard. Yes,
many programs traditionally use it, but it's incompatible to the usual
UNIX and GNU recommendations. I've never actually heard of optparser,
but I'd expect it to have the usual limitations: GNU getopt (distributed
with  Python by the way), I'm using the example because I know it fairly
well, lets you use either "-o exe" or "--output-format=exe" (GNU-style
long option) here.

So I'd recommend you either live with "-o exe" and the like, or you'll
probably have to write your own option parsing routine, which shouldn't
be too difficult, but really isn't worth it IMHO.

-- Thomas
> 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.
>   




More information about the Python-list mailing list