hy-smi 2.93 KB
Newer Older
one's avatar
one committed
1
2
3
4
5
6
#!/usr/bin/env bash

_hy_smi_completion() {
    local cur prev subcmds
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
one's avatar
one committed
7
    [[ $COMP_CWORD -gt 0 ]] && prev="${COMP_WORDS[COMP_CWORD-1]}" || prev=""
one's avatar
one committed
8

one's avatar
one committed
9
    # Subcommands (distinct from --mig/--multi-instance-gpu set option)
one's avatar
one committed
10
11
    subcmds="virtual mig"

one's avatar
one committed
12
13
    # Lazily parse options from help output on first invocation.
    # To force a refresh after upgrading hy-smi, run: unset _HY_SMI_OPTS
one's avatar
one committed
14
    if [[ -z "$_HY_SMI_OPTS" ]]; then
one's avatar
one committed
15
16
17
18
19
        # Match both GNU-style long options (--foo-bar) and
        # tool-specific single-dash multi-char options (-idmon, -sdmon, etc.)
        _HY_SMI_OPTS=$(hy-smi -h 2>/dev/null \
            | grep -oP '(?<!\w)(--[A-Za-z][A-Za-z0-9-]*|-[A-Za-z][A-Za-z0-9]*)' \
            | sort -u)
one's avatar
one committed
20
21
    fi

one's avatar
one committed
22
    # Provide completions for options that take specific values
one's avatar
one committed
23
    case "${prev}" in
one's avatar
one committed
24
        # File arguments
one's avatar
one committed
25
26
27
28
        --load|--save|--filename|-fdmon)
            COMPREPLY=( $(compgen -f -- "${cur}") )
            return 0
            ;;
one's avatar
one committed
29
30

        # Enumerated string values
one's avatar
one committed
31
32
33
34
35
36
37
38
39
40
41
42
        --loglevel)
            COMPREPLY=( $(compgen -W "debug info warning error critical" -- "${cur}") )
            return 0
            ;;
        --setperflevel)
            COMPREPLY=( $(compgen -W "auto low high manual" -- "${cur}") )
            return 0
            ;;
        --setecc)
            COMPREPLY=( $(compgen -W "on off stable" -- "${cur}") )
            return 0
            ;;
one's avatar
one committed
43
44
45
46
47
48
49
50
51
52
53
        -tdmon)
            COMPREPLY=( $(compgen -W "text csv" -- "${cur}") )
            return 0
            ;;
        -sdmon)
            # Flags can be combined (e.g. cfpu), offer individual letters
            COMPREPLY=( $(compgen -W "c f p u" -- "${cur}") )
            return 0
            ;;

        # Binary 0/1 toggles
one's avatar
one committed
54
55
56
57
58
59
60
61
        --direction)
            COMPREPLY=( $(compgen -W "0 1" -- "${cur}") )
            return 0
            ;;
        --enablelowpower|--setautoloaddriver|--setautorunhyckptd|-mig|--multi-instance-gpu)
            COMPREPLY=( $(compgen -W "0 1" -- "${cur}") )
            return 0
            ;;
one's avatar
one committed
62
63

        # Multi-value enumerations
one's avatar
one committed
64
        --setboost)
one's avatar
one committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
            # Valid levels per help output: 0-AI(default), 2-AI(typical), 3-HPC(typical)
            # NOTE: 1 does NOT exist
            COMPREPLY=( $(compgen -W "0 2 3" -- "${cur}") )
            return 0
            ;;
        --setextendbw)
            # 0: off, 1: 1GB, 2: 2GB, 3: 4GB
            COMPREPLY=( $(compgen -W "0 1 2 3" -- "${cur}") )
            return 0
            ;;
        --channel)
            # 0-31 or all
            COMPREPLY=( $(compgen -W "$(seq 0 31) all" -- "${cur}") )
            return 0
            ;;
        --link)
            # 0-6 or all
            COMPREPLY=( $(compgen -W "0 1 2 3 4 5 6 all" -- "${cur}") )
one's avatar
one committed
83
84
85
86
            return 0
            ;;
    esac

one's avatar
one committed
87
    # Complete flags and subcommands
one's avatar
one committed
88
89
90
91
    COMPREPLY=( $(compgen -W "${_HY_SMI_OPTS} ${subcmds}" -- "${cur}") )
}

complete -o default -F _hy_smi_completion hy-smi