Another newbie getopt thread. ^_^

googleboy mynews44 at yahoo.com
Sat Jul 17 18:06:25 EDT 2004


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)



More information about the Python-list mailing list