gpt4 book ai didi

c++ - 如何使用 gflags 为您自己的项目获取 bash 选项卡补全?

转载 作者:行者123 更新时间:2023-11-30 05:38:53 30 4
gpt4 key购买 nike

我刚刚开始使用 gflags对于我正在从事的几个 C++ 项目。我发现 --help 返回了大量标志,我目前正在使用 myapp --help | less 找到我正在寻找的标志。我很乐意为此完成制表符。

今晚我发现了 files for completion在 glags 的 git 仓库中。并且命令 gflags_completions.sh 已使用 gflags 安装在我的 bash 环境中。

问题

  1. 是否有任何关于 gflags_completion.sh 的文档以及如何使用它? gflags_completions.h.in
  2. 如何使用 gflags_completion.sh 脚本为我自己的应用程序创建完成条目? (见下面的答案)

最小示例:myapp.cc

要编译此文件,请运行 g++ -o myapp myapp.cc -lgflags

/** GFlags test app for tab completion.
* License: Creative Commons 4.0 Attribution
* Compile with:
* g++ -o myapp myapp.cc -lgflags
*/
#include <gflags/gflags.h>

int main(int argc, char **argv) {
google::ParseCommandLineFlags(&argc, &argv, true);
return 0;
}

我认为应该有效

在阅读了一点关于 complete 的内容之后命令,我相信这应该有效,但我没有成功。

$ complete -o nospace -C \
'/usr/local/bin/gflags_completions.sh --tab_completion_columns $COLUMNS' \
time env myapp
$ ./myapp -[TAB]

相关问题:How does bash tab completion work?

最佳答案

好的,在 friend 的帮助下,我找到了文件 gflags_completions.h.in此功能的文档所在的位置。我在这个答案的末尾粘贴了一部分头文件。

我上面的问题中缺少的是 gflags_completions.h 的 incude 语句。

最小工作示例: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;
}

要对此进行测试,请在 bash 终端中执行以下步骤:

$ 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

结果输出为:

$ ./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

片段来自 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.

关于c++ - 如何使用 gflags 为您自己的项目获取 bash 选项卡补全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555861/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com