optparse question

John Machin sjmachin at lexicon.net
Tue Jan 27 03:44:52 CET 2009


On Jan 27, 12:02 pm, Pat <P... at junk.net> wrote:
> Up until today, I never needed to pass any arguments to a Python program.
>
> I did all the requisite reading and found that I should use optparse
> instead of getopt.   I read the documentation and since the words
> "simple" and "easy" often appeared in the examples and documentation, I
> just knew that it would be a snap to implement.
>
> Problem is that all I wanted to do was pass a one flag to the program
> "-d", for to enable debug mode.  Several hours later I gave up after
> optparse complained about every variation I tried.
>
> What does it take to pass single parameter to a program?

I'm assuming that question 2 starts here. To help answer question 1
without just writing the code for you, it might help if you (a) showed
what you regard as your best effort (b) explained what part of
http://docs.python.org/library/optparse.html#handling-boolean-flag-options
you had trouble with.


> http://docs.python.org/library/optparse.htmlstated that programs always
> have options.

Does it?

>  Is that so?  What about "dir /s"?

That has one option, /s.

And this must be question 3:

>
> getopt resolved my immediate need, but I would like to know how one
> could use optparse to extract out the options from something like "dir
> /s /b".

If you mean with "/" as the option designator instead of "-": there
doesn't appear to be a documented way of doing it. You would have to
do some social engineering on the users to get them used to doing "dir
-s -b". In any case I thought the number of Windows users who know how
to fire up a Command Prompt window was diminishingly small ... you
actually have users who know how to use commands like "dir /s /b"?

Cheers,
John



More information about the Python-list mailing list