#compdef -P python[0-9.]#

# Python 2.7
# Python 3.11

local curcontext="$curcontext" state state_descr line _compskip="$_compskip"
typeset -A opt_args
local -a args

if _pick_variant python3=Python\ 3 python2 --version; then
  args=(
    '(-bb)-b[issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str]'
    '(-b)-bb[issue errors about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str]'
    '--check-hash-based-pycs[configure how Python evaluates up-to-dateness of hash-based .pyc files]:mode:(always default never)'
    '--help-env[print help about Python environment variables and exit]'
    '--help-xoptions[print help about implementation-specific -X options and exit]'
    '--help-all[print complete help information and exit]'
    "-I[isolate Python from the user's environment]"
    '-P[do not prepend a potentially unsafe path to sys.path]'
    '-q[do not print version and copyright messages]'
    '-X[set implementation-specific option]:option'
  )
else
  args=(
    '-R[use a pseudo-random salt to make hash values unpredictable]'
    '-Q+[division options]:division option:(old warn warnall new)'
    '(-tt)-t[issue warnings about inconsistent tab usage]'
    '(-t)-tt[issue errors about inconsistent tab usage]'
    '-3[warn about Python 3.x incompatibilities]'
  )
fi

_arguments -C -s -S "$args[@]" \
  "-B[don't write .py\[co\] files on import]" \
  '(1 -)-c+[program passed in as string (terminates option list)]:python command' \
  '-d[debug output from parser]' \
  '-E[ignore PYTHON* environment variables (such as PYTHONPATH)]' \
  '(1 * -)-h[display help information]' \
  '-i[inspect interactively after running script]' \
  '(1 -)-m+[run library module as a script (terminates option list)]:module:_python_modules' \
  '-O[optimize generated bytecode slightly]' \
  '-OO[remove doc-strings in addition to the -O optimizations]' \
  "-s[don't add user site directory to sys.path]" \
  "-S[don't imply 'import site' on initialization]" \
  '-u[unbuffered binary stdout and stderr]' \
  '(-vv)-v[trace module initialization and cleanup]' \
  '(-v)-vv[in addition to -v, trace all files checked when searching for a module]' \
  '(1 * -)-V[display version information]' \
  '-W+[warning control]:warning filter (action\:message\:category\:module\:lineno):(default always ignore module once error)' \
  '-x[skip first line of source, allowing use of non-Unix forms of #!cmd]' \
  '(-)1:Python script:_files -g "*.py(|c|o)(-.)"' \
  '*::script argument:= ->normal' && return

case "$state" in
  normal)
    if [[ -z "$opt_args[(I)-(c|m)]" ]]; then
      shift words
      (( CURRENT-- ))
    elif [[ -n "$opt_args[(I)-m]" ]]; then
      local ret
      _call_function ret _python_module-$opt_args[-m] && return ret
    fi
    _normal && return
    ;;
esac

return 1
