Commit c0285d0b authored by one's avatar one
Browse files

[shell] Update hy-smi bash-completion

parent f93c64de
...@@ -4,32 +4,30 @@ _hy_smi_completion() { ...@@ -4,32 +4,30 @@ _hy_smi_completion() {
local cur prev subcmds local cur prev subcmds
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" [[ $COMP_CWORD -gt 0 ]] && prev="${COMP_WORDS[COMP_CWORD-1]}" || prev=""
# Subcommands # Subcommands (distinct from --mig/--multi-instance-gpu set option)
subcmds="virtual mig" subcmds="virtual mig"
# If cache variable _HY_SMI_OPTS is empty, parse options from help output. # Lazily parse options from help output on first invocation.
# To force a refresh after upgrading hy-smi, run: unset _HY_SMI_OPTS
if [[ -z "$_HY_SMI_OPTS" ]]; then if [[ -z "$_HY_SMI_OPTS" ]]; then
# Extract only valid short/long options (e.g. -h, --help), # Match both GNU-style long options (--foo-bar) and
# avoiding punctuation-contaminated tokens like -h' or --foo. # tool-specific single-dash multi-char options (-idmon, -sdmon, etc.)
export _HY_SMI_OPTS=$(hy-smi -h 2>/dev/null | awk '{ _HY_SMI_OPTS=$(hy-smi -h 2>/dev/null \
for(i=1;i<=NF;i++) { | grep -oP '(?<!\w)(--[A-Za-z][A-Za-z0-9-]*|-[A-Za-z][A-Za-z0-9]*)' \
token=$i | sort -u)
while (match(token, /--[A-Za-z0-9][A-Za-z0-9-]*|-[A-Za-z0-9]/)) {
print substr(token, RSTART, RLENGTH)
token=substr(token, RSTART + RLENGTH)
}
}
}' | sort -u)
fi fi
# Provide intelligent suggestions for specific parameters (prev) # Provide completions for options that take specific values
case "${prev}" in case "${prev}" in
# File arguments
--load|--save|--filename|-fdmon) --load|--save|--filename|-fdmon)
COMPREPLY=( $(compgen -f -- "${cur}") ) COMPREPLY=( $(compgen -f -- "${cur}") )
return 0 return 0
;; ;;
# Enumerated string values
--loglevel) --loglevel)
COMPREPLY=( $(compgen -W "debug info warning error critical" -- "${cur}") ) COMPREPLY=( $(compgen -W "debug info warning error critical" -- "${cur}") )
return 0 return 0
...@@ -42,6 +40,17 @@ _hy_smi_completion() { ...@@ -42,6 +40,17 @@ _hy_smi_completion() {
COMPREPLY=( $(compgen -W "on off stable" -- "${cur}") ) COMPREPLY=( $(compgen -W "on off stable" -- "${cur}") )
return 0 return 0
;; ;;
-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
--direction) --direction)
COMPREPLY=( $(compgen -W "0 1" -- "${cur}") ) COMPREPLY=( $(compgen -W "0 1" -- "${cur}") )
return 0 return 0
...@@ -50,19 +59,32 @@ _hy_smi_completion() { ...@@ -50,19 +59,32 @@ _hy_smi_completion() {
COMPREPLY=( $(compgen -W "0 1" -- "${cur}") ) COMPREPLY=( $(compgen -W "0 1" -- "${cur}") )
return 0 return 0
;; ;;
# Multi-value enumerations
--setboost) --setboost)
COMPREPLY=( $(compgen -W "0-AI(default), 1-HPC(default), 2-AI(typical), 3-HPC(typical)" -- "${cur}") ) # 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}") )
return 0 return 0
;; ;;
esac esac
if [[ ${cur} == -* ]] ; then # Complete flags and subcommands
# Only complete the dynamically extracted flags
COMPREPLY=( $(compgen -W "${_HY_SMI_OPTS}" -- "${cur}") )
return 0
fi
# Default complete all flags and subcommands
COMPREPLY=( $(compgen -W "${_HY_SMI_OPTS} ${subcmds}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${_HY_SMI_OPTS} ${subcmds}" -- "${cur}") )
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment