I'm trying to make a verbose flag for my Python program. Currently, I'm doing this:
import click
#global variable
verboseFlag = False
#parse arguments
@click.command()
@click.option('--verbose', '-v', is_flag=True, help="Print more output.")
def log(verbose):
global verboseFlag
verboseFlag = True
def main():
log()
if verboseFlag:
print("Verbose on!")
if __name__ == "__main__":
main()
It'll never print "Verbose on!" even when I set the '-v' argument. My thoughts are that the log function needs a parameter, but what do I give it? Also, is there a way to check whether the verbose flag is on without global variables?
So click is not simply a command line parser. It also dispatches and processes the commands. So in your example, the
log()
function never returns tomain()
. The intention of the framework is that the decorated function, ie:log()
, will do the needed work.Code:
Test Code:
Results:
The above answer was helpful, but this is what I ended up using. I thought I'd share since so many people are looking at this question: