Created
October 16, 2014 10:44
-
-
Save LeoHuckvale/ce397e5d1b560799b6b6 to your computer and use it in GitHub Desktop.
Building a CLI with subparsers from separate modules
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
def build_subparser(subparsers): | |
""" | |
Build subparser for this command | |
""" | |
parser = subparsers.add_parser('bar', help='foo help') | |
parser.add_argument('b', type=int, help='b help') | |
parser.set_defaults(func=main) | |
def main(args): | |
""" | |
Main routine for bar | |
""" | |
print args.b |
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
def build_subparser(subparsers): | |
""" | |
Build subparser for this command | |
""" | |
parser = subparsers.add_parser('foo', help='foo help') | |
parser.add_argument('a', type=int, help='a help') | |
parser.set_defaults(func=main) | |
def main(args): | |
""" | |
Main routine for foo | |
""" | |
print args.a |
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/env python | |
#-*- coding:utf-8 -*- | |
""" | |
Sandbox example of CLI subparsers built from separate modules | |
""" | |
import foo | |
import bar | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description=__doc__) | |
subparsers = parser.add_subparsers(help='sub-command help') | |
foo.build_subparser(subparsers) | |
bar.build_subparser(subparsers) | |
args = parser.parse_args() | |
args.func(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment