Created
August 19, 2012 11:54
-
-
Save adamatan/3394463 to your computer and use it in GitHub Desktop.
cut-paste-code::argparse
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/python | |
import argparse | |
"""Boilerplate code for using argparse. | |
Clone, configure, and copy to your script. | |
Usage: | |
python argparse_cut_paste.py a 80 b c | |
""" | |
parser = argparse.ArgumentParser(description='Argaprse boilerplate example') | |
############################## Optional arguments ############################## | |
# -j, --java <==> args.java==True | |
parser.add_argument('-j', '--java', action='store_true') | |
# Mutually exclusive group (Can't store black and white together) | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument('--black', action='store_true') | |
group.add_argument('--white', action='store_true') | |
############################# Positional arguments ############################# | |
# String argument | |
parser.add_argument('name', help='String argument') | |
# Integer argument | |
parser.add_argument('number', type=int, default=80, help='Integer argument') | |
# Multiple optinal string arguments | |
parser.add_argument('more_names', nargs='+', help='At least one string arguments') | |
args = parser.parse_args() | |
print args | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment