optparse question

Robert Kern robert.kern at gmail.com
Mon Jan 26 20:18:46 EST 2009


On 2009-01-26 19:02, Pat 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.

parser = optparse.OptionParser()
parser.add_option('-d', '--debug', action='store_true')

options, args = parser.parse_args()
if options.debug:
     # Do debugging stuff.
else:
     # Do non-debugging stuff.

> What does it take to pass single parameter to a program?
> http://docs.python.org/library/optparse.html stated that programs always
> have options. Is that so? What about "dir /s"?

Can you quote exactly the part that you are talking about? I don't see any such 
claim.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list