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