Surprise! the shell suggests command line switches

2019-03-17 23:56发布

问题:

I noticed that the bash shell can suggest command line switches for your command.

Just type your command, a dash (-) and type tab. The shell will show you available switches.

For example, try:

andy@andyx:~$ java -

and press tab - Surprise!

The question is:

How do I interface with this feature. I.e., if I write a program that is to be run from the console, what's the API to tell the shell what switches are available?

回答1:

You have discovered Bash's programmable completion feature.



回答2:

See:

$ man bash

In particular, the section entitled "READLINE" and subsection "Programmable Completion"



回答3:

FYI: In Ubuntu and/or Debian the scripts are at /etc/bash_completion.d/