I want to add a verbose option to my python script but I'm not sure the best way to go about this. My main() function calls a parse_cmdline() function then a process() function. I want to create a function for my verbose option. I'm not sure where I should define my verbose function. Will I have to pass the verbose option from my parse_cmdline() to my process() function? I am looking for the best way to go about this.
-
I am already aware of how to parse arguments. I am using argparse. I want to make a function for my verbose option that is clean and easy. Information on how to parse arguments is not helpful. – wDroter Oct 11 '12 at 20:06
-
Why don't you add that information to your question then? Adding your current code (the part where you parse arguments for example) would have been helpful to illustrate your question better. – Martijn Pieters Oct 11 '12 at 20:11
-
In my question I stated I wanted to create a function for my verbose option but it seems everyone was keen to ignore that part. – wDroter Oct 11 '12 at 20:14
-
I find your question to be *very* unclear and vague in any case. People don't wilfully ignore parts of a question. If you are getting *3* answers that in your view do not address your question, then something is wrong with your question, I am afraid. – Martijn Pieters Oct 11 '12 at 20:16
-
Nobody even mentioned defining a function for verbose – wDroter Oct 11 '12 at 20:27
-
But what does `verbose` *mean* in this context? You haven't shared anything about your program, let alone what it outputs, what it'll output more of when you switch on verbose, how you currently pass configuration around. – Martijn Pieters Oct 11 '12 at 20:28
-
be more vebose in your question – Chris Wesseling Oct 11 '12 at 20:29
4 Answers
Logging
You basically need a logging module to filter out output depending on the required verbosity.
That is, every output message gets given a severity level (like debug
or warning
), and you can then decide what is the minimum severity level for the messages to be displayed.
You just use the parsed the arguments to decide the required severity level (Basically, the higher the minimum severity level, the less verbose the program is).
Logbook
I can only recommend the awesome logbook
module. Using a handler
whose log level
is set to the minimum required severity you require will easily solve your issue (e.g. Display only notice
and higher...).
You will then have to replace calls to print
in your script with logging calls logbook.debug
, logbook.info
, and so on.
There are a lot of more advanced features in logbook
, feel free to look around the documentation.
Please be aware that, by default, logbook
registers a StderrHandler
and pushes it to the application, you can easily undo that with logbook.default_handler.pop_application()
.
There are other solutions, as suggested by the documentation.

- 53,284
- 11
- 113
- 116
Parse your options with argparser
and either pass them around or put them in a global
Note that you only have to use the global
keyword when you want to assign to some name in the global scope, not when you just want yo read the value.

- 6,226
- 2
- 36
- 72
Take a look at plac
. It's an awesome package for commandline parsing, much more inituitive than the built-in argparse
, in my opinion.

- 15,797
- 3
- 42
- 36