Python : parsing the command line options using optparse

Peter Otten __peter__ at
Wed Feb 26 12:27:17 CET 2014

Ganesh Pal wrote:

> On Tue, Feb 25, 2014 at 9:55 PM, Peter Otten <__peter__ at> wrote:
>>As you are just starting I recommend that you use argparse instead of
>  optparse.
>  I would love to use argparse but  the script that I plan to write has to
> run on host machines that Python 2.6
>  I have freebsd clients  with python  2.6  dont want to install python new
> version on all the host machine which will be eventually upgraded to 2.7 .
> I wanted know if I could use argparse with python 2.6 and is it possible
> to
> add  something like   #pkg_add -r install python-argparse and install
> python argparse module before I use it.

Probably, but I have no experience with freebsd.

>> >If you are asking why short options don't work in conjunction with = --
>> >I don't know, it is probably a design choice of the optparse author.
>> >argparse accepts short options with like -f=1234
>  I wanted to know why my sample program does not work  with short hand
> option (-p) and works with long hand option .
> Here is what is happening ( only short hand with -)
> # -p=/ifs/1.txt -q=XOR  -f=1234 -n=1 -l
> Usage: [options]
> error: option -q: invalid choice: '=XOR' (choose from 'XOR',
> 'ADD',
> Result :says  invalid choice: '=XOR'

If you stick with optparse just pass the options without '='



-q XOR

should both work.

More information about the Python-list mailing list