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

Unexposed option name is None when the parameter name is not a python identifier #2768

Open
azmeuk opened this issue Aug 26, 2024 · 0 comments

Comments

@azmeuk
Copy link

azmeuk commented Aug 26, 2024

I would like to have dots in option names. As click requires option names to be valid python identifiers, I would like to use unexposed options to make dots in option names possible, and a custom callback to store the option name somewhere:

import click


def option_callback(ctx, param, value):
    ctx.ensure_object(dict)
    ctx.obj.setdefault("params", {})[param.name] = value

    return value


@click.command()
@click.option("--foo.foo", expose_value=False, callback=option_callback)
@click.option("--bar-bar", expose_value=False, callback=option_callback)
@click.pass_obj
def cli(obj):
    click.echo(obj["params"])


if __name__ == "__main__":  # pragma: no cover
    cli()

In that situation, in the callback context, option.name is None if the original option name is not a valid identifier:

$ python example.py --foo.foo baz --bar-bar baz
{None: 'baz', 'bar_bar': 'baz'}

I suppose this is due to those lines:

click/src/click/core.py

Lines 2594 to 2595 in 99015e1

if not expose_value:
return None, opts, secondary_opts

Ideally I would love a simple way to use non identifier option names, like proposed in #2433.
In the meantime I would suggest to allow Option.name to have its original value instead of None for unexposed option. I would volunteer for a PR if this is accepted.

What do you think?

  • Python version: 3.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