Skip to content

Instantly share code, notes, and snippets.

@shoma
Created November 30, 2011 04:29
Show Gist options
  • Save shoma/1408020 to your computer and use it in GitHub Desktop.
Save shoma/1408020 to your computer and use it in GitHub Desktop.
shell like main loop
# -*- 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