python – How to implement the –verbose or -v option into a script?

python – How to implement the –verbose or -v option into a script?

My suggestion is to use a function. But rather than putting the if in the function, which you might be tempted to do, do it like this:

if verbose:
    def verboseprint(*args):
        # Print each argument separately so caller doesnt need to
        # stuff everything to be printed into a single string
        for arg in args:
           print arg,
    verboseprint = lambda *a: None      # do-nothing function

(Yes, you can define a function in an if statement, and itll only get defined if the condition is true!)

If youre using Python 3, where print is already a function (or if youre willing to use print as a function in 2.x using from __future__ import print_function) its even simpler:

verboseprint = print if verbose else lambda *a, **k: None

This way, the function is defined as a do-nothing if verbose mode is off (using a lambda), instead of constantly testing the verbose flag.

If the user could change the verbosity mode during the run of your program, this would be the wrong approach (youd need the if in the function), but since youre setting it with a command-line flag, you only need to make the decision once.

You then use e.g. verboseprint(look at all my verbosity!, object(), 3) whenever you want to print a verbose message.

Use the logging module:

import logging as log
args = p.parse_args()
if args.verbose:
    log.basicConfig(format=%(levelname)s: %(message)s, level=log.DEBUG) output.)
    log.basicConfig(format=%(levelname)s: %(message)s) should be verbose.)
log.warning(This is a warning.)
log.error(This is an error.)

All of these automatically go to stderr:

% python
WARNING: This is a warning.
ERROR: This is an error.

% python -v
INFO: Verbose output.
INFO: This should be verbose.
WARNING: This is a warning.
ERROR: This is an error.

For more info, see the Python Docs and the tutorials.

python – How to implement the –verbose or -v option into a script?

Building and simplifying @kindalls answer, heres what I typically use:

v_print = None
def main()
    parser = argparse.ArgumentParser()
    parser.add_argument(-v, --verbosity, action=count, 
                        help=increase output verbosity (e.g., -vv is more than -v))

    args = parser.parse_args()

    if args.verbosity:
        def _v_print(*verb_args):
            if verb_args[0] > (3 - args.verbosity):
                print verb_args[1]  
        _v_print = lambda *a: None  # do-nothing function

    global v_print
    v_print = _v_print

if __name__ == __main__:

This then provides the following usage throughout your script:

v_print(1, INFO message)
v_print(2, WARN message)
v_print(3, ERROR message)

And your script can be called like this:

% python -v
ERROR message

% python -vv
WARN message
ERROR message

% python -vvv
INFO message
WARN message
ERROR message

A couple notes:

  1. Your first argument is your error level, and the second is your message. It has the magic number of 3 that sets the upper bound for your logging, but I accept that as a compromise for simplicity.
  2. If you want v_print to work throughout your program, you have to do the junk with the global. Its no fun, but I challenge somebody to find a better way.

Leave a Reply

Your email address will not be published. Required fields are marked *