Another newbie getopt thread. ^_^

G

googleboy

Hiya!

I discovered getopt tonight after getting halfway through writing huge
amounts of looping statements to try to ensure that text entered as
parameters to my python app are within the bounds of what is allowed.

It all worked great until I realised that I don't know how to force
people to enter digits only as parameters where appropriate.

I tried doing an if statement to qa the warning value, but it didn't
seem to like what I was doing:



try:
opts, args = getopt.getopt(sys.argv[1:], "w:c:t:Vh",
["warning=", "critical=", "timeout=", "version"])
except getopt.GetoptError:
print usage
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print intro + preamble + usage + options + bugs +
query
sys.exit()
elif opt in ("-V", "--version"):
print "Plugin\nVersion number 0.2\n(c)Googleboy"
sys.exit()
elif opt in ("-w", "--warning"):
if arg in string.whitespace != 0:
print "Warning and critical values must be
between 0 and 100\n"
sys.exit()
else:
warning = int(arg)
print "warning is set to ", warning
elif opt in ("-c", "--critical"):
critical = int(arg)
print "critical is set to ", critical
elif opt in ("-t", "--timeout"):
timeout = int(arg)



What I was trying begins to confuse me too... If I make that If
statement work, will warning be defined as a variable of type integer
for me to look at later in the script? I didn't use the library's
example of a main function because I thought that would be a problem.

In other news, despite reading the
http://docs.python.org/lib/module-getopt.html site, I get an error
whenever I try to make the -h variable have a --help option. It
points an error to the : on the line where I use identical syntax to
all the other iterations and says Syntax Error: invalid token.

Any assistance on running a check and being able to make a graceful
call to sys.exit("You need to make the value of WARNING and CRITICAL
between 0 and 100.") or something that will pring out a moderately
okay error message, or the : syntax error would make me be very
happy.

Thanks for your attention,

Googleboy.


(PS sorry if this isn't too clear - it is after 8am here and I been up
all night staring at this. Like I said, I am a n00b. And I haven't
done any real programming prior to this, so I don't really know what
the heck to do now)
 
C

Chris Cioffi

Hiya!

I discovered getopt tonight after getting halfway through writing huge
amounts of looping statements to try to ensure that text entered as
parameters to my python app are within the bounds of what is allowed.
[snip]

Look at the optparse module.
http://www.python.org/doc/2.3.4/lib/module-optparse.html

It allows you to define the parameter type, making it far easier to do
any followup validation. As a bonues, it provides automatic support
for stuff like 'program --help'.

Chris
 
G

googleboy

Chris Cioffi said:
Hiya!

I discovered getopt tonight after getting halfway through writing huge
amounts of looping statements to try to ensure that text entered as
parameters to my python app are within the bounds of what is allowed.
[snip]

Look at the optparse module.
http://www.python.org/doc/2.3.4/lib/module-optparse.html

It allows you to define the parameter type, making it far easier to do
any followup validation. As a bonues, it provides automatic support
for stuff like 'program --help'.


Thanks for the tip. I am using 2.2 here, but a bit of research
pointed me off to optik.sourceforge.net and it is all beginning to
really come together now.

There is one smallish issue I am having with getopt just at the
moment, and that is with this bonus feature of automatic support for
-h, --help. I have defined a whole bunch of things (like bugs, my
email address, a preamble explaining the app's function) which I want
to print to the screen when someone requests help, not just an
obvious usuge and options definition. I think it is probably
fortunate that I have done it as 5 smaller strings instead of one huge
one, just in case there was a time when I wanted to use only a
subsection (like when someone puts in a bad value, I will print a
small error referring to the incorrect option, and the syntax example
only.)

The docs and examples don't make it clear how to do this, and when I
defined a -h, --help option it gave me a conflict error:

optik.errors.OptionConflictError: option -h/--help: conflicting option
string(s): -h, --help

Is there anyway around this?

Finally, a quick one... Is there a way for optparse to force an
integer to be between 0 and 100? I am passing in percentage
values.... It isn't hard to make a validation on the integers
afterwards, but I thought it might be nice to have this as part of
the optparse.

Thanks for the help!

googleboy.
 
J

John Lenton

The docs and examples don't make it clear how to do this, and when I
defined a -h, --help option it gave me a conflict error:

optik.errors.OptionConflictError: option -h/--help: conflicting option
string(s): -h, --help

Is there anyway around this?

OptionParser has a add_help_option that defaults to 1; also, a 'usage'
parameter that you can override the message that appears _before_ the
options.}

And you can always subclass OptionParser...
Finally, a quick one... Is there a way for optparse to force an
integer to be between 0 and 100? I am passing in percentage
values.... It isn't hard to make a validation on the integers
afterwards, but I thought it might be nice to have this as part of
the optparse.

doesn't make_option help you do this?
 
C

Christopher T King

There is one smallish issue I am having with getopt just at the
moment, and that is with this bonus feature of automatic support for
-h, --help. I have defined a whole bunch of things (like bugs, my
email address, a preamble explaining the app's function) which I want
to print to the screen when someone requests help, not just an
obvious usuge and options definition. I think it is probably
fortunate that I have done it as 5 smaller strings instead of one huge
one, just in case there was a time when I wanted to use only a
subsection (like when someone puts in a bad value, I will print a
small error referring to the incorrect option, and the syntax example
only.)

Using % substitution should work for this:

help = '%s %s %s\n%s\n%s' % (appname,version,build,author,usage)

or, if these things are defined in a dictionary:

help = '%(appname)s %(version)s %(build)s\n%(author)s\n%(usage)s' % helpinfo
 
G

googleboy

On 18 Jul 2004, googleboy wrote:
Using % substitution should work for this:
help = '%s %s %s\n%s\n%s' % (appname,version,build,author,usage)

or, if these things are defined in a dictionary:
help = '%(appname)s %(version)s %(build)s\n%(author)s\n%(usage)s' % helpinfo


Thanks for the tip! It came a few hours too late to help me. I
actually ended up defining callbacks to execute a function whenever it
detects a call to help ( -h or --help given as a parameter).


The only issue I have with optparse now came up in final QA testing,
and it isn't a huge problem. Just *might* be a big enough issue to
keep teh guys I submit it to from using it... Optparse seems to
ignore any parameters that haven't been defined by anything. I can
put any sort of spurious characters I like after the command name
interspersed amongst my real and valid switches, it performs exactly
as though they were not there. Is there any way for optparse to
automatically bail out in such cases? I haven't found anything to
help me in the docs, nor the help list archive.

Thanks again!!!

^_^

googleboy
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top