How to move optparse from main to function?

B

Bob

I'm playing around with optparse and created the code below. How do I
move that to a function and what variable do I pass?
From the documentation it seems like "options.optparse_test" would have
the value zero if its option, either "-t" or "--test", is not detected
at the command line. When I issue "print options.optparse_test" when no
command options are used, "None" is sent to the screen (IIRC)... Is a
proper check if a command line argument is not used to use the
following:

# being check
if options.optparse_test <> 1:
print "test parameter NOT detected"
# end check

Thanks.

# begin program
#!/usr/bin/python

from optparse import OptionParser
import string

parser = OptionParser()
parser.add_option("-t", "--test", action="count", dest="optparse_test",
help="testing optparse")

(options, args) = parser.parse_args()
print options.optparse_test
if options.optparse_test == 1:
print "test parameter detected"
if options.optparse_test <> 1:
print "test parameter NOT detected"
#parser.print_help()

# end program
 
J

Jason Drew

As pointed out, the module documentation is helpful.

For your 'test' option, I don't think 'action="count"' is the best
action. 'Test' is basically an on/off option, so why count it? I would
use:

parser.add_option("-t", "--test", action="store_true",
dest="optparse_test", default=False, help="testing optparse")

Then your code can use
if options.optparse_test == True: ...
or briefer:
if options.optparse_test: ...


As for putting the optparse code into a function, I sometimes use:

def parserSetup():
"""Return a configured option parser for this program."""
parser = OptionParser()
parser.add_option( ... your option stuff ... )
parser.add_option( ... )
return parser

if __name__=="__main__":
parser = parserSetup()
(options, args) = parser.parse_args()
# Then in your case:
if options.optparse_test: ...
 
B

Bob

The module documentation helped me construct the meat of my code bu it
didn't lend a hand on how to build the real meal deal the way Jason's
explanation did.
 
B

Bob

Yes the documentation is helpful, but I wouldn't have been able to do
what you did in your code by just looking at section 6.21.2.9. I
thought I could put "parser = parserSetup()" and "(options, args) =
parser.parse_args()" in the function. Thanks for helping out with that!
 
J

Jason Drew

You're welcome!

As usual, each of us is free to write the code whichever way works best
for the particular problem at hand. That's why the module documentation
often avoids advocating here-is-the-one-best-way-to-do-it. I just like
sticking all the option setup stuff in a single function because it's
conceptually all the same and it makes the main() function or whatever
read shorter. It's partly down to experience, what little of it I can
claim.

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top