.NET CLI 概述.NET CLI overview

本文适用于: ✔️ .NET Core 2.1 SDK 及更高版本This article applies to: ✔️ .NET Core 2.1 SDK and later versions

.NET 命令行接口 (CLI) 工具是用于开发、生成、运行和发布 .NET 应用程序的跨平台工具链。The .NET command-line interface (CLI) is a cross-platform toolchain for developing, building, running, and publishing .NET applications.

.NET CLI 附带了 .NET SDKThe .NET CLI is included with the .NET SDK. 若要了解如何安装 .NET SDK,请参阅安装 .NET CoreTo learn how to install the .NET SDK, see Install .NET Core.

CLI 命令CLI commands

默认安装以下命令:The following commands are installed by default:

基本命令Basic commands

项目修改命令Project modification commands

高级命令Advanced commands

工具管理命令Tool management commands

工具是控制台应用程序,它们从 NuGet 包中安装并从命令提示符处进行调用。Tools are console applications that are installed from NuGet packages and are invoked from the command prompt. 你可自行编写工具,也可安装由第三方编写的工具。You can write tools yourself or install tools written by third parties. 工具也称为全局工具、工具路径工具和本地工具。Tools are also known as global tools, tool-path tools, and local tools. 有关详细信息,请参阅 .NET 工具概述For more information, see .NET tools overview.

命令结构Command structure

CLI 命令结构包含驱动程序(“dotnet”)命令,还可能包含命令参数选项CLI command structure consists of the driver ("dotnet"), the command, and possibly command arguments and options. 在大部分 CLI 操作中可看到此模式,例如创建新控制台应用并从命令行运行该应用,因为从名为 my_app 的目录中执行时,显示以下命令:You see this pattern in most CLI operations, such as creating a new console app and running it from the command line as the following commands show when executed from a directory named my_app:

dotnet new console
dotnet build --output /build_output
dotnet /build_output/my_app.dll

驱动程序Driver

驱动程序名为 dotnet,并具有两项职责,即运行依赖于框架的应用或执行命令。The driver is named dotnet and has two responsibilities, either running a framework-dependent app or executing a command.

若要运行依赖于框架的应用,请在驱动程序后指定应用,例如,dotnet /path/to/my_app.dllTo run a framework-dependent app, specify the app after the driver, for example, dotnet /path/to/my_app.dll. 从应用的 DLL 驻留的文件夹执行命令时,只需执行 dotnet my_app.dll 即可。When executing the command from the folder where the app's DLL resides, simply execute dotnet my_app.dll. 如果要使用特定版本的 .NET 运行时,请使用 --fx-version <VERSION> 选项(请参阅 dotnet 命令参考)。If you want to use a specific version of the .NET Runtime, use the --fx-version <VERSION> option (see the dotnet command reference).

为驱动程序提供命令时,dotnet.exe 启动 CLI 命令执行过程。When you supply a command to the driver, dotnet.exe starts the CLI command execution process. 例如:For example:

dotnet build

首先,驱动程序确定要使用的 SDK 版本。First, the driver determines the version of the SDK to use. 如果没有 global.json 文件,则使用可用的最新版本 SDK。If there is no global.json file, the latest version of the SDK available is used. 这有可能是预览版或稳定版,具体取决于计算机上的最新版本。This might be either a preview or stable version, depending on what is latest on the machine. 确定 SDK 版本后,它便会执行命令。Once the SDK version is determined, it executes the command.

命令Command

由命令执行操作。The command performs an action. 例如,dotnet build 生成代码。For example, dotnet build builds code. dotnet publish 发布代码。dotnet publish publishes code. 使用 dotnet {command} 约定将命令作为控制台应用程序实现。The commands are implemented as a console application using a dotnet {command} convention.

自变量Arguments

在命令行上传递的参数是被调用的命令的参数。The arguments you pass on the command line are the arguments to the command invoked. 例如,执行 dotnet publish my_app.csproj 时,my_app.csproj 参数指示要发布的项目,并被传递到 publish 命令。For example, when you execute dotnet publish my_app.csproj, the my_app.csproj argument indicates the project to publish and is passed to the publish command.

选项Options

在命令行上传递的选项是被调用的命令的选项。The options you pass on the command line are the options to the command invoked. 例如,执行 dotnet publish --output /build_output 时,--output 选项及其值被传递到 publish 命令。For example, when you execute dotnet publish --output /build_output, the --output option and its value are passed to the publish command.

请参阅See also