#compdef yubico-piv-tool

autoload -U is-at-least

_yubico-piv-tool() {
  typeset -A opt_args
  typeset -a _arguments_options
  local ret=1

  if is-at-least 5.2; then
    _arguments_options=(-s -S -C)
  else
    _arguments_options=(-s -C)
  fi

  local context curcontext="$curcontext" state line
  _arguments "${_arguments_options[@]}" : \
    '*:file:_files' \
    {-h,--help}'[Print help and exit]' \
    '--full-help[Print help, including hidden options, and exit]' \
    {-V,--version}'[Print version and exit]' \
    {-v,--verbose=}'[Print more information (default=0)]:INT:' \
    {-r,--reader=}'[Only use a matching reader (default=Yubikey)]:STRING:' \
    {-k,--key=}'[Management key to use, if no value is specified key will be asked for]:STRING:' \
    {-a,--action=}'[Action to take (verify-pin, generate, set-mgm-key, reset]:ENUM:(version generate set-mgm-key reset pin-retries import-key import-certificate set-chuid request-certificate verify-pin verify-bio change-pin change-puk unblock-pin self‐sign-certificate delete-certificate read-certificate read-public-key status test-signature test-decipher list-readers set-ccc write-object read-object attest move-key delete-key)' \
    {-s,--slot=}'[What key slot to operate on (9c PIN always, 9e PIN never)]:ENUM:(9a 9c 9d 9e 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 f9)' \
    '--to-slot=[What slot to move an existing key to (9c PIN always, 9e PIN never)]:ENUM:(9a 9c 9d 9e 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 f9)' \
    {-A,--algorithm=}'[What algorithm to use (default=RSA2048)]:ENUM:(RSA1024 RSA2048 RSA3072 RSA4096 ECCP256 ECCP384 ED25519 X25519)' \
    {-H,--hash=}'[Hash to use for signatures (default=SHA256)]:ENUM:(SHA1 SHA256 SHA384 SHA512)' \
    {-n,--new-key=}'[New management key to use for action set-mgm-key]:STRING:' \
    '--pin-retries=[Number of retries before the pin code is blocked]:INT:' \
    '--puk-retries=[Number of retries before the puk code is blocked]:INT:' \
    {-i,--input=}'[Filename to use as input, - for stdin (default=-)]:STRING:_files' \
    {-o,--output=}'[Filename to use as output, - for stdout (default=-)]:STRING:_files' \
    {-K,--key-format=}'[Format of the key being read/written (default=PEM)]:ENUM:(PEM PKCS12 GZIP DER SSH)' \
    '--compress[Compress a large certificate using GZIP before import (default=off)]' \
    '--global[Reset the whole device over all applications (default=off)]' \
    {-p,--password=}'[Password for decryption of private key file]:STRING:' \
    {-S,--subject=}'[The subject to use for certificate request]:STRING [/CN=host.example.com/OU=test/O=example.com/]:' \
    '--serial=[Serial number of the self-signed certificate]:INT:' \
    '--valid-days=[Time (in days) until the self-signed certificate expires (default=365)]:INT:' \
    {-P,--pin=}'[Pin/puk code for verification, if omitted pin/puk will be asked for]:STRING:' \
    {-N,--new-pin=}'[New pin/puk code for changing, if omitted pin/puk will be asked for]:STRING:' \
    '--pin-policy=[Set pin policy for action generate or import-key]:ENUM:(never once always matchonce matchalways)' \
    '--touch-policy=[Set touch policy for action generate, import-key or set-mgm-key]:ENUM:(never always cached)' \
    '--id=[Id of object for write/read object]:INT:' \
    {-f,--format=}'[Format of data for write/read object (default=hex)]:ENUM:(hex base64 binary)' \
    '--attestation[Add attestation cross-signature (default=off)]' \
    {-m,--new-key-algo=}'[New management key algorithm to use for action set-mgm-key(default=TDES)]:ENUM:(TDES AES128 AES192 AES256)' \
    '--enc[Communication with YubiKey is done over an encrypted channel(default=off)]' \
    && ret=0
}

(( $+functions[_yubico-piv-tool_commands] )) \
  || _yubico-piv-tool_commands() {
    local commands
    commands=()
    _describe -t commands 'yubico-piv-tool commands' commands "$@"
  }

if [ "$funcstack[1]" = "_yubico-piv-tool" ]; then
  _yubico-piv-tool "$@"
else
  compdef _yubico-piv-tool yubico-piv-tool
fi
