comparison cmd2.py @ 39:71151157ac7c

cleaned up: proper exit after -h, -v
author catherine@localhost
date Tue, 20 May 2008 12:56:05 -0400
parents 6d715d7a04fd
children 1275d59c08a7
comparison
equal deleted inserted replaced
38:6d715d7a04fd 39:71151157ac7c
19 flagReader.py options are still supported for backward compatibility 19 flagReader.py options are still supported for backward compatibility
20 """ 20 """
21 import cmd, re, os, sys, optparse, subprocess, tempfile 21 import cmd, re, os, sys, optparse, subprocess, tempfile
22 from optparse import make_option 22 from optparse import make_option
23 23
24 class Option(optparse.Option):
25 # optparse.Option.take_action keeps trying to use sys.exit. That's very rude.
26 # We don't want to exit, we just want to know so we can terminate a particular
27 # function call.
28 def take_action(self, action, dest, opt, value, values, parser):
29 if action == "help":
30 parser.print_help()
31 parser.exit()
32 setattr(values, '_exit', True)
33 elif action == "version":
34 parser.print_version()
35 parser.exit()
36 setattr(values, '_exit', True)
37
38 else:
39 return optparse.Option.take_action(self, action, dest, opt, value, values, parser)
40
41 return 1
42
43 #make_option = Option
44
45 class OptionParser(optparse.OptionParser): 24 class OptionParser(optparse.OptionParser):
46 def exit(self, status=0, msg=None): 25 def exit(self, status=0, msg=None):
47 self.values._exit = True 26 self.values._exit = True
48 if msg: 27 if msg:
49 print msg 28 print msg
50 # though we must trap and prevent the exit, we also want to end whatever command
51 # was called
52 29
53 def error(self, msg): 30 def error(self, msg):
54 """error(msg : string) 31 """error(msg : string)
55 32
56 Print a usage message incorporating 'msg' to stderr and exit. 33 Print a usage message incorporating 'msg' to stderr and exit.
57 If you override this in a subclass, it should not return -- it 34 If you override this in a subclass, it should not return -- it
58 should either exit or raise an exception. 35 should either exit or raise an exception.
59 """ 36 """
60 #print msg
61 #print self.format_help()
62 raise 37 raise
63 38
64 def options(option_list): 39 def options(option_list):
65 def option_setup(func): 40 def option_setup(func):
66 optionParser = OptionParser() 41 optionParser = OptionParser()