argparse icon indicating copy to clipboard operation
argparse copied to clipboard

Dynamic shell completions

Open daurnimator opened this issue 4 years ago • 3 comments

Is it possible to create dynamic shell completions? e.g. if I type mycommand --profile <TAB>, can I provide a way to get a list of available profiles?

If not, we should make that possible...

@pauloue

daurnimator avatar Sep 07 '21 04:09 daurnimator

It is not currently possible.

Some libraries support it with a special hidden command that takes the shell command line and prints a list of completions. Then generated completion scripts would invoke e.g. mycommand __complete '--profile ' to get a list of completions.

That's what Cobra (Go) does: __complete command, generated completion script. Clang does something similar: http://blog.llvm.org/2017/09/clang-bash-better-auto-completion-is.html.

p-ouellette avatar Sep 07 '21 05:09 p-ouellette

Some libraries support it with a special hidden command that takes the shell command line and prints a list of completions. Then generated completion scripts would invoke e.g. mycommand __complete '--profile ' to get a list of completions.

Cool; I figured as much.

I guess we should add it as a special option to the existing completion subcommand/option? I think you'd need to pass any current contexts (e.g. subcommands, other existing arguments that might filter down the output) e.g. mycommand completion --complete-this='"mycommand","somesubcommand","--profile"'

daurnimator avatar Sep 07 '21 05:09 daurnimator

@daurnimator If you are interested unifying in shell completion, there was an attempt to define a DSL which could be simplified by stripping regex and counting/loops: https://gitlab.redox-os.org/AdminXVII/shellac-server/-/tree/next

Portable context-aware autocompletion (the shell is asking what currently can be completed) requires an ABI-extension at POSIX-level, but I dont see interest to fix the shortcomings of posix shell so this will not happen.

matu3ba avatar Sep 30 '21 22:09 matu3ba