Created
November 30, 2011 04:29
-
-
Save shoma/1408020 to your computer and use it in GitHub Desktop.
shell like main loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
def call_command(name, *args): | |
""" | |
call method from command module. | |
command class have run method. | |
""" | |
command = import_subcommand(name) | |
cmd = command(args) | |
cmd.run() | |
def import_subcommand(name): | |
""" | |
dynamic import a subcommand module from commands dir. | |
""" | |
__import__("commands.%s" % name) | |
return sys.modules[name] | |
if __name__ == '__main__': | |
""" | |
main loop | |
""" | |
while 1: | |
input = raw_input("> ") | |
if input: | |
name = input.split(' ')[0] | |
args = input.split(' ')[1:] | |
try: | |
call_command(name, args) | |
except Exception, msg: | |
print msg | |
# vim: fenc=utf8 et sw=2 ts=2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment