1 // Copyright (c) 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // Bash-style command line flag completion for C++ binaries
34 // This module implements bash-style completions. It achieves this
35 // goal in the following broad chunks:
37 // 1) Take a to-be-completed word, and examine it for search hints
38 // 2) Identify all potentially matching flags
39 // 2a) If there are no matching flags, do nothing.
40 // 2b) If all matching flags share a common prefix longer than the
41 // completion word, output just that matching prefix
42 // 3) Categorize those flags to produce a rough ordering of relevence.
43 // 4) Potentially trim the set of flags returned to a smaller number
44 // that bash is happier with
45 // 5) Output the matching flags in groups ordered by relevence.
46 // 5a) Force bash to place most-relevent groups at the top of the list
47 // 5b) Trim most flag's descriptions to fit on a single terminal line
51 #include <string.h> // for strlen
59 #include "gflags/gflags.h"
60 #include "gflags/gflags_completions.h"
68 DEFINE_string(tab_completion_word, "",
69 "If non-empty, HandleCommandLineCompletions() will hijack the "
70 "process and attempt to do bash-style command line flag "
71 "completion on this value.");
72 DEFINE_int32(tab_completion_columns, 80,
73 "Number of columns to use in output for tab completion");
76 namespace GFLAGS_NAMESPACE {
80 // Function prototypes and Type forward declarations. Code may be
81 // more easily understood if it is roughly ordered according to
82 // control flow, rather than by C's "declare before use" ordering
83 struct CompletionOptions;
86 // The entry point if flag completion is to be used.
87 static void PrintFlagCompletionInfo(void);
90 // 1) Examine search word
91 static void CanonicalizeCursorWordAndSearchOptions(
92 const string &cursor_word,
93 string *canonical_search_token,
94 CompletionOptions *options);
96 static bool RemoveTrailingChar(string *str, char c);
99 // 2) Find all matches
100 static void FindMatchingFlags(
101 const vector<CommandLineFlagInfo> &all_flags,
102 const CompletionOptions &options,
103 const string &match_token,
104 set<const CommandLineFlagInfo *> *all_matches,
105 string *longest_common_prefix);
107 static bool DoesSingleFlagMatch(
108 const CommandLineFlagInfo &flag,
109 const CompletionOptions &options,
110 const string &match_token);
113 // 3) Categorize matches
114 static void CategorizeAllMatchingFlags(
115 const set<const CommandLineFlagInfo *> &all_matches,
116 const string &search_token,
117 const string &module,
118 const string &package_dir,
119 NotableFlags *notable_flags);
121 static void TryFindModuleAndPackageDir(
122 const vector<CommandLineFlagInfo> &all_flags,
124 string *package_dir);
127 // 4) Decide which flags to use
128 static void FinalizeCompletionOutput(
129 const set<const CommandLineFlagInfo *> &matching_flags,
130 CompletionOptions *options,
131 NotableFlags *notable_flags,
132 vector<string> *completions);
134 static void RetrieveUnusedFlags(
135 const set<const CommandLineFlagInfo *> &matching_flags,
136 const NotableFlags ¬able_flags,
137 set<const CommandLineFlagInfo *> *unused_flags);
141 static void OutputSingleGroupWithLimit(
142 const set<const CommandLineFlagInfo *> &group,
143 const string &line_indentation,
144 const string &header,
145 const string &footer,
146 bool long_output_format,
147 int *remaining_line_limit,
148 size_t *completion_elements_added,
149 vector<string> *completions);
152 static string GetShortFlagLine(
153 const string &line_indentation,
154 const CommandLineFlagInfo &info);
156 static string GetLongFlagLine(
157 const string &line_indentation,
158 const CommandLineFlagInfo &info);
164 // Try to deduce the intentions behind this completion attempt. Return the
165 // canonical search term in 'canonical_search_token'. Binary search options
166 // are returned in the various booleans, which should all have intuitive
167 // semantics, possibly except:
168 // - return_all_matching_flags: Generally, we'll trim the number of
169 // returned candidates to some small number, showing those that are
170 // most likely to be useful first. If this is set, however, the user
171 // really does want us to return every single flag as an option.
172 // - force_no_update: Any time we output lines, all of which share a
173 // common prefix, bash will 'helpfully' not even bother to show the
174 // output, instead changing the current word to be that common prefix.
175 // If it's clear this shouldn't happen, we'll set this boolean
176 struct CompletionOptions {
177 bool flag_name_substring_search;
178 bool flag_location_substring_search;
179 bool flag_description_substring_search;
180 bool return_all_matching_flags;
181 bool force_no_update;
184 // Notable flags are flags that are special or preferred for some
185 // reason. For example, flags that are defined in the binary's module
186 // are expected to be much more relevent than flags defined in some
187 // other random location. These sets are specified roughly in precedence
188 // order. Once a flag is placed in one of these 'higher' sets, it won't
189 // be placed in any of the 'lower' sets.
190 struct NotableFlags {
191 typedef set<const CommandLineFlagInfo *> FlagSet;
192 FlagSet perfect_match_flag;
193 FlagSet module_flags; // Found in module file
194 FlagSet package_flags; // Found in same directory as module file
195 FlagSet most_common_flags; // One of the XXX most commonly supplied flags
196 FlagSet subpackage_flags; // Found in subdirectories of package
201 // Tab completion implementation - entry point
202 static void PrintFlagCompletionInfo(void) {
203 string cursor_word = FLAGS_tab_completion_word;
204 string canonical_token;
205 CompletionOptions options = { };
206 CanonicalizeCursorWordAndSearchOptions(
211 DVLOG(1) << "Identified canonical_token: '" << canonical_token << "'";
213 vector<CommandLineFlagInfo> all_flags;
214 set<const CommandLineFlagInfo *> matching_flags;
215 GetAllFlags(&all_flags);
216 DVLOG(2) << "Found " << all_flags.size() << " flags overall";
218 string longest_common_prefix;
224 &longest_common_prefix);
225 DVLOG(1) << "Identified " << matching_flags.size() << " matching flags";
226 DVLOG(1) << "Identified " << longest_common_prefix
227 << " as longest common prefix.";
228 if (longest_common_prefix.size() > canonical_token.size()) {
229 // There's actually a shared common prefix to all matching flags,
230 // so may as well output that and quit quickly.
231 DVLOG(1) << "The common prefix '" << longest_common_prefix
232 << "' was longer than the token '" << canonical_token
233 << "'. Returning just this prefix for completion.";
234 fprintf(stdout, "--%s", longest_common_prefix.c_str());
237 if (matching_flags.empty()) {
238 VLOG(1) << "There were no matching flags, returning nothing.";
244 TryFindModuleAndPackageDir(all_flags, &module, &package_dir);
245 DVLOG(1) << "Identified module: '" << module << "'";
246 DVLOG(1) << "Identified package_dir: '" << package_dir << "'";
248 NotableFlags notable_flags;
249 CategorizeAllMatchingFlags(
255 DVLOG(2) << "Categorized matching flags:";
256 DVLOG(2) << " perfect_match: " << notable_flags.perfect_match_flag.size();
257 DVLOG(2) << " module: " << notable_flags.module_flags.size();
258 DVLOG(2) << " package: " << notable_flags.package_flags.size();
259 DVLOG(2) << " most common: " << notable_flags.most_common_flags.size();
260 DVLOG(2) << " subpackage: " << notable_flags.subpackage_flags.size();
262 vector<string> completions;
263 FinalizeCompletionOutput(
269 if (options.force_no_update)
270 completions.push_back("~");
272 DVLOG(1) << "Finalized with " << completions.size()
273 << " chosen completions";
275 for (vector<string>::const_iterator it = completions.begin();
276 it != completions.end();
278 DVLOG(9) << " Completion entry: '" << *it << "'";
279 fprintf(stdout, "%s\n", it->c_str());
284 // 1) Examine search word (and helper method)
285 static void CanonicalizeCursorWordAndSearchOptions(
286 const string &cursor_word,
287 string *canonical_search_token,
288 CompletionOptions *options) {
289 *canonical_search_token = cursor_word;
290 if (canonical_search_token->empty()) return;
292 // Get rid of leading quotes and dashes in the search term
293 if ((*canonical_search_token)[0] == '"')
294 *canonical_search_token = canonical_search_token->substr(1);
295 while ((*canonical_search_token)[0] == '-')
296 *canonical_search_token = canonical_search_token->substr(1);
298 options->flag_name_substring_search = false;
299 options->flag_location_substring_search = false;
300 options->flag_description_substring_search = false;
301 options->return_all_matching_flags = false;
302 options->force_no_update = false;
304 // Look for all search options we can deduce now. Do this by walking
305 // backwards through the term, looking for up to three '?' and up to
306 // one '+' as suffixed characters. Consume them if found, and remove
307 // them from the canonical search token.
308 int found_question_marks = 0;
309 int found_plusses = 0;
311 if (found_question_marks < 3 &&
312 RemoveTrailingChar(canonical_search_token, '?')) {
313 ++found_question_marks;
316 if (found_plusses < 1 &&
317 RemoveTrailingChar(canonical_search_token, '+')) {
324 switch (found_question_marks) { // all fallthroughs
325 case 3: options->flag_description_substring_search = true;
326 case 2: options->flag_location_substring_search = true;
327 case 1: options->flag_name_substring_search = true;
330 options->return_all_matching_flags = (found_plusses > 0);
333 // Returns true if a char was removed
334 static bool RemoveTrailingChar(string *str, char c) {
335 if (str->empty()) return false;
336 if ((*str)[str->size() - 1] == c) {
337 *str = str->substr(0, str->size() - 1);
344 // 2) Find all matches (and helper methods)
345 static void FindMatchingFlags(
346 const vector<CommandLineFlagInfo> &all_flags,
347 const CompletionOptions &options,
348 const string &match_token,
349 set<const CommandLineFlagInfo *> *all_matches,
350 string *longest_common_prefix) {
351 all_matches->clear();
352 bool first_match = true;
353 for (vector<CommandLineFlagInfo>::const_iterator it = all_flags.begin();
354 it != all_flags.end();
356 if (DoesSingleFlagMatch(*it, options, match_token)) {
357 all_matches->insert(&*it);
360 *longest_common_prefix = it->name;
362 if (longest_common_prefix->empty() || it->name.empty()) {
363 longest_common_prefix->clear();
366 string::size_type pos = 0;
367 while (pos < longest_common_prefix->size() &&
368 pos < it->name.size() &&
369 (*longest_common_prefix)[pos] == it->name[pos])
371 longest_common_prefix->erase(pos);
377 // Given the set of all flags, the parsed match options, and the
378 // canonical search token, produce the set of all candidate matching
379 // flags for subsequent analysis or filtering.
380 static bool DoesSingleFlagMatch(
381 const CommandLineFlagInfo &flag,
382 const CompletionOptions &options,
383 const string &match_token) {
384 // Is there a prefix match?
385 string::size_type pos = flag.name.find(match_token);
386 if (pos == 0) return true;
388 // Is there a substring match if we want it?
389 if (options.flag_name_substring_search &&
393 // Is there a location match if we want it?
394 if (options.flag_location_substring_search &&
395 flag.filename.find(match_token) != string::npos)
398 // TODO(user): All searches should probably be case-insensitive
399 // (especially this one...)
400 if (options.flag_description_substring_search &&
401 flag.description.find(match_token) != string::npos)
407 // 3) Categorize matches (and helper method)
409 // Given a set of matching flags, categorize them by
410 // likely relevence to this specific binary
411 static void CategorizeAllMatchingFlags(
412 const set<const CommandLineFlagInfo *> &all_matches,
413 const string &search_token,
414 const string &module, // empty if we couldn't find any
415 const string &package_dir, // empty if we couldn't find any
416 NotableFlags *notable_flags) {
417 notable_flags->perfect_match_flag.clear();
418 notable_flags->module_flags.clear();
419 notable_flags->package_flags.clear();
420 notable_flags->most_common_flags.clear();
421 notable_flags->subpackage_flags.clear();
423 for (set<const CommandLineFlagInfo *>::const_iterator it =
425 it != all_matches.end();
427 DVLOG(2) << "Examining match '" << (*it)->name << "'";
428 DVLOG(7) << " filename: '" << (*it)->filename << "'";
429 string::size_type pos = string::npos;
430 if (!package_dir.empty())
431 pos = (*it)->filename.find(package_dir);
432 string::size_type slash = string::npos;
433 if (pos != string::npos) // candidate for package or subpackage match
434 slash = (*it)->filename.find(
436 pos + package_dir.size() + 1);
438 if ((*it)->name == search_token) {
439 // Exact match on some flag's name
440 notable_flags->perfect_match_flag.insert(*it);
441 DVLOG(3) << "Result: perfect match";
442 } else if (!module.empty() && (*it)->filename == module) {
443 // Exact match on module filename
444 notable_flags->module_flags.insert(*it);
445 DVLOG(3) << "Result: module match";
446 } else if (!package_dir.empty() &&
447 pos != string::npos && slash == string::npos) {
448 // In the package, since there was no slash after the package portion
449 notable_flags->package_flags.insert(*it);
450 DVLOG(3) << "Result: package match";
452 // In the list of the XXX most commonly supplied flags overall
453 // TODO(user): Compile this list.
454 DVLOG(3) << "Result: most-common match";
455 } else if (!package_dir.empty() &&
456 pos != string::npos && slash != string::npos) {
457 // In a subdirectory of the package
458 notable_flags->subpackage_flags.insert(*it);
459 DVLOG(3) << "Result: subpackage match";
462 DVLOG(3) << "Result: not special match";
466 static void PushNameWithSuffix(vector<string>* suffixes, const char* suffix) {
468 StringPrintf("/%s%s", ProgramInvocationShortName(), suffix));
471 static void TryFindModuleAndPackageDir(
472 const vector<CommandLineFlagInfo> &all_flags,
474 string *package_dir) {
476 package_dir->clear();
478 vector<string> suffixes;
479 // TODO(user): There's some inherant ambiguity here - multiple directories
480 // could share the same trailing folder and file structure (and even worse,
481 // same file names), causing us to be unsure as to which of the two is the
482 // actual package for this binary. In this case, we'll arbitrarily choose.
483 PushNameWithSuffix(&suffixes, ".");
484 PushNameWithSuffix(&suffixes, "-main.");
485 PushNameWithSuffix(&suffixes, "_main.");
486 // These four are new but probably merited?
487 PushNameWithSuffix(&suffixes, "-test.");
488 PushNameWithSuffix(&suffixes, "_test.");
489 PushNameWithSuffix(&suffixes, "-unittest.");
490 PushNameWithSuffix(&suffixes, "_unittest.");
492 for (vector<CommandLineFlagInfo>::const_iterator it = all_flags.begin();
493 it != all_flags.end();
495 for (vector<string>::const_iterator suffix = suffixes.begin();
496 suffix != suffixes.end();
498 // TODO(user): Make sure the match is near the end of the string
499 if (it->filename.find(*suffix) != string::npos) {
500 *module = it->filename;
501 string::size_type sep = it->filename.rfind(PATH_SEPARATOR);
502 *package_dir = it->filename.substr(0, (sep == string::npos) ? 0 : sep);
509 // Can't specialize template type on a locally defined type. Silly C++...
510 struct DisplayInfoGroup {
513 set<const CommandLineFlagInfo *> *group;
515 int SizeInLines() const {
516 int size_in_lines = static_cast<int>(group->size()) + 1;
517 if (strlen(header) > 0) {
520 if (strlen(footer) > 0) {
523 return size_in_lines;
527 // 4) Finalize and trim output flag set
528 static void FinalizeCompletionOutput(
529 const set<const CommandLineFlagInfo *> &matching_flags,
530 CompletionOptions *options,
531 NotableFlags *notable_flags,
532 vector<string> *completions) {
534 // We want to output lines in groups. Each group needs to be indented
535 // the same to keep its lines together. Unless otherwise required,
536 // only 99 lines should be output to prevent bash from harassing the
539 // First, figure out which output groups we'll actually use. For each
540 // nonempty group, there will be ~3 lines of header & footer, plus all
541 // output lines themselves.
542 int max_desired_lines = // "999999 flags should be enough for anyone. -dave"
543 (options->return_all_matching_flags ? 999999 : 98);
544 int lines_so_far = 0;
546 vector<DisplayInfoGroup> output_groups;
547 bool perfect_match_found = false;
548 if (lines_so_far < max_desired_lines &&
549 !notable_flags->perfect_match_flag.empty()) {
550 perfect_match_found = true;
551 DisplayInfoGroup group =
554 ¬able_flags->perfect_match_flag };
555 lines_so_far += group.SizeInLines();
556 output_groups.push_back(group);
558 if (lines_so_far < max_desired_lines &&
559 !notable_flags->module_flags.empty()) {
560 DisplayInfoGroup group = {
561 "-* Matching module flags *-",
562 "===========================",
563 ¬able_flags->module_flags };
564 lines_so_far += group.SizeInLines();
565 output_groups.push_back(group);
567 if (lines_so_far < max_desired_lines &&
568 !notable_flags->package_flags.empty()) {
569 DisplayInfoGroup group = {
570 "-* Matching package flags *-",
571 "============================",
572 ¬able_flags->package_flags };
573 lines_so_far += group.SizeInLines();
574 output_groups.push_back(group);
576 if (lines_so_far < max_desired_lines &&
577 !notable_flags->most_common_flags.empty()) {
578 DisplayInfoGroup group = {
579 "-* Commonly used flags *-",
580 "=========================",
581 ¬able_flags->most_common_flags };
582 lines_so_far += group.SizeInLines();
583 output_groups.push_back(group);
585 if (lines_so_far < max_desired_lines &&
586 !notable_flags->subpackage_flags.empty()) {
587 DisplayInfoGroup group = {
588 "-* Matching sub-package flags *-",
589 "================================",
590 ¬able_flags->subpackage_flags };
591 lines_so_far += group.SizeInLines();
592 output_groups.push_back(group);
595 set<const CommandLineFlagInfo *> obscure_flags; // flags not notable
596 if (lines_so_far < max_desired_lines) {
597 RetrieveUnusedFlags(matching_flags, *notable_flags, &obscure_flags);
598 if (!obscure_flags.empty()) {
599 DisplayInfoGroup group = {
603 lines_so_far += group.SizeInLines();
604 output_groups.push_back(group);
608 // Second, go through each of the chosen output groups and output
609 // as many of those flags as we can, while remaining below our limit
610 int remaining_lines = max_desired_lines;
611 size_t completions_output = 0;
612 int indent = static_cast<int>(output_groups.size()) - 1;
613 for (vector<DisplayInfoGroup>::const_iterator it =
614 output_groups.begin();
615 it != output_groups.end();
617 OutputSingleGroupWithLimit(
619 string(indent, ' '), // line indentation
620 string(it->header), // header
621 string(it->footer), // footer
622 perfect_match_found, // long format
623 &remaining_lines, // line limit - reduces this by number printed
624 &completions_output, // completions (not lines) added
625 completions); // produced completions
626 perfect_match_found = false;
629 if (completions_output != matching_flags.size()) {
630 options->force_no_update = false;
631 completions->push_back("~ (Remaining flags hidden) ~");
633 options->force_no_update = true;
637 static void RetrieveUnusedFlags(
638 const set<const CommandLineFlagInfo *> &matching_flags,
639 const NotableFlags ¬able_flags,
640 set<const CommandLineFlagInfo *> *unused_flags) {
641 // Remove from 'matching_flags' set all members of the sets of
642 // flags we've already printed (specifically, those in notable_flags)
643 for (set<const CommandLineFlagInfo *>::const_iterator it =
644 matching_flags.begin();
645 it != matching_flags.end();
647 if (notable_flags.perfect_match_flag.count(*it) ||
648 notable_flags.module_flags.count(*it) ||
649 notable_flags.package_flags.count(*it) ||
650 notable_flags.most_common_flags.count(*it) ||
651 notable_flags.subpackage_flags.count(*it))
653 unused_flags->insert(*it);
657 // 5) Output matches (and helper methods)
659 static void OutputSingleGroupWithLimit(
660 const set<const CommandLineFlagInfo *> &group,
661 const string &line_indentation,
662 const string &header,
663 const string &footer,
664 bool long_output_format,
665 int *remaining_line_limit,
666 size_t *completion_elements_output,
667 vector<string> *completions) {
668 if (group.empty()) return;
669 if (!header.empty()) {
670 if (*remaining_line_limit < 2) return;
671 *remaining_line_limit -= 2;
672 completions->push_back(line_indentation + header);
673 completions->push_back(line_indentation + string(header.size(), '-'));
675 for (set<const CommandLineFlagInfo *>::const_iterator it = group.begin();
676 it != group.end() && *remaining_line_limit > 0;
678 --*remaining_line_limit;
679 ++*completion_elements_output;
680 completions->push_back(
682 ? GetLongFlagLine(line_indentation, **it)
683 : GetShortFlagLine(line_indentation, **it)));
685 if (!footer.empty()) {
686 if (*remaining_line_limit < 1) return;
687 --*remaining_line_limit;
688 completions->push_back(line_indentation + footer);
692 static string GetShortFlagLine(
693 const string &line_indentation,
694 const CommandLineFlagInfo &info) {
696 bool is_string = (info.type == "string");
697 SStringPrintf(&prefix, "%s--%s [%s%s%s] ",
698 line_indentation.c_str(),
700 (is_string ? "'" : ""),
701 info.default_value.c_str(),
702 (is_string ? "'" : ""));
704 FLAGS_tab_completion_columns - static_cast<int>(prefix.size());
708 (static_cast<int>(info.description.size()) > remainder ?
709 (info.description.substr(0, remainder - 3) + "...").c_str() :
710 info.description.c_str());
711 return prefix + suffix;
714 static string GetLongFlagLine(
715 const string &line_indentation,
716 const CommandLineFlagInfo &info) {
718 string output = DescribeOneFlag(info);
720 // Replace '-' with '--', and remove trailing newline before appending
721 // the module definition location.
722 string old_flagname = "-" + info.name;
724 output.find(old_flagname),
727 // Stick a newline and indentation in front of the type and default
728 // portions of DescribeOneFlag()s description
729 static const char kNewlineWithIndent[] = "\n ";
730 output.replace(output.find(" type:"), 1, string(kNewlineWithIndent));
731 output.replace(output.find(" default:"), 1, string(kNewlineWithIndent));
732 output = StringPrintf("%s Details for '--%s':\n"
734 line_indentation.c_str(),
737 info.filename.c_str());
739 // Eliminate any doubled newlines that crept in. Specifically, if
740 // DescribeOneFlag() decided to break the line just before "type"
741 // or "default", we don't want to introduce an extra blank line
742 static const string line_of_spaces(FLAGS_tab_completion_columns, ' ');
743 static const char kDoubledNewlines[] = "\n \n";
744 for (string::size_type newlines = output.find(kDoubledNewlines);
745 newlines != string::npos;
746 newlines = output.find(kDoubledNewlines))
747 // Replace each 'doubled newline' with a single newline
748 output.replace(newlines, sizeof(kDoubledNewlines) - 1, string("\n"));
750 for (string::size_type newline = output.find('\n');
751 newline != string::npos;
752 newline = output.find('\n')) {
753 int newline_pos = static_cast<int>(newline) % FLAGS_tab_completion_columns;
754 int missing_spaces = FLAGS_tab_completion_columns - newline_pos;
755 output.replace(newline, 1, line_of_spaces, 1, missing_spaces);
761 void HandleCommandLineCompletions(void) {
762 if (FLAGS_tab_completion_word.empty()) return;
763 PrintFlagCompletionInfo();
768 } // namespace GFLAGS_NAMESPACE