[Python] Using boolean flags in Click library

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página