Last active
December 15, 2022 14:43
-
-
Save Winand/c581fd09133a44e9837097f43da70074 to your computer and use it in GitHub Desktop.
Allow abbreviated argument names (see also allow_abbrev=True in argparse)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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