Ability to specify choices for help text independently of parameter type #733
-
First Check
Commit to Help
Example Codefrom typing import Annotated
import typer
def _get_list_of_currently_installed_log_output_plugin_names() -> list[str]:
return ["file", "datadog", "loggly"] # Real implementation is dynamic
def main(log_output: Annotated[str, typer.Option(help_choices=_get_list_of_currently_installed_log_output_plugin_names()]) -> None:
typer.echo(f"Log output plugin specification is {log_output!r}")
if __name__ == "__main__":
typer.run(main) DescriptionI have a need to be able to display some choices for a parameter in help text, but not actually enforce those choices. My particular use case for this is that my app uses plugins extensively, and the user needs to be able to specify which plugins to use for specific purposes on the command line. The problem is that plugins optionally take arguments, which the user can provide using a colon-separated syntax. My specific example here is letting the user select where to send logs, and the available plugins might be For this case, what I want is for help output for this parameter to display the choices
I understand that it would be left up to me to check the true validity of the string and raise the appropriate error for typer if it's wrong. Also, using an Enum would be fine if we must, although IMO even the typical, existing uses of choices should support simple lists instead of Enums, but that's a separate discussion. :) Operating SystemLinux Operating System DetailsNo response Typer Version0.9.0 Python Version3.12.1 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I ended up solving this by creating my own |
Beta Was this translation helpful? Give feedback.
I ended up solving this by creating my own
click
type, that specifically handles my plugin specification syntax. Unfortunately it's in a proprietary codebase, so I can't share it as an example, but it's relatively straightforward to do so. In theclick
codebase, look throughclick/types.py
for many examples.