By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,724 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

How to move optparse from main to function?

P: n/a
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

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


# begin program

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"

# end program

Feb 23 '06 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Doesn't the module documentation ...

.... tell you what you need?


Feb 23 '06 #2

P: n/a
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

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

Feb 23 '06 #3

P: n/a
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.

Feb 23 '06 #4

P: n/a
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 I
thought I could put "parser = parserSetup()" and "(options, args) =
parser.parse_args()" in the function. Thanks for helping out with that!

Feb 23 '06 #5

P: n/a
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

Good luck!

Feb 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.