OK, with some help from a friend i found the file gflags_completions.h.in where the documentation for this feature is. I have pasted a part of the header file in the end of this answer.
What was missing from my question above, is the incude statement for gflags_completions.h
.
Minimal working example: myapp.cc
/** GFlags test app for tab completion.
* License: Creative Commons 4.0 Attribution
* Compile with: g++ -o myapp myapp.cc -lgflags
*/
#include <gflags/gflags.h>
#include <gflags/gflags_completions.h>
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
return 0;
}
To test this, go through these steps in a bash terminal:
$ g++ -o myapp myapp.cc -lgflags # Compile the example.
$ which gflags_completions.sh # Check the path of gflags_compl...
/usr/local/bin/gflags_completions.sh # I use this path below.
# Tell bash to use autocomplete for myapp.
$ complete -o bashdefault -o default -o nospace -C \
'/usr/local/bin/gflags_completions.sh --tab_completion_columns $COLUMNS'\
time env myapp
The resulting output is:
$ ./myapp -[TAB]
~
-----------------
--flagfile [''] load flags from file
--fromenv [''] set flags from the environment [use 'export FLAGS_f'...
--help [false] show help on all flags [tip: all flags can have two ...
--helpfull [false] show help on all flags -- same as -help
--helpmatch [''] show help on modules whose name contains the speci...
--helpon [''] show help on the modules named by this flag value
--helppackage [false] show help on all modules in the main package
--helpshort [false] show help on only the main module for this program
--helpxml [false] produce an xml version of help
-* Other flags *-
--tab_completion_columns [80] Number of columns to use in output fo...
--tab_completion_word [''] If non-empty, HandleCommandLineCompletio...
--tryfromenv [''] set flags from the environment if present
--undefok [''] comma-separated list of flag names that it is okay t...
--version [false] show version and build info and exit
Snipplet from gflags_completions.h.in:
How to have bash accept completions from a binary:
Bash requires that it be informed about each command that programmatic
completion should be enabled for. Example addition to a .bashrc
file would be (your path to gflags_completions.sh file may differ):
$ complete -o bashdefault -o default -o nospace -C \
'/home/build/eng/bash/bash_completions.sh --tab_completion_columns $COLUMNS'\
time env binary_name another_binary [...]
This would allow the following to work:
$ /path/to/binary_name --vmodule<TAB>
Or:
$ ./bin/path/another_binary --gfs_u<TAB>
(etc)
Sadly, it appears that bash gives no easy way to force this behavior
for all commands. That's where the "time" in the above
example comes in. If you haven't specifically added a
command to the list of completion supported commands, you
can still get completions by prefixing the entire
command with "env".
$ env /some/brand/new/binary --vmod<TAB>
Assuming that "binary" is a newly compiled binary, this should still
produce the expected completion output.