option argument length

Peter Otten __peter__ at web.de
Wed Dec 7 10:55:00 EST 2005


Ritesh Raj Sarraf wrote:

>          parser.add_option("-d","--download-dir", dest="download_dir",
>          help="Root directory path to save the downloaded files",
>          action="store", type="string")
>          parser.set_defaults(download_dir="foo")

This can be simplified to

parser.add_option("-d", "--download-dir", default="foo", 
    help="Root directory path to save the downloaded files")

which seems to be the reason why I've never seen the set_defaults() call
before.

>          if len(options) != 1 or len(options) > 2:

It doesn't matter much as it won't work anyway, but

len(options) > 2 implies len(options) != 1, so

if len(options) != 1: 
    #...

would suffice here.

Now to the actual problem: I think you didn't understand my previous
question. I cannot infer from your non-working code what it actually should
do. I asked for examples of how your script would be used. E. g,
assuming the code above is in a file called sarraf.py, what should the
following invocations

./sarraf.py --fetch-update bar
./sarraf.py --fetch-update bar --the-mutually-inclusive-option baz

do? Would the first terminate with an error message that another option must
also be given? Would it use the default? Would the second be accepted? Try
to describe it as simple and clear as possible. Imagine you were talking to
someone who has never written a line of code.

Peter



More information about the Python-list mailing list