"vscode:/vscode.git/clone" did not exist on "ee382ad9d40906c7933356c7baf0a5bdbe6b3b97"
bash-completion 6.21 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
# list of commands/arguments
Junwei Sun's avatar
Junwei Sun committed
2
3
4
__nnictl_cmds="create resume view update stop trial experiment platform webui config log package tensorboard top ss_gen --version"
__nnictl_create_cmds="--config --port --debug --foreground"
__nnictl_resume_cmds="--port --debug --foreground"
liuzhe-lz's avatar
liuzhe-lz committed
5
__nnictl_view_cmds="--port"
6
__nnictl_update_cmds="searchspace concurrency duration trialnum"
liuzhe-lz's avatar
liuzhe-lz committed
7
8
9
__nnictl_update_searchspace_cmds="--filename"
__nnictl_update_concurrency_cmds="--value"
__nnictl_update_duration_cmds="--value"
10
__nnictl_update_trialnum_cmds="--value"
11
__nnictl_stop_cmds="--port --all"
Junwei Sun's avatar
Junwei Sun committed
12
13
__nnictl_trial_cmds="ls kill"
__nnictl_trial_ls_cmds="--head --tail"
14
__nnictl_trial_kill_cmds="--trial_id"
Junwei Sun's avatar
Junwei Sun committed
15
__nnictl_experiment_cmds="show status list delete export import save load"
16
17
__nnictl_experiment_list_cmds="--all"
__nnictl_experiment_delete_cmds="--all"
Junwei Sun's avatar
Junwei Sun committed
18
19
20
21
__nnictl_experiment_export_cmds="--filename --type --intermediate"
__nnictl_experiment_import_cmds="--filename"
__nnictl_experiment_save_cmds="--path --saveCodeDir"
__nnictl_experiment_load_cmds="--path --codeDir --logDir"
22
23
__nnictl_platform_cmds="clean"
__nnictl_platform_clean_cmds="--config"
24
__nnictl_webui_cmds="url"
liuzhe-lz's avatar
liuzhe-lz committed
25
__nnictl_config_cmds="show"
26
__nnictl_log_cmds="stdout stderr trial"
liuzhe-lz's avatar
liuzhe-lz committed
27
28
__nnictl_log_stdout_cmds="--tail --head --path"
__nnictl_log_stderr_cmds="--tail --head --path"
29
__nnictl_log_trial_cmds="--trial_id"
Junwei Sun's avatar
Junwei Sun committed
30
__nnictl_package_cmds="install show list uninstall"
31
__nnictl_package_install_cmds="--name"
Junwei Sun's avatar
Junwei Sun committed
32
__nnictl_package_list_cmds="--all"
33
34
35
__nnictl_tensorboard_cmds="start stop"
__nnictl_tensorboard_start_cmds="--trial_id --port"
__nnictl_top_cmds="--time"
Junwei Sun's avatar
Junwei Sun committed
36
__nnictl_ss_gen_cmds="--trial_command --trial_dir --file"
liuzhe-lz's avatar
liuzhe-lz committed
37

38
# list of commands that accept an experiment ID as second argument
Junwei Sun's avatar
Junwei Sun committed
39
__nnictl_2nd_expid_cmds=" resume view stop top "
40
41
# list of commands that accept an experiment ID as third argument
__nnictl_3rd_expid_cmds=" update trial experiment webui config log tensorboard "
liuzhe-lz's avatar
liuzhe-lz committed
42
43
44
45
46
47


# remove already set arguments from candidates
__nnictl_remain_args()
{
    local ret=${!1}  # ret = $__nnictl_xxx_cmds
Junwei Sun's avatar
Junwei Sun committed
48
49
50
51
52
53
    # prevent that "--trial_id" changes to "--_id" in this situation: "nnictl trial kill --trial_id"
    if [[ ${ret} != "--trial_id" ]]; then
        for arg in "${COMP_WORDS[@]::${#COMP_WORDS[@]}-1}"; do
            local ret=${ret/$arg/}  # remove it from $ret
        done
    fi
liuzhe-lz's avatar
liuzhe-lz committed
54
55
56
    echo $ret
}

57
58
59
60
61
62
63
64
65
66
67
# complete files with specific extension
__nnictl_complete_extension()
{
    COMPREPLY=($(compgen -f -X "!*.$1" -- ${COMP_WORDS[-1]}))
    if [[ -z "${COMPREPLY[*]}" ]]; then
        # if there is no matching file here, search in sub-directories
        COMPREPLY=($(compgen -d -S "/" -- ${COMP_WORDS[-1]}))
        compopt -o nospace
    fi
}

