Skip to content

Instantly share code, notes, and snippets.

@tkarna
Last active February 21, 2019 18:52
Show Gist options
  • Save tkarna/d6d0ae5467f2cb6dbf41dba41acdc328 to your computer and use it in GitHub Desktop.
Save tkarna/d6d0ae5467f2cb6dbf41dba41acdc328 to your computer and use it in GitHub Desktop.
Python argparse example demonstrating the most common usage options.
"""
Python argparse example demonstrating the most common usage options.
"""
import argparse
parser = argparse.ArgumentParser(
description='Description of the routine',
# includes default values in help entries
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument('a', type=int,
help='first positional argument, int')
parser.add_argument('b',
help='second positional argument, str')
parser.add_argument('files', metavar='file', nargs='+',
help='a file to process, multiple values accepted')
parser.add_argument('-k', '--option1', action='store_true',
help='optional boolean argument')
parser.add_argument('-l', '--option2', type=int, default=3,
help='optional integer argument')
parser.add_argument('-m', '--option3', type=int, choices=[0, 1, 2],
default=0,
help='optional integer argument with fixed choices')
parser.add_argument('-v', '--verbosity', action='count', default=0,
help='increase output verbosity')
args = parser.parse_args()
print(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment