option argument length

Ritesh Raj Sarraf rrs at researchut.com
Wed Dec 7 13:57:58 EST 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Peter,


Peter Otten on Wednesday December 7 2005 21:25 wrote:

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

As per python docs, it's mentioned that "default=" has been deprecated.
That's why I changed to parser.set_defaults()

> 
>>          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:
>     #...
>

Yes, you're right. Sorry.
 
> would suffice here.
>

But will len(options) give a  meaningful output. "options" is an instance.
It gives a Value Error.

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

./sarraf.py --fetch-update /bar

If the user gives the /bar argument, the program should save the downloaded
files to /bar. But I'm assuming that the user could be dumb or too lazy, in
which case --fetch-udpate should use the parser.set_defaults value
i.e. /foo

./sarraf.py --set-upgrade foo.dat --upgrade-type minimal

set-upgrade will again write data to foo.dat. If the user doesn't pass it as
an arguemnt it should take the defaults (again whatever is there in
parser.set_defaults). This will be inclusive with the --upgrade-type option
because the user will have a choice of selecting what kind of upgrade he'd
like to do, minimal or full or blah.

For this I think this should be enough:

if not options.set_upgrade and options.upgrade_type:
        parser.error("They are mutually inclusive options")


But my main concern is what if the user doesn't pass any arguemtns. Every
option I have has a default value. So I want to check what the user has
passed.
But unfortunately the args variable has "0" as its value always.

Is my way (up till now) of using optparse logically incorrect or improper ?


Regards,

rrs
- -- 
Ritesh Raj Sarraf
RESEARCHUT -- http://www.researchut.com
"Stealing logics from one person is plagiarism, stealing from many is
research."
"Necessity is the mother of invention."

Note: Please CC me. I'm not subscribed to the list
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDlzC84Rhi6gTxMLwRAtCBAJ9z5zDQ8oyx8Jy/rLe9JrwLII3xtACfTaEV
VhMmj8OD+/p+yN/8wF6xe+8=
=atC6
-----END PGP SIGNATURE-----




More information about the Python-list mailing list