Bash 내장 사용: 완료(도와주세요!)

Bash 내장 사용: 완료(도와주세요!)

페이지를 찾을 수 없는 것 같습니다 man complete. 따라서 사람들이 전체 명령(에서 complete --help)의 옵션을 설명하는 데 도움을 줄 수 있기를 바랍니다.

complete: complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
Specify how arguments are to be completed by Readline.

For each NAME, specify how arguments are to be completed.  If no options
are supplied, existing completion specifications are printed in a way that
allows them to be reused as input.

Options:
  -p        print existing completion specifications in a reusable format
  -r        remove a completion specification for each NAME, or, if no
            NAMEs are supplied, all completion specifications
  -D        apply the completions and actions as the default for commands
            without any specific completion defined
  -E        apply the completions and actions to "empty" commands --
            completion attempted on a blank line

When completion is attempted, the actions are applied in the order the
uppercase-letter options are listed above.  The -D option takes
precedence over -E.

bash옵션 1로 스크립트 명령을 수행한 다음 (이상적으로는 적절한 경우) 유효한 파일 및/또는 디렉터리를 사용하려고 합니다 . 이것을 사용할 수 있습니까 complete? 아니면 다른 접근 방식이 필요합니까?

이제 나는 이것을 가지고 있습니다 :

complete -W "mount unmount" phone

...이라는 스크립트의 경우 phone.

답변1

귀하의 요청에 따라"전체 명령의 옵션 설명 도움말":

모든 세부 사항을 설명하려면 이야기가 길어지므로 이 기사 마지막 부분에서 도움이 될 것으로 예상되는 참조 문서 목록을 찾아보세요.

$1명령 및 파일로 수행되는 특정 사용 사례와 관련하여 $2다음 섹션에 작은 예를 추가했습니다.

완성 함수 작성위치 차이 처리:

# completion function to differentiate type of completion by argument type
_complete_phone () {
  case ${#COMP_WORDS[@]} in
    1) # 1st position is the `phone` command itself
       return;;
    2) # 2nd position are sub-commands, so we use `-W` for word completion
       words="mount unmount"
       COMPREPLY=($(compgen -W "$words" -- "${COMP_WORDS[COMP_CWORD]}"))
       ;;
    *) # other arguments filter files, either
       # any files using option `-f`
       #COMPREPLY=($(compgen -f -- "${COMP_WORDS[COMP_CWORD]}"))
       # and for specific files (i.e. pattern `*.phone`), use words again
       COMPREPLY=($(compgen -W "$(ls *.phone 2>/dev/null)" -- "${COMP_WORDS[COMP_CWORD]}"))
       ;;
  esac
}

그 다음에함수를 통해 완성 활성화매개변수 활성화를 통해 -F완성 함수 이름을 사용하세요.

# complete using your function 
complete -F _complete_phone phone

마지막으로 다음을 시도해 보십시오(재현성을 위해 phone임의의 프로그램과 "관련" 파일을 만들어야 함).

# try
phone () { echo "hello, phone program will $@"; }
touch {my,your}.phone
phone m\tab \tab \tab

문서

배쉬 참조 매뉴얼

정보가 필요할 때 bash.

특히 bash 완료에 대해서는 해당 장을 참조하십시오.

원천:www.gnu.org

배쉬 매뉴얼

실행 man bash하고 섹션으로 이동합니다.

  • READLINE

    • Readline Variablesset완료 관련 변수를 처리하는 방법을 설명합니다 . 예를 들어 set completion-ignore-case on"대소문자를 구분하지 않고 파일 이름 일치 및 완성을 수행합니다"입니다.
    • Completing기본 bash 완성.
    • Programmable Completion완료된 프로세스와 메소드 compgen, 변수 등 위의 예에서 발견된 관련 요소를 설명하세요.COMP_WORDS
  • SHELL BUILTIN COMMANDS

    • compgen완성 일치를 생성합니다.
    • complete수행 방법을 지정합니다. -W, 등 -F의 모든 옵션 도 설명됩니다 -o.
    • compopt추가 완료 옵션을 설정합니다.

출처: GNU bash, 버전 5.1.16(1)-릴리스(x86_64-pc-linux-gnu)

관련 정보