#compdef py-spy

autoload -U is-at-least

_py-spy() {
    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[@]}" \
'-h[Print help information]' \
'--help[Print help information]' \
'-V[Print version information]' \
'--version[Print version information]' \
":: :_py-spy_commands" \
"*::: :->py-spy" \
&& ret=0
    case $state in
    (py-spy)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:py-spy-command-$line[1]:"
        case $line[1] in
            (record)
_arguments "${_arguments_options[@]}" \
'-p+[PID of a running python program to spy on]:pid: ' \
'--pid=[PID of a running python program to spy on]:pid: ' \
'-o+[Output filename]:filename: ' \
'--output=[Output filename]:filename: ' \
'-f+[Output file format]:format:(flamegraph raw speedscope)' \
'--format=[Output file format]:format:(flamegraph raw speedscope)' \
'-d+[The number of seconds to sample for]:duration: ' \
'--duration=[The number of seconds to sample for]:duration: ' \
'-r+[The number of samples to collect per second]:rate: ' \
'--rate=[The number of samples to collect per second]:rate: ' \
'--full-filenames[Show full Python filenames, instead of shortening to show only the package part]' \
'-s[Profile subprocesses of the original process]' \
'--subprocesses[Profile subprocesses of the original process]' \
'-F[Aggregate samples by function'\''s first line number, instead of current line number]' \
'--function[Aggregate samples by function'\''s first line number, instead of current line number]' \
'--nolineno[Do not show line numbers]' \
'-t[Show thread ids in the output]' \
'--threads[Show thread ids in the output]' \
'-g[Only include traces that are holding on to the GIL]' \
'--gil[Only include traces that are holding on to the GIL]' \
'-i[Include stack traces for idle threads]' \
'--idle[Include stack traces for idle threads]' \
'--capture[Captures output from child process]' \
'--hideprogress[Hides progress bar (useful for showing error output on record)]' \
'-n[Collect stack traces from native extensions written in Cython, C or C++]' \
'--native[Collect stack traces from native extensions written in Cython, C or C++]' \
'--nonblocking[Don'\''t pause the python process when collecting samples. Setting this option will reduce the performance impact of sampling, but may lead to inaccurate results]' \
'-h[Print help information]' \
'--help[Print help information]' \
'*::python_program -- commandline of a python program to run:' \
&& ret=0
;;
(top)
_arguments "${_arguments_options[@]}" \
'-p+[PID of a running python program to spy on]:pid: ' \
'--pid=[PID of a running python program to spy on]:pid: ' \
'-r+[The number of samples to collect per second]:rate: ' \
'--rate=[The number of samples to collect per second]:rate: ' \
'-s[Profile subprocesses of the original process]' \
'--subprocesses[Profile subprocesses of the original process]' \
'--full-filenames[Show full Python filenames, instead of shortening to show only the package part]' \
'-g[Only include traces that are holding on to the GIL]' \
'--gil[Only include traces that are holding on to the GIL]' \
'-i[Include stack traces for idle threads]' \
'--idle[Include stack traces for idle threads]' \
'-n[Collect stack traces from native extensions written in Cython, C or C++]' \
'--native[Collect stack traces from native extensions written in Cython, C or C++]' \
'--nonblocking[Don'\''t pause the python process when collecting samples. Setting this option will reduce the performance impact of sampling, but may lead to inaccurate results]' \
'-h[Print help information]' \
'--help[Print help information]' \
'*::python_program -- commandline of a python program to run:' \
&& ret=0
;;
(dump)
_arguments "${_arguments_options[@]}" \
'-p+[PID of a running python program to spy on]:pid: ' \
'--pid=[PID of a running python program to spy on]:pid: ' \
'--full-filenames[Show full Python filenames, instead of shortening to show only the package part]' \
'*-l[Show local variables for each frame. Passing multiple times (-ll) increases verbosity]' \
'*--locals[Show local variables for each frame. Passing multiple times (-ll) increases verbosity]' \
'-j[Format output as JSON]' \
'--json[Format output as JSON]' \
'-s[Profile subprocesses of the original process]' \
'--subprocesses[Profile subprocesses of the original process]' \
'-n[Collect stack traces from native extensions written in Cython, C or C++]' \
'--native[Collect stack traces from native extensions written in Cython, C or C++]' \
'--nonblocking[Don'\''t pause the python process when collecting samples. Setting this option will reduce the performance impact of sampling, but may lead to inaccurate results]' \
'-h[Print help information]' \
'--help[Print help information]' \
&& ret=0
;;
(completions)
_arguments "${_arguments_options[@]}" \
'-h[Print help information]' \
'--help[Print help information]' \
'::shell -- Shell type:(bash elvish fish powershell zsh)' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
'*::subcommand -- The subcommand whose help message to display:' \
&& ret=0
;;
        esac
    ;;
esac
}

(( $+functions[_py-spy_commands] )) ||
_py-spy_commands() {
    local commands; commands=(
'record:Records stack trace information to a flamegraph, speedscope or raw file' \
'top:Displays a top like view of functions consuming CPU' \
'dump:Dumps stack traces for a target program to stdout' \
'completions:Generate shell completions' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'py-spy commands' commands "$@"
}
(( $+functions[_py-spy__completions_commands] )) ||
_py-spy__completions_commands() {
    local commands; commands=()
    _describe -t commands 'py-spy completions commands' commands "$@"
}
(( $+functions[_py-spy__dump_commands] )) ||
_py-spy__dump_commands() {
    local commands; commands=()
    _describe -t commands 'py-spy dump commands' commands "$@"
}
(( $+functions[_py-spy__help_commands] )) ||
_py-spy__help_commands() {
    local commands; commands=()
    _describe -t commands 'py-spy help commands' commands "$@"
}
(( $+functions[_py-spy__record_commands] )) ||
_py-spy__record_commands() {
    local commands; commands=()
    _describe -t commands 'py-spy record commands' commands "$@"
}
(( $+functions[_py-spy__top_commands] )) ||
_py-spy__top_commands() {
    local commands; commands=()
    _describe -t commands 'py-spy top commands' commands "$@"
}

_py-spy "$@"
