Move code in __name__ == '__main__' block into main() function, rewrite code so

there's no need to subclass OptionParser.
This commit is contained in:
Johannes Gijsbers 2005-01-09 01:58:02 +00:00
parent 97613ad02d
commit 2abe785fee
1 changed files with 14 additions and 16 deletions

View File

@ -583,31 +583,29 @@ class Profile:
def Stats(*args):
print 'Report generating functions are in the "pstats" module\a'
# When invoked as main program, invoke the profiler on a script
if __name__ == '__main__':
def main():
usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
if not sys.argv[1:]:
print "Usage: ", usage
sys.exit(2)
class ProfileParser(OptionParser):
def __init__(self, usage):
OptionParser.__init__(self)
self.usage = usage
parser = ProfileParser(usage)
parser = OptionParser(usage=usage)
parser.allow_interspersed_args = False
parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort",
help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
if not sys.argv[1:]:
parser.print_usage()
sys.exit(2)
(options, args) = parser.parse_args()
sys.argv[:] = args
if (len(sys.argv) > 0):
sys.path.insert(0, os.path.dirname(sys.argv[0]))
run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
else:
print "Usage: ", usage
parser.print_usage()
return parser
# When invoked as main program, invoke the profiler on a script
if __name__ == '__main__':
main()