473,472 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to move optparse from main to function?

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

Feb 23 '06 #1
5 3180
Doesn't the module documentation ...

http://docs.python.org/lib/optparse-...-together.html

.... tell you what you need?

Giles

Feb 23 '06 #2
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: ...

Feb 23 '06 #3
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.

Feb 23 '06 #4
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!

Feb 23 '06 #5
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!

Feb 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hans-Joachim Widmaier | last post by:
I was really pleased when the Optik module found its way into the standard Python battery compartment, as it matched all my option parsing requirements. But, as always, there's really nothing that...
3
by: washu | last post by:
Hi, I'm was going through the module help located on http://docs.python.org/lib/optparse-store-action.html and I tried modifying it a tiny bit and things don't work. If someone could tell me...
7
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing...
1
by: sector119 | last post by:
Hi I use optparse with callback action, my callback function return some value, but optparse does not store this value, options.callback_dest always is None. How can I store callback function...
3
by: Bob | last post by:
I'd like to setup command line switches that are dependent on other switches, similar to what rpm does listed below. From the grammar below we see that the "query-options" are dependent on the...
0
by: Robert Kern | last post by:
Jeff Keasler wrote: If you code it up with unit tests and documentation, it has a good chance. But in the meantime, you can tell optparse to stop processing options using the standard "--"...
7
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour...
0
by: John O'Hagan | last post by:
Here's a strange one for you: I have a generator function which produces lists of numbers and takes options which influence the output. The generator contains a loop, and to enable the options...
1
by: John O'Hagan | last post by:
Hello, I've recently found it convenient to do something like this: options = optparse_function(sys.argv) ##print options => ##{option_one:4, option_two:, option_three:'/home/files'} ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.