g (Go Version Manager) 的 Fish 补全
g 是个好东西,然而 Fish 无法自动补全它的子命令,遂记录:
\~/.config/fish/completions/g.fish:
Fish
# --- 1. 语言辅助函数 ---
function __g_desc
# 判断环境语言是否为中文
if string match -q "zh_*" $LANG
echo $argv[1] # 返回中文描述
else
echo $argv[2] # 返回英文描述
end
end
# --- 2. 补全辅助函数 ---
function __fish_g_no_subcommand
set -l cmd (commandline -opc)
if test (count $cmd) -eq 1
return 0
end
return 1
end
function __fish_g_installed_versions
set -l versions (g ls 2>/dev/null | string replace -r '\*' '' | string trim | string match -rv '^(VERSION|INSTALLED|No versions|.*found|^$)')
end
# --- 3. 初始化 ---
complete -c g -f # 禁用默认文件补全
# --- 4. 全局选项 ---
complete -c g -s h -l help -d (__g_desc "显示帮助信息" "Show help")
complete -c g -s v -l version -d (__g_desc "显示版本号" "Print the version")
# --- 5. 子命令及其 Flag ---
# ls / l
complete -c g -n "__fish_g_no_subcommand" -a "ls l" -d (__g_desc "列出已安装版本" "List installed versions")
complete -c g -n "__fish_seen_subcommand_from ls l" -s o -l output -r -a "text json" -d (__g_desc "输出格式" "Output format")
# ls-remote / lr / lsr
complete -c g -n "__fish_g_no_subcommand" -a "ls-remote lr lsr" -d (__g_desc "列出远程可用的 Go 版本" "List remote versions")
complete -c g -n "__fish_seen_subcommand_from ls-remote lr lsr" -s o -l output -r -a "text json" -d (__g_desc "输出格式" "Output format")
complete -c g -n "__fish_seen_subcommand_from ls-remote lr lsr" -a "stable archived unstable" -d (__g_desc "版本类型" "Version type")
# use
complete -c g -n "__fish_g_no_subcommand" -a "use" -d (__g_desc "切换 Go 版本 (支持 go.mod)" "Switch Go version")
complete -c g -n "__fish_seen_subcommand_from use" -a "(__fish_g_installed_versions)"
# install / i
complete -c g -n "__fish_g_no_subcommand" -a "install i" -d (__g_desc "下载并安装版本" "Download and install a version")
complete -c g -n "__fish_seen_subcommand_from install i" -s n -l nouse -d (__g_desc "仅安装不切换" "Only install without using")
complete -c g -n "__fish_seen_subcommand_from install i" -l skip-checksum -d (__g_desc "跳过校验和验证" "Skip checksum verification")
# uninstall
complete -c g -n "__fish_g_no_subcommand" -a "uninstall" -d (__g_desc "卸载指定版本" "Uninstall a version")
complete -c g -n "__fish_seen_subcommand_from uninstall" -a "(__fish_g_installed_versions)"
# clean
complete -c g -n "__fish_g_no_subcommand" -a "clean" -d (__g_desc "清除下载缓存" "Remove files from package download directory")
# env
complete -c g -n "__fish_g_no_subcommand" -a "env" -d (__g_desc "显示环境变量" "Show env variables of g")
# self (带有子命令)
complete -c g -n "__fish_g_no_subcommand" -a "self" -d (__g_desc "管理 g 自身" "Modify g itself")
# self update
complete -c g -n "__fish_seen_subcommand_from self; and not __fish_seen_subcommand_from update uninstall" -a "update" -d (__g_desc "更新 g" "Download and install updates to g")
# self uninstall
complete -c g -n "__fish_seen_subcommand_from self; and not __fish_seen_subcommand_from update uninstall" -a "uninstall" -d (__g_desc "卸载 g" "Uninstall g")
# mcp
complete -c g -n "__fish_g_no_subcommand" -a "mcp" -d (__g_desc "以 mcp 服务器模式运行" "Run in mcp server mode")