Skip to content

Instantly share code, notes, and snippets.

@leoncamel
Created September 11, 2015 08:59
Show Gist options
  • Save leoncamel/10d165c24d59db1ca09b to your computer and use it in GitHub Desktop.
Save leoncamel/10d165c24d59db1ca09b to your computer and use it in GitHub Desktop.
argparse
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='sub-command help', dest="cmd")
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')
parser_b = subparsers.add_parser('b', help='a help')
parser_b.add_argument('kaka', type=int, help='bar help')
return parser
def dispatch_cmd(args):
if not args.foo:
args.foo = True
if args.cmd == 'a':
cmd_a(args)
elif args.cmd == 'b':
cmd_b(args)
else:
pass
def cmd_a(args):
print "cmd_a"
print args
def cmd_b(args):
print "cmd_b"
print args
def main():
args = build_argparser().parse_args()
print args
dispatch_cmd(args)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment