Shell completion with "SubcommandApp"
I have a CLI using SubcommandApp like in: https://brentyi.github.io/tyro/examples/subcommands/#decorator-based-subcommands
https://brentyi.github.io/tyro/tab_completion/ looks like, that i can call my CLI just with --tyro-write-completion but this just results in:
Unrecognized options: --tyro-write-completion
Maybe there is a helper in tyro, that enables my to just add a own write_completion() sub command to my CLI?
I just checked and this works for me with 04_decorator_subcommands.py. Can you make sure you have exactly three arguments?
For zsh, it should be something like this:
python 04_decorator_subcommands.py --tyro-write-completion zsh ~/.zfunc/_04_decorator_subcommands_py
You can also replace zsh with bash, etc. To minimize maintenance effort also note that all tyro does is generate the completion script. Putting it in the right place, making sure that the shell loads it correctly, etc, is left to the user.
To write your own helper: this is possible. We're just using shtab under-the-hood, so what you can do in your subcommand is:
- Call
tyro.extras.get_parser()to get an argparse object - Use
shtabto generate the completion script
Can you make sure you have exactly three arguments?
Ah! I just tested only with --tyro-write-completion ;)
So, yes, it works with all three arguments. Great!
Maybe try to raise a better error, if the first argument is --tyro-write-completion ?
Call tyro.extras.get_parser() to get an argparse object
I have no idea to call this correct... You be great if the is a full automation, but maybe https://github.com/brentyi/tyro/issues/13 is a better place for this idea?