16

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!

Screenshot

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?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • 1
    Hm... my bash seams to be lacking this feature. Do I need to enable it somehow? – Burkhard Apr 22 '09 at 11:39
  • It only works if a command is set up to use it in /etc/bash_completion.d (see MrX answer below.) I tried it too, with ls, and it didn't work, but looking in that dir I see that yast2 has a 100 line completion script on one of our systems. Lotta work to set this up! Might be nice to do if you are writing a unix tool that a lot of people are going to use, but not for personal work. Use --help with gnu utilities, instead. – Andrej Panjkov May 15 '09 at 01:13

3 Answers3

19

You have discovered Bash's programmable completion feature.

unwind
  • 391,730
  • 64
  • 469
  • 606
6

See:

$ man bash

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

Alnitak
  • 334,560
  • 70
  • 407
  • 495
5

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