xref: /aosp_15_r20/external/gflags/src/gflags_completions.h.in (revision 08ab5237c114d5c0eac1090c56f941d3f639d7d3)
1*08ab5237SOystein Eftevaag// Copyright (c) 2008, Google Inc.
2*08ab5237SOystein Eftevaag// All rights reserved.
3*08ab5237SOystein Eftevaag//
4*08ab5237SOystein Eftevaag// Redistribution and use in source and binary forms, with or without
5*08ab5237SOystein Eftevaag// modification, are permitted provided that the following conditions are
6*08ab5237SOystein Eftevaag// met:
7*08ab5237SOystein Eftevaag//
8*08ab5237SOystein Eftevaag//     * Redistributions of source code must retain the above copyright
9*08ab5237SOystein Eftevaag// notice, this list of conditions and the following disclaimer.
10*08ab5237SOystein Eftevaag//     * Redistributions in binary form must reproduce the above
11*08ab5237SOystein Eftevaag// copyright notice, this list of conditions and the following disclaimer
12*08ab5237SOystein Eftevaag// in the documentation and/or other materials provided with the
13*08ab5237SOystein Eftevaag// distribution.
14*08ab5237SOystein Eftevaag//     * Neither the name of Google Inc. nor the names of its
15*08ab5237SOystein Eftevaag// contributors may be used to endorse or promote products derived from
16*08ab5237SOystein Eftevaag// this software without specific prior written permission.
17*08ab5237SOystein Eftevaag//
18*08ab5237SOystein Eftevaag// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*08ab5237SOystein Eftevaag// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*08ab5237SOystein Eftevaag// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*08ab5237SOystein Eftevaag// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*08ab5237SOystein Eftevaag// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*08ab5237SOystein Eftevaag// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*08ab5237SOystein Eftevaag// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*08ab5237SOystein Eftevaag// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*08ab5237SOystein Eftevaag// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*08ab5237SOystein Eftevaag// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*08ab5237SOystein Eftevaag// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*08ab5237SOystein Eftevaag//
30*08ab5237SOystein Eftevaag// ---
31*08ab5237SOystein Eftevaag
32*08ab5237SOystein Eftevaag//
33*08ab5237SOystein Eftevaag// Implement helpful bash-style command line flag completions
34*08ab5237SOystein Eftevaag//
35*08ab5237SOystein Eftevaag// ** Functional API:
36*08ab5237SOystein Eftevaag// HandleCommandLineCompletions() should be called early during
37*08ab5237SOystein Eftevaag// program startup, but after command line flag code has been
38*08ab5237SOystein Eftevaag// initialized, such as the beginning of HandleCommandLineHelpFlags().
39*08ab5237SOystein Eftevaag// It checks the value of the flag --tab_completion_word.  If this
40*08ab5237SOystein Eftevaag// flag is empty, nothing happens here.  If it contains a string,
41*08ab5237SOystein Eftevaag// however, then HandleCommandLineCompletions() will hijack the
42*08ab5237SOystein Eftevaag// process, attempting to identify the intention behind this
43*08ab5237SOystein Eftevaag// completion.  Regardless of the outcome of this deduction, the
44*08ab5237SOystein Eftevaag// process will be terminated, similar to --helpshort flag
45*08ab5237SOystein Eftevaag// handling.
46*08ab5237SOystein Eftevaag//
47*08ab5237SOystein Eftevaag// ** Overview of Bash completions:
48*08ab5237SOystein Eftevaag// Bash can be told to programatically determine completions for the
49*08ab5237SOystein Eftevaag// current 'cursor word'.  It does this by (in this case) invoking a
50*08ab5237SOystein Eftevaag// command with some additional arguments identifying the command
51*08ab5237SOystein Eftevaag// being executed, the word being completed, and the previous word
52*08ab5237SOystein Eftevaag// (if any).  Bash then expects a sequence of output lines to be
53*08ab5237SOystein Eftevaag// printed to stdout.  If these lines all contain a common prefix
54*08ab5237SOystein Eftevaag// longer than the cursor word, bash will replace the cursor word
55*08ab5237SOystein Eftevaag// with that common prefix, and display nothing.  If there isn't such
56*08ab5237SOystein Eftevaag// a common prefix, bash will display the lines in pages using 'more'.
57*08ab5237SOystein Eftevaag//
58*08ab5237SOystein Eftevaag// ** Strategy taken for command line completions:
59*08ab5237SOystein Eftevaag// If we can deduce either the exact flag intended, or a common flag
60*08ab5237SOystein Eftevaag// prefix, we'll output exactly that.  Otherwise, if information
61*08ab5237SOystein Eftevaag// must be displayed to the user, we'll take the opportunity to add
62*08ab5237SOystein Eftevaag// some helpful information beyond just the flag name (specifically,
63*08ab5237SOystein Eftevaag// we'll include the default flag value and as much of the flag's
64*08ab5237SOystein Eftevaag// description as can fit on a single terminal line width, as specified
65*08ab5237SOystein Eftevaag// by the flag --tab_completion_columns).  Furthermore, we'll try to
66*08ab5237SOystein Eftevaag// make bash order the output such that the most useful or relevent
67*08ab5237SOystein Eftevaag// flags are the most likely to be shown at the top.
68*08ab5237SOystein Eftevaag//
69*08ab5237SOystein Eftevaag// ** Additional features:
70*08ab5237SOystein Eftevaag// To assist in finding that one really useful flag, substring matching
71*08ab5237SOystein Eftevaag// was implemented.  Before pressing a <TAB> to get completion for the
72*08ab5237SOystein Eftevaag// current word, you can append one or more '?' to the flag to do
73*08ab5237SOystein Eftevaag// substring matching.  Here's the semantics:
74*08ab5237SOystein Eftevaag//   --foo<TAB>     Show me all flags with names prefixed by 'foo'
75*08ab5237SOystein Eftevaag//   --foo?<TAB>    Show me all flags with 'foo' somewhere in the name
76*08ab5237SOystein Eftevaag//   --foo??<TAB>   Same as prior case, but also search in module
77*08ab5237SOystein Eftevaag//                  definition path for 'foo'
78*08ab5237SOystein Eftevaag//   --foo???<TAB>  Same as prior case, but also search in flag
79*08ab5237SOystein Eftevaag//                  descriptions for 'foo'
80*08ab5237SOystein Eftevaag// Finally, we'll trim the output to a relatively small number of
81*08ab5237SOystein Eftevaag// flags to keep bash quiet about the verbosity of output.  If one
82*08ab5237SOystein Eftevaag// really wanted to see all possible matches, appending a '+' to the
83*08ab5237SOystein Eftevaag// search word will force the exhaustive list of matches to be printed.
84*08ab5237SOystein Eftevaag//
85*08ab5237SOystein Eftevaag// ** How to have bash accept completions from a binary:
86*08ab5237SOystein Eftevaag// Bash requires that it be informed about each command that programmatic
87*08ab5237SOystein Eftevaag// completion should be enabled for.  Example addition to a .bashrc
88*08ab5237SOystein Eftevaag// file would be (your path to gflags_completions.sh file may differ):
89*08ab5237SOystein Eftevaag
90*08ab5237SOystein Eftevaag/*
91*08ab5237SOystein Eftevaag$ complete -o bashdefault -o default -o nospace -C                            \
92*08ab5237SOystein Eftevaag '/home/build/eng/bash/bash_completions.sh --tab_completion_columns $COLUMNS' \
93*08ab5237SOystein Eftevaag  time  env  binary_name  another_binary  [...]
94*08ab5237SOystein Eftevaag*/
95*08ab5237SOystein Eftevaag
96*08ab5237SOystein Eftevaag// This would allow the following to work:
97*08ab5237SOystein Eftevaag//   $ /path/to/binary_name --vmodule<TAB>
98*08ab5237SOystein Eftevaag// Or:
99*08ab5237SOystein Eftevaag//   $ ./bin/path/another_binary --gfs_u<TAB>
100*08ab5237SOystein Eftevaag// (etc)
101*08ab5237SOystein Eftevaag//
102*08ab5237SOystein Eftevaag// Sadly, it appears that bash gives no easy way to force this behavior for
103*08ab5237SOystein Eftevaag// all commands.  That's where the "time" in the above example comes in.
104*08ab5237SOystein Eftevaag// If you haven't specifically added a command to the list of completion
105*08ab5237SOystein Eftevaag// supported commands, you can still get completions by prefixing the
106*08ab5237SOystein Eftevaag// entire command with "env".
107*08ab5237SOystein Eftevaag//   $ env /some/brand/new/binary --vmod<TAB>
108*08ab5237SOystein Eftevaag// Assuming that "binary" is a newly compiled binary, this should still
109*08ab5237SOystein Eftevaag// produce the expected completion output.
110*08ab5237SOystein Eftevaag
111*08ab5237SOystein Eftevaag
112*08ab5237SOystein Eftevaag#ifndef GFLAGS_COMPLETIONS_H_
113*08ab5237SOystein Eftevaag#define GFLAGS_COMPLETIONS_H_
114*08ab5237SOystein Eftevaag
115*08ab5237SOystein Eftevaagnamespace @GFLAGS_NAMESPACE@ {
116*08ab5237SOystein Eftevaag
117*08ab5237SOystein Eftevaagextern void HandleCommandLineCompletions(void);
118*08ab5237SOystein Eftevaag
119*08ab5237SOystein Eftevaag}
120*08ab5237SOystein Eftevaag
121*08ab5237SOystein Eftevaag#endif  // GFLAGS_COMPLETIONS_H_
122