optparse variable parsing?
David Goodger
goodger at python.org
Tue Mar 9 09:25:04 EST 2004
CJ Kucera wrote:
> I'm wondering if it's possible to have an option with optparse
> function as either a true/false option,
Often called a "switch".
> or optionally use a supplied value when it's provided.
An "option argument". The answer is no, optparse doesn't support
optional option arguments. The main reason is that it makes a command
like "cmd -abc" impossible to parse reliably: is it equivalent to
"cmd -a bc" or to "cmd -a -b -c"? In the Python tradition, optparse
chooses not to guess.
> For example, let's say that I'm writing a util to add a user to some
> system. By default, the program would assign a random password:
>
> > $ adduser.py
> > Assigned password: <blah>
>
> If the user specifies "-p", the program would prompt for a password
> instead:
>
> > $ adduser.py -p
> > Pass:
> > Confirm:
>
> And then it'd be great to be able to pass in a hashed value and have
> the program use that instead:
>
> > $ adduser.py -p\$1\$KgM/m2Ys\$.o0sbvwHddeKMCKJdfcMJ0
I would suggest that you should have two different options here, a
simple switch and an option which expects/requires an argument.
That's easier to document as well.
-- David Goodger
More information about the Python-list
mailing list