Last active
December 23, 2016 13:00
-
-
Save stevedoyle/20701a0dceab00196d2d to your computer and use it in GitHub Desktop.
Using argparse for command line argument parsing in python
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
import argparse | |
import logging | |
def main(): | |
parser = argparse.ArgumentParser(description="Example") | |
parser.add_argument('-v', '--verbose', action='store_true', help="Verbose output") | |
parser.add_argument('--size', nargs='*', default=[256], type=int, choices=[256,512,768,1024], help="Data sizes (default = 256)") | |
parser.add_argument('--csv', nargs='?', default='off', choices=['on', 'off'], help='CSV mode (default = off)') | |
args = parser.parse_args() | |
if args.verbose: | |
logging.basicConfig(level=logging.INFO) | |
for size in args.size: | |
# do something | |
continue | |
if __name__ == "__main__": | |
main() | |
# Sample output ... | |
# | |
# usage: tmp.py [-h] [-v] [--size [{256,512,768,1024} [{256,512,768,1024} ...]]] | |
# [--csv [{on,off}]] | |
# | |
# Example | |
# | |
# optional arguments: | |
# -h, --help show this help message and exit | |
# -v, --verbose Verbose output | |
# --size [{256,512,768,1024} [{256,512,768,1024} ...]] | |
# Data sizes (default = 256) | |
# --csv [{on,off}] CSV mode (default = off) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment