Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag option with secondary opts: show_default=True does not show value from default_map in "help" output #2632

Open
a-stangl opened this issue Nov 11, 2023 · 0 comments

Comments

@a-stangl
Copy link

I'm setting the default_map from a config file, and it seems the values from the default_map are not correctly shown in the --help output if I set show_default=True on my option.

My Option looks like this:

@click.option(
    "--long/--short",
    "-l/-s",
    is_flag=True,
    show_default=True,
    help="show additional information like size and creation date",
)

I initialize the default_map with a custom command class that I attach via the @click.command decorator like this @click.command(cls=ConfigAwareCommand)

from click import Command as _Command


class ConfigAwareCommand(_Command):
    def __init__(self, *args, **kwargs):
        kwargs["context_settings"] = {
            "default_map": CONFIG.get_cli_command_defaults(kwargs["name"])
        }
        super().__init__(*args, **kwargs)

The default_map value is in my example {'long': True}. In the --help output, the default value is shown like this

  -l, --long / -s, --short        show additional information like size and
                                  creation date  [default: short]

When executing the command, the default value from the default_map is used correctly (long defaults to True).

During debugging, I might have found the culprit inside src/click/core.py

            elif self.is_bool_flag and self.secondary_opts:
                # For boolean flags that have distinct True/False opts,
                # use the opt without prefix instead of the value.
                default_string = _split_opt(
                    (self.opts if self.default else self.secondary_opts)[0]
                )[1]

As you can see, self.default is used instead of the default_value variable that is initialized further above this code snippet.

If I change the part of the code above and use default_value instead of self.default the help output shows the correct default values from the default_map.

            elif self.is_bool_flag and self.secondary_opts:
                # For boolean flags that have distinct True/False opts,
                # use the opt without prefix instead of the value.
                default_string = _split_opt(
                    (self.opts if default_value else self.secondary_opts)[0]
                )[1]

I am not sure if I do something wrong or if this is indeed a bug.

Environment:

  • Python version: 3.10.12
  • Click version: 8.1.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant