From 574751f9c7e905ce8cc57603a59cd5033ba74948 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Fri, 11 Jul 2008 17:45:58 +0200 Subject: [PATCH] gitstats: Refactored stats.py to use a main function This makes it easier to call stats.py's main through exec and as such to profile it with programs as cProfile. --- src/stats.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/stats.py b/src/stats.py index c5397ce..fa10679 100755 --- a/src/stats.py +++ b/src/stats.py @@ -65,11 +65,11 @@ class Dispatcher(): argv: The arguments to parse and dispatch. """ - if not len(argv) > 1: + if not len(argv) > 0: self.showUsageMessage() return 1 - command = argv[1] + command = argv[0] for key, value in self.commands.iteritems(): if key.startswith(command): @@ -79,10 +79,10 @@ class Dispatcher(): raise DispatchException("Unknown command '" + command + "'.") # When not specifying a command, throw in the --help switch - if len(argv) == 2: + if len(argv) == 1: argv.append("--help") - return func(*argv[2:]) + return func(*argv[1:]) commands = { "author" : author.dispatch, @@ -94,9 +94,17 @@ commands = { "-v" : version, } +def main(args): + """Main routine, dispatches the command specified in args + + If called with sys.argv, cut off the first element! + """ + + result = Dispatcher(commands).dispatch(args) + return result if __name__ == '__main__': import sys - result = Dispatcher(commands).dispatch(sys.argv) + result = main(sys.argv[1:]) sys.exit(result) -- 2.11.4.GIT