Last active
November 11, 2016 01:48
-
-
Save lukehinds/c0121a1e1a400fad9a11 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
import argparse | |
# Set up the main parser | |
parser = argparse.ArgumentParser(description='An awesome program') | |
# And the subparser | |
subparsers = parser.add_subparsers( | |
title='subcommands', | |
description='valid subcommands', | |
help='additional help') | |
# The first subparser 'Create' | |
parser_create = subparsers.add_parser('create') | |
# Store the result in which for a conditional check later | |
parser_create.set_defaults(which='create') | |
# Add the first arg to create (First Name) | |
parser_create.add_argument( | |
'--first_name', | |
required=True, | |
help='First Name') | |
# Add the second arg to create (Last Name) | |
parser_create.add_argument( | |
'--last_name', | |
required=True, | |
help='Last Name') | |
# The Second subparser 'Delete' | |
parser_delete = subparsers.add_parser('delete') | |
parser_delete.set_defaults(which='delete') | |
parser_delete.add_argument( | |
'id', help='Database ID') | |
args = vars(parser.parse_args()) | |
if args['which'] == 'create': | |
print "Creating {} {}".format(args['first_name'], args['last_name']) | |
else: | |
print "Deleting {}".format(args['id']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment