Skip to content

Instantly share code, notes, and snippets.

@jg75
Created September 26, 2018 16:38
Show Gist options
  • Save jg75/5df6d283ae55560ce0d7faeac16dbdcc to your computer and use it in GitHub Desktop.
Save jg75/5df6d283ae55560ce0d7faeac16dbdcc to your computer and use it in GitHub Desktop.
"""Example CLI."""
import argparse
class Example:
"""Python lorem ipsum."""
@staticmethod
def show_base(**arguments):
"""Show the current base."""
print("All your base are belong to us.")
@staticmethod
def evacuate_pack(**arguments):
"""Evacuate the local packs opposite of the staged applied archives."""
smack_reveal_tag = arguments.get("smack_reveal_tag")
pin_rip_history = arguments.get("pin_rip_history")
book_branch = arguments.get("book_branch")
region = arguments.get("region")
print(f"Executing in region: {region}")
for pack in arguments.get("packs"):
print(f"Evacuating local pack: {pack}")
if smack_reveal_tag:
print(" - Relinking log to the submodule")
if pin_rip_history:
print(f" - Using {pin_rip_history} to parse archives/bases/")
if book_branch:
print(f" - Index prefixed using {book_branch}")
def __init__(self):
"""Override."""
self.cli = ExampleCLI()
class ExampleCLI:
"""An example command line interface using argparse."""
commands = {
"show-base": Example.show_base,
"evacuate-pack": Example.evacuate_pack
}
@staticmethod
def parse_default_arguments(parser):
"""Create default arguments."""
parser.add_argument(
"--region",
default="default",
help="A valid region"
)
@classmethod
def create_subparser(cls, subparsers, command):
"""Create command subparser."""
parser = subparsers.add_parser(command)
cls.parse_default_arguments(parser)
parser.set_defaults(func=cls.commands[command])
return parser
@classmethod
def parse_show_base(cls, subparsers):
"""Create arguments for show-base command."""
cls.create_subparser(subparsers, "show-base")
@classmethod
def parse_evacuate_pack(cls, subparsers):
"""Create arguments for evacuate-pack command."""
parser = cls.create_subparser(subparsers, "evacuate-pack")
parser.add_argument(
"--smack-reveal-tag",
action="store_true",
help="Relink a log for the submodule that is set by automatic pack"
)
parser.add_argument(
"--pin-rip-history",
help="Use file to parse achive/bases/ to reset commit"
)
parser.add_argument(
"--book-branch",
choices=["subtrees/bases/", "heads/tips/"],
help="Prefix indexes using subtrees/bases/ or heads/tips/"
)
parser.add_argument(
"packs",
nargs="+",
help="The local packs opposite of the staged applied archives"
)
@classmethod
def parse(cls):
"""Create the argument parser."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
subparsers.required = True
cls.parse_show_base(subparsers)
cls.parse_evacuate_pack(subparsers)
return vars(parser.parse_args())
def __init__(self):
"""Override."""
self.arguments = self.parse()
self.command = self.arguments.pop("command")
self.function = self.arguments.pop("func")
if __name__ == "__main__":
example = Example()
example.cli.function(**example.cli.arguments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment