__nnictl_cmds="create resume update stop trial webui experiment config rest log" __nnictl_create_cmds="--config --webuiport" __nnictl_resume_cmds="--experiment --manager --webuiport" __nnictl_update_cmds="searchspace concurrency duration" __nnictl_update_searchspace_cmds="--filename" __nnictl_update_concurrency_cmds="--value" __nnictl_update_duration_cmds="--value" __nnictl_trial_cmds="ls kill" __nnictl_trial_kill_cmds="--trialid" __nnictl_webui_cmds="start stop url" __nnictl_webui_start_cmds="--port" __nnictl_experiment_cmds="show" __nnictl_config_cmds="show" __nnictl_rest_cmds="check" __nnictl_log_cmds="stdout stderr" __nnictl_log_stdout_cmds="--tail --head --path" __nnictl_log_stderr_cmds="--tail --head --path" __nnictl_remain_args() { ret=${!1} # ret = $__nnictl_xxx_cmds # for arg in COMP_WORDS[:-1]: for arg in "${COMP_WORDS[@]::${#COMP_WORDS[@]}-1}"; do # remove previously set argument from ret if [[ $arg == --* ]]; then ret=${ret/$arg/} fi done echo $ret } _nnictl() { _words_cnt=${#COMP_WORDS[@]} if [ $_words_cnt == 1 ]; then # no argument input, list all commands complete -W "$_nnictl_cmds" elif [ $_words_cnt == 2 ]; then # completing frst argument from __nnictl_cmds COMPREPLY=($(compgen -W "$__nnictl_cmds" -- "${COMP_WORDS[1]}")) elif [ $_words_cnt == 3 ]; then # completing second argument from __nnictl_${FirstArg}_cmds args=__nnictl_${COMP_WORDS[1]}_cmds COMPREPLY=($(compgen -W "${!args}" -- "${COMP_WORDS[2]}")) elif [[ ${COMP_WORDS[-2]} != -* ]]; then # last argument does not starts with "-", so this one is likely to be "--xxx" if [[ ${COMP_WORDS[2]} == -* ]]; then # second argument starts with "-", use __nnictl_${FirstArg}_cmds args=__nnictl_${COMP_WORDS[1]}_cmds else # second argument is a word, use __nnictl_${FirstArg}_{SecondArg}_cmds args=__nnictl_${COMP_WORDS[1]}_${COMP_WORDS[2]}_cmds fi # remove already set arguments from candidates remain_args=$(__nnictl_remain_args ${args}) COMPREPLY=($(compgen -W "$remain_args" -- "${COMP_WORDS[-1]}")) fi } complete -F _nnictl nnictl