Last active
November 22, 2022 12:47
-
-
Save andreburto/15e9c03d34d014f12fff6b7ca3f2b879 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
##### | |
import argparse | |
import sys | |
class MyToys: | |
@classmethod | |
def setup_args(cls, subparsers): | |
pass | |
def __init__(self, args): | |
self.args = args | |
def say(self): | |
name = self.args.name if self.args.name else self.type | |
print(f"The {name} says, \"{self.message}!\"") | |
class Kaiju(MyToys): | |
type = "Kaiju" | |
message = "Skreeeonk" | |
@classmethod | |
def setup_args(cls, subparsers): | |
kaiju = subparsers.add_parser("kaiju", help="Kaiju toys") | |
class Tmnt(MyToys): | |
type = "Ninja Turtles" | |
@classmethod | |
def setup_args(cls, subparsers): | |
tmnt = subparsers.add_parser("tmnt", help="Ninja Turtles toys") | |
tmnt.add_argument("--pizza-time", dest="pizza_time", action="store_true", default=False) | |
def __init__(self, args): | |
super().__init__(args) | |
self.message = "Pizza time" if self.args.pizza_time else "Cowabunga" | |
class Help(MyToys): | |
def say(self): | |
print("Use --help to use this script") | |
sys.exit(1) | |
def main(): | |
class_by_command = { | |
"kaiju": Kaiju, | |
"tmnt": Tmnt, | |
} | |
parser = argparse.ArgumentParser(description="Toy test") | |
parser.add_argument("--name", dest="name") | |
subparsers = parser.add_subparsers(dest="command") | |
for c in class_by_command.values(): | |
print(c) | |
c.setup_args(subparsers) | |
args = parser.parse_args() | |
toy_object = class_by_command.get(args.command, Help)(args) | |
toy_object.say() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment