summaryrefslogtreecommitdiffstats
path: root/completions/python
blob: 5bf4b708e99d2f159266c27941e032330c4ccd99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# bash completion for python                               -*- shell-script -*-

# @since 2.12
_comp_xfunc_python_compgen_modules()
{
    local _python=python
    [[ ${comp_args[0]##*/} == *3* ]] && _python=python3
    _comp_cmd_python__compgen_modules "$_python"
}

# @deprecated 2.12 use `_comp_xfunc_python_compgen_modules` instead
_python_modules()
{
    _comp_compgen -a -i python modules "${1:-python}"
}

_comp_cmd_python__compgen_modules()
{
    _comp_compgen_split -- "$("$1" \
        "${BASH_SOURCE[0]%/*}/../helpers/python" "$cur" 2>/dev/null)"
}

# @since 2.12
_comp_xfunc_python_compgen_warning_actions()
{
    _comp_compgen -- -W "ignore default all module once error" \
        ${prefix:+-P "$prefix"}
}

# @deprecated 2.12 use `_comp_xfunc_python_compgen_warning_actions` instead
_python_warning_actions()
{
    _comp_compgen -a -i python warning_actions "${1:-python}"
}

_comp_cmd_python()
{
    local cur prev words cword comp_args prefix
    _comp_initialize -- "$@" || return

    case $cur in
        -[QWX]?*)
            prefix=${cur:0:2}
            prev=$prefix
            cur=${cur:2}
            ;;
    esac

    local noargopts='!(-*|*[cmQWX]*)'

    # if command, module, or script is already given by [-c command | -m module
    # | script], complete all kind of files.
    local i has_command=""
    for ((i = 1; i < cword; i++)); do
        # shellcheck disable=SC2254
        case ${words[i]} in
            -${noargopts}[QWX])
                ((i++))
                ;;
            -${noargopts}[cm]?*)
                has_command=set
                break
                ;;
            -- | -${noargopts}[cm])
                if ((i + 1 < cword)); then
                    has_command=set
                    break
                fi
                ;;
            -*) ;;
            *)
                has_command=set
                break
                ;;
        esac
    done
    if [[ $has_command ]]; then
        _comp_compgen_filedir
        return
    fi

    # shellcheck disable=SC2254
    case $prev in
        --help | --version | -${noargopts}[?hVc])
            return
            ;;
        -${noargopts}m)
            _comp_cmd_python__compgen_modules "$1"
            return
            ;;
        -${noargopts}Q)
            _comp_compgen -- -W 'old new warn warnall' -P "$prefix"
            return
            ;;
        -${noargopts}W)
            _comp_xfunc_python_compgen_warning_actions
            return
            ;;
        -${noargopts}X)
            _comp_compgen_split -- "$("$1" -h 2>&1 |
                _comp_awk '$1 == "-X" && $2 ~ /:$/ {
                    sub(":$","",$2); sub("=.*","=",$2); print $2
                }')"
            [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
            return
            ;;
        --jit)
            # TODO: quite a few others, parse from "--jit help" output?
            _comp_compgen -- -W "help off"
            return
            ;;
    esac

    if [[ $prev == -- || $cur != -* ]]; then
        _comp_compgen_filedir '@(py?([cowz])|zip)'
    else
        _comp_compgen_help - <<<"$("$1" -h |
            _comp_awk '{ sub("\\(-bb:","\n-bb "); print }')"
    fi
} &&
    complete -F _comp_cmd_python \
        python python2 python2.7 python3 python3.{3..12} \
        pypy pypy3 pyston pyston3 micropython

# ex: filetype=sh