Skip to content

Instantly share code, notes, and snippets.

@bryan-lott
Forked from robmcmullen/argparse_reminder.py
Created March 9, 2016 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryan-lott/a5f492847cd9f382217a to your computer and use it in GitHub Desktop.
Save bryan-lott/a5f492847cd9f382217a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="""
I never freaking remember argparse syntax and the docs are so all over the place
that I need this for an example.
""")
# https://docs.python.org/dev/library/argparse.html#the-add-argument-method
parser.add_argument('thing', metavar='thing', nargs='+', help='we get to have 1 or more of these')
parser.add_argument('--stringlist', nargs='+', help='also, one or more space separated items')
parser.add_argument('--stringthing', default='something', help='helpful message')
parser.add_argument('--flag', action='store_true', help='your basic flag')
parser.add_argument('--count', type=int, default=1, help='we can have types of things')
parser.add_argument('--filehandle', type=argparse.FileType('r'), help='use a file type to get a filehandle')
parser.add_argument('--item', choices=[1,2,3], help='restrict from list of choices')
parser.add_argument('--1', action='store_const', dest='const', default=0, const=1, help='multiple flags to set one argument')
parser.add_argument('--2', action='store_const', dest='const', default=0, const=2, help='... from a set of flags')
parser.add_argument('--3', action='store_const', dest='const', default=0, const=3, help='... specify a default value when no flags are set')
args = parser.parse_args()
print args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment