고쳐 쓰다:

고쳐 쓰다:

내 사용자 정의 스크립트가 있습니다 zsh completion. 3개의 선택적 인수 를 사용 --insert하고 지정된 경로에서 파일을 완성합니다.--edit--rm

#compdef pass

_pass() {

  local -a args

  args+=(
    '--insert[Create a new password entry]'
    '--edit[Edit a password entry]'
    '--rm[Delete a password entry]'
  )

  _arguments $args '1: :->directory'

  case $state in
  directory)
    _path_files -W $HOME/passwords -g '*(/)' -S /
    _path_files -W $HOME/passwords -g '*.gpg(:r)' -S ' '
    ;;
  esac
}

완료( 및 -P입력 시 )를 제공하지만 경로 완성은 제공하지 않는 다른 옵션을 추가해야 합니다 . 이 옵션은 문자열만 허용해야 합니다. 따라서 경로와 일치해서는 안 되며 지정된 경우 추가 옵션을 제공해서는 안 됩니다.-TAB-P

완성 스크립트에 이 새 옵션을 어떻게 추가하나요?

고쳐 쓰다:

option 에서는 완료가 작동하지 않습니다 -P. 즉, 다음과 같이 하면 됩니다.

pass -P <TAB>

옵션 -P가 문자열을 기대하기 때문에 아무 것도 수행하지 않습니다. 이것은 좋다. 그러나 내가 할 때

pass -P foo <TAB> 

그것도 아무것도 성취하지 못합니다. 하지만 현재 경로의 디렉터리를 완성해야 합니다. 이것이 어떻게 달성될 수 있습니까?

답변1

언급한 모든 옵션이 상호 배타적이라고 가정하면 해결책은 다음과 같습니다.

#compdef pass

_pass() {
  local -a args=(
      # (-) makes an option mutually exclusive with all other options. 
      '(-)--insert[Create a new password entry]'
      '(-)--edit[Edit a password entry]'
      '(-)--rm[Delete a password entry]'
      '(-)-P:string:'
      '1:password entry:->directory'
  )

  _arguments $args

  case $state in
    directory)
      _path_files -W $HOME/passwords -g '*(/)' -S /
      _path_files -W $HOME/passwords -g '*.gpg(:r)' -S ' '
      ;;
  esac
}

문서는 여기에 있습니다:http://zsh.sourceforge.net/Doc/Release/Completion-System.html#index-_005farguments

관련 정보