Skip to content

show_default attempts to print lambdas when using dynamic defaults #844

Closed

Description

@Wilfred

Given the code:

#!/usr/bin/env python

import click
from datetime import date


@click.command()
@click.option('--day', help="choose the day of the week",
              default=lambda: date.today().strftime("%A"), show_default=True)
def main(day):
    print "Hello world! It's a wonderful {}.".format(day)


if __name__ == '__main__':
    main()

We get the correct default value when running the script:

$ ./hello.py
Hello world! It's a wonderful Tuesday.

However, the help text tries to print the lambda:

$ ./hello.py --help
Usage: hello.py [OPTIONS]

Options:
  --day TEXT  choose the day of the week  [default: <function <lambda> at
              0x109c028c0>]
  --help      Show this message and exit.

Activity

  1. untitaker commented on Aug 29, 2017

    @untitaker
    Contributor

    I see. We should prevent usage of show_default=True when the default is dynamic. The correct way to document a dynamic default is to describe it in the help text IMO (not evaluate it).

  2. Wilfred commented on Aug 30, 2017

    @Wilfred
    Author

    Is there any downside to evaluating it?

    I couldn't see any way of defining a dynamic help text, which is what I'd want in this case if click didn't evaluate my default.

  3. untitaker commented on Aug 30, 2017

    @untitaker
    Contributor

    Why would you want to evaluate it? That is less informative than describing how this value is calculated.

  4. Wilfred commented on Aug 31, 2017

    @Wilfred
    Author

    So users can see the current value. I use this for expanding ~/foo to /home/wilfred/foo, and I'd like to show the path in my help message.

    Click can't understand what an arbitrary lambda expression does, and I can already describe how it's calculated in my argument summary.

  5. untitaker commented on Aug 31, 2017

    @untitaker
    Contributor
  6. blueyed commented on Nov 28, 2017

    @blueyed
    Contributor

    Duplicate of #547.

  7. locked as resolved and limited conversation to collaborators on Nov 13, 2020
  8. added this to the 7.0 milestone on Aug 8, 2026
  9. added
    help output
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help output

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions