Python optparse not working - syntax error -
this script based on assignment. code looks same theirs , both error out syntax error. running activestate active python 2.7 64-bit
def showevents(): ''' showevents command list events event logger following options: -t or -type (type of event search for) warning (default option) error information -g or -goback (time in hours search) integer default 100 hours >>>showevents -t warning -g 100 type = warning goback = 100 hours ''' import optparse parser = optparse.optionparser() parser.add_option('-t', '--type', \ choices=('error', 'warning', 'information', 'all' ), \ help='write type eventtype', default = 'warning') parser.add_option('-g', '--goback', \ help='write goback goback', default = '100') (options, args) = parser.parse_args() return options options = showevents() print 'type = ', options.type print 'goback =', options.goback, 'hours' if options.type == 'all': if options.goback == '24': import os os.startfile('logfile.htm') this returns defaults when runs, wont accept input. have missed?
>>> type = warning goback = 100 hours >>> showevents -t error traceback ( file "<interactive input>", line 1 showevents -t error ^ syntaxerror: invalid syntax >>> thanks looking.
the problem isn't code, way you're trying test it.
showevents -t error not valid line of python, is valid line of sh or cmd.
so, don't type python interpreter, type bash/dos prompt in terminal/dos window.
also, if you're not on windows, script have saved showevents (rather showevents.py), , have installed somewhere on path, , have have executable flag set, , need #!/usr/bin/env python or similar first line. (or can skip , type python showevents.py -t error @ bash prompt.)
on windows, can call showevents.py; long you're cd'd same directory it's saved in it's automatically on path; , there no executable flag or shebang line worry about.
Comments
Post a Comment