wanderer at dialup4less.com
Tue Jul 24 22:44:57 CEST 2012
On Jul 24, 4:31 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> On Tue, Jul 24, 2012 at 1:57 PM, Wanderer <wande... at dialup4less.com> wrote:
> > If I use the code
> > assert False, "unhandled option"
> > I get output like:
> > option -q not recognized
> > for help use --help
> > What other expressions can I use other than "unhandled option"? Is there a list somewhere?
> Are you using argparse or optparse or getopt or something else
> altogether? And where are you placing this assert? It would be
> helpful to see some actual code to understand what you are doing.
> And by the way, assert is a very bad way to check user input or to
> unconditionally raise an exception. The reason is that if Python is
> invoked with -O, then all assertions are removed from the compiled
> bytecode, and then your unconditional exception code doesn't raise any
> exception at all. If you want to raise an exception, just do it:
> raise Exception("unhandled option")
> Ideally, you would also subclass Exception to create a more specific
> exception class for your custom exception:
> class UnhandledOptionException(Exception):
> # Then, later on...
> raise UnhandledOptionException("-q")
I'm using getopt but not at that point. I really don't have a problem.
I'm just curious. I've never seen anything else after
Here is some code.
help_message = \
("\nOtFixture.py:\n Set the Optics Test Fixture Light Source Light
" -l, --level= <The light level percent of Max: 0.0 to 100.0>\n"
" -v, --verbose: Print messages to the terminal.\n"
" -h, --help: This message\n")
level = None
verbose = False
helpflag = False
options = "hl:v"
long_options = ["help","level=","verbose"]
if argv is None:
argv = sys.argv
opts, _args = getopt.getopt(argv[1:],
except getopt.error, msg:
for o, a in opts:
if o in ("-h", "--help"):
helpflag = True
elif o in ("-l", "--level"):
level = a
elif o in ("-v", "--verbose"):
verbose = True
assert False, "unhandled option"
if not helpflag:
if level == None:
level = raw_input("Enter the light level from 0.0 to
if level.replace(".", "", 1).isdigit():
level = float(level)
msg = "\n" + str(level) + " is not a number.\n"
if verbose and level is not None:
print "The level is ", level, " percent"
if level is not None:
if 0.0 <= level <= 100.0:
ot = OtFixture(verbose)
print "Light Level set to ", level,"%."
msg = "\n" + str(level) + " is not in the range 0.0 to
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
if __name__ == "__main__":
More information about the Python-list