#compdef cp

autoload -U is-at-least

_cp() {
    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[@]}" : \
'(-T --no-target-directory)-t+[cp-help-target-directory]:target-directory:_files -/' \
'(-T --no-target-directory)--target-directory=[cp-help-target-directory]:target-directory:_files -/' \
'--backup=[make a backup of each existing destination file]' \
'-S+[override the usual backup suffix]:SUFFIX:_default' \
'--suffix=[override the usual backup suffix]:SUFFIX:_default' \
'--update=[move only when the SOURCE file is newer than the destination file or when the destination file is missing]' \
'--reflink=[cp-help-reflink]' \
'*--preserve=[cp-help-preserve]' \
'*--no-preserve=[cp-help-no-preserve]' \
'--sparse=[cp-help-sparse]:WHEN:(never auto always)' \
'--context=[cp-help-context]' \
'(-t --target-directory)-T[cp-help-no-target-directory]' \
'(-t --target-directory)--no-target-directory[cp-help-no-target-directory]' \
'-i[cp-help-interactive]' \
'--interactive[cp-help-interactive]' \
'-l[cp-help-link]' \
'--link[cp-help-link]' \
'-n[cp-help-no-clobber]' \
'--no-clobber[cp-help-no-clobber]' \
'-R[cp-help-recursive]' \
'-r[cp-help-recursive]' \
'--recursive[cp-help-recursive]' \
'--strip-trailing-slashes[cp-help-strip-trailing-slashes]' \
'--debug[cp-help-debug]' \
'-v[cp-help-verbose]' \
'--verbose[cp-help-verbose]' \
'-s[cp-help-symbolic-link]' \
'--symbolic-link[cp-help-symbolic-link]' \
'-f[cp-help-force]' \
'--force[cp-help-force]' \
'--remove-destination[cp-help-remove-destination]' \
'-b[like --backup but does not accept an argument]' \
'-u[like --update but does not accept an argument]' \
'--attributes-only[cp-help-attributes-only]' \
'-p[cp-help-preserve-default]' \
'--preserve-default-attributes[cp-help-preserve-default]' \
'--parents[cp-help-parents]' \
'-P[cp-help-no-dereference]' \
'--no-dereference[cp-help-no-dereference]' \
'-L[cp-help-dereference]' \
'--dereference[cp-help-dereference]' \
'-H[cp-help-cli-symbolic-links]' \
'-a[cp-help-archive]' \
'--archive[cp-help-archive]' \
'-d[cp-help-no-dereference-preserve-links]' \
'-x[cp-help-one-file-system]' \
'--one-file-system[cp-help-one-file-system]' \
'-Z[cp-help-selinux]' \
'-g[cp-help-progress]' \
'--progress[cp-help-progress]' \
'--copy-contents[cp-help-copy-contents]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'*::paths:_files' \
&& ret=0
}

(( $+functions[_cp_commands] )) ||
_cp_commands() {
    local commands; commands=()
    _describe -t commands 'cp commands' commands "$@"
}

if [ "$funcstack[1]" = "_cp" ]; then
    _cp "$@"
else
    compdef _cp cp
fi
