以编程方式获取命令的操作

Ion*_*zău 5 shell bash shell-script command

让我们git举个例子:

$ git<TAB><TAB>
git                 git-import-dscs     git-shell
git-buildpackage    git-import-orig     git-upload-archive
git-dch             git-pbuilder        git-upload-pack
git-import-dsc      git-receive-pack    
Run Code Online (Sandbox Code Playgroud)

注意之后git

$ git <tab><tab>
add                 fetch               push 
am                  filter-branch       rebase 
annotate            format-patch        reflog 
apply               fsck                relink 
archive             gc                  remote 
bisect              get-tar-commit-id   repack 
blame               grep                replace 
branch              help                request-pull 
buildpackage        imap-send           reset 
bundle              import-dsc          revert 
checkout            import-dscs         rm 
cherry              import-orig         shortlog 
cherry-pick         init                show 
clean               instaweb            show-branch 
clone               log                 stage 
column              merge               stash 
commit              mergetool           status 
config              mv                  submodule 
credential          name-rev            subtree 
--More--
Run Code Online (Sandbox Code Playgroud)

写作 git re

$ git re<tab><tab>
rebase         relink         repack         request-pull   revert 
reflog         remote         replace        reset          

$ git remote <tab><tab>
add            remove         set-branches   set-url        update 
prune          rename         set-head       show           
Run Code Online (Sandbox Code Playgroud)

一般来说:

$ command <tab> 
[actions]
Run Code Online (Sandbox Code Playgroud)

如何以编程方式获取这些操作?这可以通过 shell/bash 脚本实现吗?

slm*_*slm 7

这个工具是由所谓的 Bash Completion 完成的。支持它的文件存储在/etc/bash_completion.d每个命令都有自己的文件下。所以在这种git情况下:

/etc/bash_completion.d/git
Run Code Online (Sandbox Code Playgroud)

如果您查看此文件,您会注意到它使用额外的功能使您的环境过载。特别值得一提的是这个人:

$ __git_commands
Run Code Online (Sandbox Code Playgroud)

当你运行它时,你会得到子命令的列表。

$ __git_commands | head -5
  add                       merge-recursive
  add--interactive          merge-resolve
  am                        merge-subtree
  annotate                  merge-tree
  apply                     mergetool
Run Code Online (Sandbox Code Playgroud)

知道这些只是您环境中的函数后,您可以执行以下操作:

$ __git<tab><tab>
__git_aliased_command                __git_complete_remote_or_refspec     __git_diff_index_files               __git_index_files                    __git_refs
__git_aliases                        __git_complete_revlist               __git_diff_index_helper              __gitk_main                          __git_refs2
__git_commands                       __git_complete_revlist_file          __gitdir                             __git_list_all_commands              __git_refs_remotes
__gitcomp                            __git_complete_strategy              __git_find_on_cmdline                __git_list_merge_strategies          __git_remotes
__gitcompadd                         __gitcomp_nl                         __git_func_wrap                      __git_list_porcelain_commands        __git_tags
__gitcomp_file                       __git_compute_all_commands           __git_has_doubledash                 __git_ls_files_helper                __git_wrap__gitk_main
__git_complete                       __git_compute_merge_strategies       __git_heads                          __git_main                           __git_wrap__git_main
__git_complete_diff_index_file       __git_compute_porcelain_commands     __git_index_file_list_filter         __git_match_ctag                     
__git_complete_file                  __git_config_get_set_variables       __git_index_file_list_filter_bash    __git_pretty_aliases                 
__git_complete_index_file            __git_count_arguments                __git_index_file_list_filter_compat  __git_reassemble_comp_words_by_ref   
Run Code Online (Sandbox Code Playgroud)

获取所有 Bash Completion 函数的列表,这些函数提供有关git命令的各种信息。