shtab icon indicating copy to clipboard operation
shtab copied to clipboard

Completion with `python -m module_name` without creating an explicit executable script?

Open rameloni opened this issue 1 year ago • 0 comments

I'm trying to enable auto-completion for Python scripts executed with python -m my_module -[TAB] [TAB] or python my_module.py -[TAB] [TAB].

I first created the script:

#!/usr/bin/env python3

import argparse

def get_main_parser():
    parser = argparse.ArgumentParser(prog="my_module")
    parser.add_argument("--a", default=None)
    parser.add_argument("--b", default=None)
    return parser


if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print(args)

Then:

chmod +x ./my_module.py
alias my_module='./my_module.py'
eval "$(shtab --prog=my_module --shell=bash my_module.get_main_parser)"

And my_module -[TAB] [TAB] works but when I try python3 -m my_module -[TAB] [TAB] or python3 my_module.py -[TAB] [TAB] autocompletion doesn't work. I also tried to replace prog="my_module" with prog="my_module.py" but still it doesn't work.

rameloni avatar Oct 09 '24 11:10 rameloni