pallets / click

Python composable command line interface toolkit
https://click.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
15.62k stars 1.4k forks source link

`flag_value` is not taken into account with `envvar` #2746

Open dennisoehme opened 3 months ago

dennisoehme commented 3 months ago

When using the --debug option DEBUG environment variable in the sample command, the debug value is not correctly set (flag_value). It is expected to be either logging.DEBUG or None, but it seems to be getting the integer value directly from the environment variable.

Sample:

import logging
import os
import sys

import click

# Works as expected
# sys.argv = ['', '--debug']

# Does not work as expected
# os.environ['DEBUG'] = '1'

@click.command()
@click.option('--debug', is_flag=True, flag_value=logging.DEBUG, envvar='DEBUG')
def sample(debug):
    click.echo(f"DEBUG: {debug}")
    assert debug in [logging.DEBUG, None], f"Invalid debug value: {debug} - expected >{logging.DEBUG}< or None"

if __name__ == '__main__':
    sample()

There is no different in using os.environ or DEBUG=1 python cli.py.

DEBUG=8 python cli.py prints out: 8

Environment: