Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

option

I know the --verbose or -v from several tools and I'd like to implement this into some of my own scripts and tools.

I thought of placing:

if verbose:     print ... 

through my source code, so that if a user passes the -v option, the variable verbose will be set to True and the text will be printed.

Is this the right approach or is there a more common way?

Addition: I am not asking for a way to implement the parsing of arguments. That I know how it is done. I am only interested specially in the verbose option.

like image 975
Aufwind Avatar asked May 12 '11 15:05

Aufwind


People also ask

How do you run a script verbose?

The -v option tells the shell to run in verbose mode. In practice , this means that shell will echo each command prior to execute the command. This is very useful in that it can often help to find the errors.

How do you use verbose option?

There are several ways to enable Verbose Mode. During startup, the screen may display which key(s) to press on the keyboard to enable Verbose Mode. Usually, users would press the Esc (escape) key for Linux, or the keyboard shortcut Ctrl + V for Microsoft Windows, and Command + V for macOS.

How do you add a verbose in Python?

verbose: log. basicConfig(format="%(levelname)s: %(message)s", level=log. DEBUG) log.info("Verbose output.") else: log. basicConfig(format="%(levelname)s: %(message)s") log.info("This should be verbose.") log.

What is verbose in shell script?

-v (short for verbose) – tells the shell to show all lines in a script while they are read, it activates verbose mode. -n (short for noexec or no ecxecution) – instructs the shell read all the commands, however doesn't execute them. This options activates syntax checking mode.


2 Answers

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 doesn't need to         # stuff everything to be printed into a single string         for arg in args:            print arg,         print else:        verboseprint = lambda *a: None      # do-nothing function 

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

If you're using Python 3, where print is already a function (or if you're willing to use print as a function in 2.x using from __future__ import print_function) it's 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 (you'd need the if in the function), but since you're 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.

like image 67
kindall Avatar answered Sep 22 '22 06:09

kindall


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)     log.info("Verbose output.") else:     log.basicConfig(format="%(levelname)s: %(message)s")  log.info("This should be verbose.") log.warning("This is a warning.") log.error("This is an error.") 

All of these automatically go to stderr:

% python myprogram.py WARNING: This is a warning. ERROR: This is an error.  % python myprogram.py -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.

like image 37
Profpatsch Avatar answered Sep 23 '22 06:09

Profpatsch