One line of code adds tab completion to Python CLIs via shtab and jsonargparse
A developer building a Python command-line tool found that users had no way to discover valid argument values mid-command, such as the four accepted strings for an order_by parameter. Rather than writing separate shell completion scripts for bash, zsh, fish, and tcsh — each in a different syntax — the author turned to shtab, an optional dependency of jsonargparse. By adding shtab to the project's dependencies and calling set_parsing_settings(add_print_completion_argument=True) in one line, the CLI automatically gains a --print_completion flag. The completion scripts are generated by inspecting the existing parser, which already knows all valid choices from the client's type hints, meaning no manual maintenance is required. The approach keeps tab completion in sync with the interface automatically, since both derive from the same source code.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in