Skip to content

Instantly share code, notes, and snippets.

@Winand
Last active December 15, 2022 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Winand/c581fd09133a44e9837097f43da70074 to your computer and use it in GitHub Desktop.
Save Winand/c581fd09133a44e9837097f43da70074 to your computer and use it in GitHub Desktop.
Allow abbreviated argument names (see also allow_abbrev=True in argparse)
import typer
def main(
hello: int = typer.Option(None),
there: int = typer.Option(None),
infinite: bool = typer.Option(False),
) -> None:
print(hello, there, infinite)
if __name__=='__main__':
def token_normalize(token: str) -> str:
"Predict argument by partial name"
token_lower = token.lower()
predicted_param = tuple(filter(lambda s: s.startswith(token_lower), available_params))
if len(predicted_param) == 1: # not ambiguous
return predicted_param[0]
return token
# typer.run(main)
app = typer.Typer()
app.command(context_settings={'token_normalize_func': token_normalize})(main)
available_params = [
i.name.lower() for i in typer.main.get_command(app).params
if isinstance(i.name, str) and i.name not in ('install_completion', 'show_completion')
]
app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment