Last active
August 29, 2015 14:25
-
-
Save NickBeeuwsaert/044f63864ea617840de4 to your computer and use it in GitHub Desktop.
Trying to do simple subcommands in python
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 | |
| class Command(object): | |
| name = None | |
| aliases = [] | |
| description = None | |
| @classmethod | |
| def init_with_subparsers(cls, subparser): | |
| parser = subparser.add_parser(cls.name, aliases=cls.aliases, help=cls.description) | |
| parser.set_defaults(func=cls) | |
| return parser | |
| class ListCommand(Command): | |
| name = 'list' | |
| aliases = ['ls'] | |
| def __init__(self, args): | |
| print("received list!") | |
| print(args) | |
| @classmethod | |
| def init_with_subparsers(cls, sub): | |
| super().init_with_subparsers(sub) | |
| parser.add_argument('--dir', '-d', help="Hello!") | |
| parser = argparse.ArgumentParser(description="Subcommands!") | |
| subparsers = parser.add_subparsers() | |
| ListCommand.init_with_subparsers(subparsers) | |
| args = parser.parse_args(['ls']) | |
| args.func(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment