Created
August 27, 2015 13:03
-
-
Save aeppert/9a70521aee67cc35744d to your computer and use it in GitHub Desktop.
Python shell for having a singular script, via symlink, have multiple personalities easily.
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
#!/usr/bin/python | |
import sys | |
import os | |
from optparse import OptionParser | |
class cli_handlers(object): | |
def __init__(self, argv): | |
self.argv = argv | |
self.parser = OptionParser() | |
def process(self): | |
pass | |
class t2_handler(cli_handlers): | |
def __init__(self, argv): | |
cli_handlers.__init__(self, argv) | |
self.parser.add_option('--a', dest='a', help='a') | |
self.parser.add_option('--b', dest='b', help='b') | |
(self.options, self.args) = self.parser.parse_args(argv[1:]) | |
def process(self): | |
print 't2_handler - a = %s, b = %s' % (self.options.a, self.options.b) | |
class t3_handler(cli_handlers): | |
def __init__(self, argv): | |
cli_handlers.__init__(self, argv) | |
self.parser.add_option('--c', dest='c', help='c') | |
self.parser.add_option('--d', dest='d', help='d') | |
(self.options, self.args) = self.parser.parse_args(argv[1:]) | |
def process(self): | |
print 't3_handler - c = %s, d = %s' % (self.options.c, self.options.d) | |
cli_lookup = {'t2': t2_handler, | |
't3': t3_handler, } | |
def main(): | |
fn, ext = os.path.splitext(os.path.basename(sys.argv[0])) | |
handler = cli_lookup.get(fn, None) | |
if handler: | |
h = handler(sys.argv) | |
h.process() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment