-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Allow adding dynamic parameters to every Context #2784
base: main
Are you sure you want to change the base?
Conversation
f8c5852
to
3901925
Compare
I'm currently rewriting the parser, so I won't be able to add this until I understand how it will interact with that, which will be some time. |
I'm not sure I like this, since it moves parameter definitions out of the command itself. We already support dynamic commands, and that doesn't require storing them in the context. |
The issue is dynamic parameters rather than commands themselves. Here is an example that I'm about to use at work using this PR implementation: def resolve_environment(ctx: DynamicContext, param: click.Option, value: str) -> str: # noqa: ARG001
from msgspec_click import generate_options
dev_env_class = get_dev_env(value)
ctx.params['env_class'] = dev_env_class
ctx.dynamic_params.extend(generate_options(dev_env_class.config_class()))
return value
@click.command(short_help='Start a developer environment', context_settings={'ignore_unknown_options': True})
@click.option(
'--type',
'-t',
'env_type',
type=click.Choice(AVAILABLE_DEV_ENVS),
default=DEFAULT_DEV_ENV,
show_default=True,
is_eager=True,
callback=resolve_environment,
help='The type of developer environment',
)
@click.pass_context
def cmd(ctx: click.Context, env_type: str, **kwargs: Any) -> None:
import msgspec
env_class: type[DeveloperEnvironmentInterface] = ctx.params['env_class']
config = msgspec.convert(kwargs, env_class.config_class())
env = env_class(
name=env_type,
storage_dirs=app.config.storage,
config=config,
) |
I would be open to anything that allows me to make dynamic options per execution/context rather than per command! edit: without the use of a synchronization primitive like |
For this point in particular, I was trying to copy the code style by adding a parameter which is saved as a property of the instance. I actually don't see a use case for passing in parameters and would much prefer only the property without modifying the signature of |
I know. With dynamic commands, you can override what commands are available within a group during execution, without special support in the the context and without modifying the global definition of the group. I want dynamic parameters to behave similarly. |
I don't have enough knowledge of the code to understand the paradigm you desire. Could you perhaps show an example? |
Edit: tests are now fixed and I added a new classmethod to Command that is required for this use case |
The docs build error appears unrelated and I'm not sure how to fix that. |
This adds a new
dynamic_params
property to the Context object which the Command object will use in itsget_params
method.fixes #2783
should fail without the change.
.. versionchanged::
entries in any relevant code docs.