Skip to content

Instantly share code, notes, and snippets.

@fawix
Created April 13, 2017 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fawix/9bed924a07cea66750368c69bf798d69 to your computer and use it in GitHub Desktop.
Save fawix/9bed924a07cea66750368c69bf798d69 to your computer and use it in GitHub Desktop.
Python - Verbose Flag
#!/usr/bin/python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='count', default=0)
for c in ['', '-v', '-v -v', '-vv', '-vv -v', '-v -v --verbose -vvvv']:
print parser.parse_args(c.split())
@fawix
Copy link
Author

fawix commented Apr 13, 2017

This code snippet shows how to do verbose arguments with Python argparse. The way to use these is to put the count result in a variable and then use if statements in order to determine the verbosity and print messages accordingly.

Something like:

VERBOSE = parser.verbose
if VERBOSE > 0:
    print "verbose messages"
if VERBOSE > 1:
    print "very verbose messages"
if VERBOSE > 2:
    print "very very verbose messages"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment