4

I am looking for a method of having console auto complete - such that given an application like:

int main (int argc, char ** argv)
{
  if (argc == 1) return EXIT_FAILURE;
  if (strcmp(argv[1], "good")==0) printf("good\n");
  if (strcmp(argv[1], "bad")==0) printf("bad\n");
  return EXIT_FAILURE;
}

When running it, I would like pressing [tab] after the command, such that it would give me one of the possible useful options.

Example:

./a.out g[tab]

would auto complete to

./a.out good

I don't want to edit /etc/bash-completion.d/, I was hoping for a much stronger auto-complete, something like a function in the executable itself that would be called - perhaps so it could query a database for the list of possible options. Or perhaps output a message letting you know what the options are.

If you think this is simply totally impossible, let me know!

Kevin Branigan
  • 223
  • 3
  • 10
  • The closest I think (at least with zsh, not bash which I guess is similar these days) you can get is by adding a --help option that returns a list of the valid options in one of the "standard" layouts, which your shell completion can then understand. – Flexo May 24 '11 at 08:18

2 Answers2

4

Completions are a property of the shell you run the application from. You will have to provide completion functions for all the shells you want to support (bash, zsh, tcsh and fish have customizable completions). A completion function can call your application (e.g. run you_application --list-possible-arguments) or do whatever it chooses to generate the completions — it's already a “strong” completion in your terminology.

In bash, you declare completions with the complete built-in. Look in /etc/completion.d for examples (gpg is a fairly simple example; git is a rather involved one).

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • Fantastic! It's unfortunate that I just didn't look further at this, or had insight to call the application itself with a descriptor argument request. Now all I have to do is install bash-complete (I'm using Mac OS - I assumed it'd be similar but it seems Mac OS's bash doesn't ship with it) – Kevin Branigan May 24 '11 at 20:05
0

If you are using BASH then have a look at this similar post:

Auto-complete command line arguments

================================

If you want to provide your own command line then have a look at the Readline library:

The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in. Both Emacs and vi editing modes are available. The Readline library includes additional functions to maintain a list of previously-entered command lines, to recall and perhaps reedit those lines, and perform csh-like history expansion on previous commands.

Community
  • 1
  • 1
Nick
  • 25,026
  • 7
  • 51
  • 83
  • That's not shell command lines though, it's console style commands within your application. – Flexo May 24 '11 at 08:11