luarocks / argparse

Feature-rich command line parser for Lua
https://github.com/luarocks/argparse
Other
58 stars 8 forks source link

Dynamic shell completions #21

Open daurnimator opened 3 years ago

daurnimator commented 3 years ago

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

p-ouellette commented 3 years ago

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.

daurnimator commented 3 years ago

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"'

matu3ba commented 3 years ago

@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.