Created
March 8, 2014 17:48
-
-
Save curzona/9435786 to your computer and use it in GitHub Desktop.
Recipe for interactive python command line application (REPL).
This file contains hidden or 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
| import sys | |
| from optparse import OptionParser | |
| def command(options, args): | |
| function, parameters = args[0], args[1:] | |
| print "You typed: {0}({1})".format(function, parameters) | |
| def interactive(options, args): | |
| print "Python interactive window. Type $help for a list of commands." | |
| while True: | |
| sys.stdout.write(">>> ") | |
| line = sys.stdin.readline().strip() | |
| if line.lower() in ["quit", "q", "exit"]: | |
| break | |
| columns = line.split(" ") | |
| command(options, columns) | |
| def main(): | |
| parser = OptionParser(usage='usage: %prog [options]', version="%prog {0}".format(__version__)) | |
| (options, args) = parser.parse_args() | |
| interactive(options, args) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment