dotnet tool exec

本文适用于: ✔️.NET 10.0.100 SDK 及更高版本

Name

dotnet tool exec - 下载并调用 .NET 工具,而无需永久安装该工具。

概要

dotnet tool exec <PACKAGE_NAME>[@<VERSION>]
    [--allow-roll-forward] [-a|--arch <ARCHITECTURE>]
    [--add-source <SOURCE>] [--configfile <FILE>] [--disable-parallel]
    [--framework <FRAMEWORK>] [--ignore-failed-sources] [--interactive]
    [--no-http-cache] [--prerelease]
    [-v|--verbosity <LEVEL>]
    [--] [<tool-arguments>...]

dotnet tool exec -h|--help

Description

dotnet tool exec 命令为 .NET 工具提供一次性工具调用模式。 它会自动将指定的工具包下载到 NuGet 缓存并调用它,而无需修改系统 PATH 或需要永久安装。

运行 dotnet tool exec时,命令:

  1. 根据配置的 NuGet 源检查指定的版本(或版本范围),以确定要下载的包(如果未指定最新版本)。
  2. 将指定的包下载到 NuGet 缓存(如果尚不存在)。
  3. 使用提供的任何参数调用该工具。
  4. 返回工具的退出代码。

dotnet tool exec 使用全局和本地工具无缝工作。 如果有可用的本地工具清单,它将使用清单来确定要运行的工具版本。

此命令还存在于其他两种形式中,以便于使用

  • dotnet dnx - 该 dotnet tool exec 别名的隐藏别名用作轻松实现 dnx 脚本本身的方法
  • dnx - 从 SDK 调用 dotnet dnx 的 shell 脚本。 此脚本由安装程序提供,在以下版本上 PATH可用。 它允许通过 dnx <toolname>直接使用非常简单的工具。

Arguments

  • PACKAGE_NAME

    要执行的 .NET 工具的 NuGet 包 ID。 可以选择使用 @ 语法指定版本,例如 dotnetsay@2.1.0

  • tool-arguments

    要传递给正在执行的工具的参数。 直接传递到工具后 -- 的所有内容。

选项

  • --allow-roll-forward

    如果该工具未安装面向的运行时,则允许该工具使用较新版本的 .NET 运行时。

  • --add-source <SOURCE>

    添加安装过程中要使用的其他 NuGet 包源。 源是并行访问的,而不是在回退级联序列中访问的。 如果多个源中提供了相同的包和版本,则最快的源将获胜。 有关详细信息,请参阅 安装 NuGet 包时会发生什么情况。 可以通过使用 NuGet 包源映射来控制这一点。 有关详细信息,请参阅 包源映射

  • --configfile <FILE>

    要使用的 NuGet 配置文件 (nuget.config)。 如果指定,则仅使用此文件中的设置。 如果未指定,则使用当前目录中的配置文件层次结构。 有关详细信息,请参阅常见的 NuGet 配置

  • --disable-parallel

    禁用并行查询配置的 NuGet 源。

  • --ignore-failed-sources

    将包源故障视为警告。

  • --interactive

    允许命令停止并等待用户输入或作,例如完成身份验证。 此选项默认为 true 命令检测到它正由用户直接运行时。

  • --no-http-cache

    不会将 HTTP 请求缓存到配置的 NuGet 源。

  • --prerelease

    允许在解析要安装的版本时选择预发行版包。

  • -v|--verbosity <LEVEL>

    设置命令的详细级别。 允许使用的值为 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]。 默认值为 normal

  • -?|-h|--help

    打印出有关如何使用命令的说明。

例子

  • dotnet tool exec dotnetsay

    下载(如有必要),并运行该工具的 dotnetsay 最新版本。

  • dotnet tool exec dotnetsay@2.1.0

    下载(如有必要)并运行该工具版本 2.1.0 dotnetsay

  • dotnet tool exec dotnetsay@2.*

    下载(如有必要),并在 2.x 版本范围内运行该工具的 dotnetsay 最新版本。

  • dotnet tool exec dotnetsay -- Hello World

    dotnetsay运行该工具并将“Hello World”作为参数传递给该工具。

  • dotnet tool exec --add-source https://api.nuget.org/v3/index.json mytool

    使用指定的 NuGet 源下载和运行 mytool

与其他命令的比较

此命令旨在成为使用 .NET 工具的统一方法。 虽然以前可用的工具安装命令仍然可用, dotnet tool exec 但为大多数用户提供了更简单、更灵活的体验。

Command 目的 Installation Scope
dotnet tool exec 一次性执行 无(仅缓存) Temporary
dotnet tool install -g 永久全局安装 全球 系统范围
dotnet tool install 永久本地安装 本地清单 项目
dotnet tool run 运行已安装的本地工具 需要事先安装 项目

对于想要永久安装工具的用户,该 dotnet tool install -g 命令仍然具有重要的用途。 但是,对于想要试用工具或在 CI/CD 管道中运行该工具的用户, dotnet tool exec 通常更合适。

另请参阅