Skip to content

default=lambda and show_default behavior #547

Closed

Description

@jcejohnson

This:
@click.option('--username', callback=set_option_value, default=lambda: get_option_default('username'), show_default=True)

gives this --help output:
--username TEXT [default: <function <lambda> at 0x104858a28>]

If that is the intended behavior, is there an option to have it invoke the function during --help?

Activity

  1. untitaker commented on Apr 1, 2016

    @untitaker
    Contributor

    The current behavior is a bug, but I'm unsure whether it's a good idea to just inline the return value of the lambda? IMO it's better to describe the default callback's logic in the option helptext.

  2. jcejohnson commented on Apr 1, 2016

    @jcejohnson
    Author

    I think I agree but it would also be nice to have the option for --help to invoke the function.

  3. blueyed commented on Nov 28, 2017

    @blueyed
    Contributor

    JFI: the code in question:

    click/click/core.py

    Lines 1635 to 1639 in defe6ae

    if self.default is not None and self.show_default:
    extra.append('default: %s' % (
    ', '.join('%s' % d for d in self.default)
    if isinstance(self.default, (list, tuple))
    else self.default, ))

  4. dougharris commented on May 14, 2018

    @dougharris
    Contributor

    In #844, the suggestion is made to evaluate the default value when running --help. This seems possibly dangerous or ripe for exploitation if the lambda on the option ran something which updates a database, writes to a file, etc.

    Instead, I'm working on a fix for this that adds a new optional parameter to @click.option to be used to give a description of what the dynamic value would be.

  5. locked as resolved and limited conversation to collaborators on Nov 13, 2020
  6. added this to the 7.0 milestone on Aug 8, 2026
  7. 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