Skip to content

Variable precedence #873

Closed
Closed
@rgreinho

Description

@rgreinho

Playing with click's ability to read environment variables and provide default values, I noticed that the order in which the variables are looked up seems wrong.

Here is what I expected (first variable found wins):

  1. Retrieve the value from the CLI
  2. Retrieve the value from the environment
  3. Retrieve the default value

But here is what happened:

  1. Retrieve the value from the CLI
  2. Retrieve the default value
  3. Retrieve the value from the environment

This seems wrong because the fact that I set default values should not prevent the environment lookup to happen.

How to reproduce

Create the precedence.py file:

import click

@click.command()
@click.option('--name', help="Specify a name")
def prec(name):
    """Greet the world."""
    click.echo(f'Hey {name}!')


if __name__ == '__main__':
    prec(auto_envvar_prefix='CLICK', default_map={'name': 'Stranger'})

Then run:

$ export CLICK_NAME=Tiger
$ python precedence.py --name Joe
Hey Joe!
$ python precedence.py
Hey Stranger!

Then remove the default_map argument from the command call and you get:

$ export CLICK_NAME=Tiger
$ python precedence.py --name Joe
Hey Joe!
$ python precedence.py
Hey Tiger!

Versions

Python: 3.6.3
Click: 6.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions