开发手册 欢迎您!
软件开发者资料库

.NET Core CLI在PowerShell、bash、zsh中命令自动补全的方法

命令行模式下可以很方便的使用TAB键进行命令补全,从.NET Core 2.0 SDK 开始,NET Core CLI 支持 tab 自动补全。本文介绍PowerShell、Bash 和 zsh 配置 tab 自动补全的方法。

PowerShell中命令自动补全

要将 tab 自动补全添加到适用于 .NET Core CLI 的 PowerShell,请创立或编辑存储在变量 $PROFILE 中的配置文件 。 有关详细信息,请参阅如何创建配置文件配置文件和执行策略

将如下代码添加到配置文件中:

# PowerShell 中为dotnet CLI的参数完成补全Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {     param($commandName, $wordToComplete, $cursorPosition)         dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)         } }

bash中命令自动补全

使用tab 自动补全 在.NET Core CLI 的 bash shell中,请将以下代码添加到.bashrc文件:

# bash 中为 dotnet CLI的参数完成补全_dotnet_bash_complete(){  local word=${COMP_WORDS[COMP_CWORD]}  local completions  completions="$(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)"  if [ $? -ne 0 ]; then    completions=""  fi  COMPREPLY=( $(compgen -W "$completions" -- "$word") )}complete -f -F _dotnet_bash_complete dotnet

zsh中命令自动补全

 使用tab 自动补全在 .NET Core CLI 的 zsh shell,请将如下代码添加到.zshrc文件:

# zsh中为 dotnet CLI的参数完成补全_dotnet_zsh_complete() {  local completions=("$(dotnet complete "$words")")  reply=( "${(ps:\n:)completions}" )}compctl -K _dotnet_zsh_complete dotnet