프로그래밍 방식으로 명령 동작 가져오기

프로그래밍 방식으로 명령 동작 가져오기

예 를 살펴보겠습니다 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    

다음 사항에 주의하세요 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--

글쓰기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           

일반적으로 말하면:

$ command <tab> 
[actions]

프로그래밍 방식으로 이러한 작업을 수행하려면 어떻게 해야 합니까? 쉘/bash 스크립트를 통해 이를 달성할 수 있습니까?

답변1

이 기능은 소위 Bash Completion을 통해 수행됩니다. 이 작업을 지원하는 파일은 /etc/bash_completion.d각 명령마다 고유한 파일이 있는 디렉터리에 저장됩니다 . 따라서 이 git경우:

/etc/bash_completion.d/git

이 파일을 보면 추가 기능으로 인해 환경에 과부하가 걸리는 것을 알 수 있습니다. 특히 이 사람은:

$ __git_commands

실행하면 하위 명령 목록이 표시됩니다.

$ __git_commands | head -5
  add                       merge-recursive
  add--interactive          merge-resolve
  am                        merge-subtree
  annotate                  merge-tree
  apply                     mergetool

이것이 단지 환경의 특징임을 알고 있으면 다음을 수행할 수 있습니다.

$ __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   

git명령에 대한 다양한 정보를 제공하는 모든 Bash 완료 함수 목록을 가져옵니다 .

답변2

여기 보이는 것이 바로프로그래밍 가능한 완성. Debian/Ubunutu 기반 시스템에서 패키지는 일반적으로 /usr/share/bash-completion/completions명령에 대해 프로그래밍 가능한 완성 기능을 제공하는 파일을 설치합니다 . 다른 배포판에서는 /etc/bash_completion.d이 디렉터리를 사용할 수 있습니다(이 위치는 Debian/Ubuntu에서는 더 이상 사용되지 않지만 일부 패키지에서는 여전히 사용합니다). 나에게 완성을 생성하는 함수가 포함된 파일 git/usr/share/bash-completion/completions/.

기본 프로세스는 쉘 배열의 내용을 생성하는 함수를 정의한 다음 쉘 내장 기능을 사용하여 COMPREPLY이러한 내용을 특정 명령에 등록하는 것입니다.complete

작업 수행 방식을 변경하려면 시스템 업데이트로 인해 변경 사항이 손상될 수 있으므로 이러한 파일을 직접 변경하지 않는 것이 가장 좋습니다. 대신 .bashrc사용자별 위치(예: 다음 위치)에 필요한 기능의 새 버전 디렉터리를 만듭니다. 집).

관련 정보