liuzhe-lz's avatar
liuzhe-lz committed
68
69
_nnictl()
{
70
71
    local cur=${COMP_WORDS[-1]}
    local last=${COMP_WORDS[-2]}
liuzhe-lz's avatar
liuzhe-lz committed
72
    if [[ ${#COMP_WORDS[@]} -eq 2 ]]; then
Junwei Sun's avatar
Junwei Sun committed
73
        # completing first argument from __nnictl_cmds
liuzhe-lz's avatar
liuzhe-lz committed
74
75
        COMPREPLY=($(compgen -W "$__nnictl_cmds" -- "${COMP_WORDS[1]}"))

liuzhe-lz's avatar
liuzhe-lz committed
76
    elif [[ ${#COMP_WORDS[@]} -eq 3 ]]; then
liuzhe-lz's avatar
liuzhe-lz committed
77
78
79
80
        # completing second argument from __nnictl_${FirstArg}_cmds
        local args=__nnictl_${COMP_WORDS[1]}_cmds
        COMPREPLY=($(compgen -W "${!args}" -- "${COMP_WORDS[2]}"))

81
        # add experiment IDs to candidates if desired
liuzhe-lz's avatar
liuzhe-lz committed
82
        if [[ $__nnictl_2nd_expid_cmds =~ " ${COMP_WORDS[1]} " ]]; then
Junwei Sun's avatar
Junwei Sun committed
83
            local experiments=$(ls ~/nni-experiments 2>/dev/null)
84
85
86
            COMPREPLY+=($(compgen -W "$experiments" -- $cur))
        fi

Junwei Sun's avatar
Junwei Sun committed
87
    elif [[ $last != -* || $last == --debug || $last == --foreground || $last == --intermediate || $last == --all ]]; then
liuzhe-lz's avatar
liuzhe-lz committed
88
        # last argument does not starts with "-", so this one is likely to be "--xxx"
89
        local args=__nnictl_${COMP_WORDS[1]}_${COMP_WORDS[2]}_cmds
Junwei Sun's avatar
Junwei Sun committed
90
91
        if [[ $args =~ "-" || $__nnictl_2nd_expid_cmds =~ " ${COMP_WORDS[1]} " ]]; then
            # the second argument starts with "-" or is an experiment id, use __nnictl_${FirstArg}_cmds
liuzhe-lz's avatar
liuzhe-lz committed
92
93
94
95
            local args=__nnictl_${COMP_WORDS[1]}_cmds
        fi
        # remove already set arguments from candidates
        local remain_args=$(__nnictl_remain_args ${args})
96
97
98
99
100
        COMPREPLY=($(compgen -W "$remain_args" -- $cur))

        # if this is 3rd arguments, try adding experiment IDs to candidates
        if [[ ${#COMP_WORDS[@]} -eq 4 ]]; then
            if [[ $__nnictl_3rd_expid_cmds =~ " ${COMP_WORDS[1]} " && ${COMP_WORDS[2]} != "list" ]]; then
Junwei Sun's avatar
Junwei Sun committed
101
                local experiments=$(ls ~/nni-experiments 2>/dev/null)
102
103
104
105
                COMPREPLY+=($(compgen -W "$experiments" -- $cur))
            fi
        fi

Junwei Sun's avatar
Junwei Sun committed
106
    elif [[ ${COMP_WORDS[2]} == "export" ]]; then
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        # "export" command is somewhat unique
        if [[ " --type -t " =~ " $last " ]]; then
            COMPREPLY=($(compgen -W "json csv" -- $cur))

        elif [[ " --filename -f " =~ " $last " ]]; then
            # try to detect whether complete CSV file or JSON file
            [[ "$COMP_LINE" =~ "csv" ]] && local export_csv=1
            [[ "$COMP_LINE" =~ "json" ]] && local export_json=1
            if [[ -n $export_csv && -z $export_json ]]; then
                local ext="csv"         # CSV only
            elif [[ -n $export_json && -z $export_csv ]]; then
                local ext="json"        # JSON only
            else
                local ext="@(csv|json)" # both
            fi
            __nnictl_complete_extension "$ext"
        fi

Junwei Sun's avatar
Junwei Sun committed
125
    elif [[ " --trial_id -T " =~ " $last " ]]; then
126
        # complete trial ID
Junwei Sun's avatar
Junwei Sun committed
127
128
129
130
        if [[ -e ${HOME}/nni-experiments/${COMP_WORDS[2]} ]]; then
            local trials=$(ls -d ~/nni-experiments/${COMP_WORDS[2]}/trials/* 2>/dev/null | grep -o '[^/]*$')
        elif [[ -e "${HOME}/nni-experiments/${COMP_WORDS[3]}" ]]; then
            local trials=$(ls -d ~/nni-experiments/${COMP_WORDS[3]}/trials/* 2>/dev/null | grep -o '[^/]*$')
131
        else
Junwei Sun's avatar
Junwei Sun committed
132
            local trials=$(ls -d ~/nni-experiments/*/trials/* 2>/dev/null | grep -o '[^/]*$')
133
134
        fi
        COMPREPLY=($(compgen -W "$trials" -- $cur))
liuzhe-lz's avatar
liuzhe-lz committed
135

136
137
138
    elif [[ " --config -c " =~ " $last " ]]; then
        # complete YAML file
        __nnictl_complete_extension "@(yml|yaml)"
liuzhe-lz's avatar
liuzhe-lz committed
139

140
141
142
    elif [[ " --filename -f " =~ " $last " ]]; then
        # complete JSON file
        __nnictl_complete_extension "json"
liuzhe-lz's avatar
liuzhe-lz committed
143
144
145
146

    fi
}

liuzhe-lz's avatar
liuzhe-lz committed
147
148
149
150
151
if [[ ${BASH_VERSINFO[0]} -le 4 && ${BASH_VERSINFO[1]} -le 4 ]]; then
    complete -F _nnictl nnictl
else
    complete -o nosort -F _nnictl nnictl
fi