.NET Core 命令行接口 (CLI) 工具.NET Core command-line interface (CLI) tools

.NET Core 命令行接口 (CLI) 工具是用于开发 .NET 应用程序的新型跨平台工具链。The .NET Core command-line interface (CLI) is a new cross-platform toolchain for developing .NET applications. CLI 是更高级别的工具(如集成开发环境 (IDE)、编辑器和生成协调程序)可以驻留的基础。The CLI is a foundation upon which higher-level tools, such as Integrated Development Environments (IDEs), editors, and build orchestrators, can rest.

安装Installation

使用本机安装程序或使用安装 shell 脚本:Either use the native installers or use the installation shell scripts:

  • 本机安装程序主要用于开发人员的计算机,并使用每个受支持的平台的本机安装机制,例如,Ubuntu 上的 DEB 程序包或 Windows 上的 MSI 捆绑包。The native installers are primarily used on developer's machines and use each supported platform's native install mechanism, for instance, DEB packages on Ubuntu or MSI bundles on Windows. 这些安装程序安装并配置环境,供开发人员立即使用,但是需要具有该计算机上的管理权限。These installers install and configure the environment for immediate use by the developer but require administrative privileges on the machine. 可以在 .NET Core 安装指南中查看安装说明。You can view the installation instructions in the .NET Core installation guide.
  • Shell 脚本主要用于设置生成服务器或希望安装工具但没有管理权限的情况。Shell scripts are primarily used for setting up build servers or when you wish to install the tools without administrative privileges. 安装脚本不会在计算机上安装先决条件,必须手动安装它。Install scripts don't install prerequisites on the machine, which must be installed manually. 有关详细信息,请参阅安装脚本引用主题For more information, see the install script reference topic. 有关如何在你的持续集成 (CI) 生成服务器上设置 CLI 的信息,请参阅在持续集成 (CI) 中使用 .NET Core SDK 和工具For information on how to set up CLI on your continuous integration (CI) build server, see Using .NET Core SDK and tools in Continuous Integration (CI).

在默认情况下,SLI 以并行 (SxS) 方式安装,因此,因此,CLI 工具的多个版本可以在一个计算机上共存。By default, the CLI installs in a side-by-side (SxS) manner, so multiple versions of the CLI tools can coexist on a single machine. 有关确定在安装了多个版本的计算机上所使用的版本的类型,在驱动程序部分中对此有更详尽的介绍。Determining which version is used on a machine where multiple versions are installed is explained in more detail in the Driver section.

CLI 命令CLI commands

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

CLI 采用可使你为项目指定其他工具的扩展性模型。The CLI adopts an extensibility model that allows you to specify additional tools for your projects. 有关详细信息,请参阅 .NET Core CLI 扩展性模型主题。For more information, see the .NET Core CLI extensibility model topic.

命令结构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 Core 运行时,请使用 --fx-version <VERSION> 选项(请参阅 dotnet 命令参考)。If you want to use a specific version of the .NET Core 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', 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.

从 project.json 迁移Migration from project.json

如果使用预览版 2 工具生成基于 project.json 的项目,请查阅 dotnet 迁移主题,了解将你的项目迁移到 MSBuild/ .csproj 以使用版本工具的信息。If you used Preview 2 tooling to produce project.json-based projects, consult the dotnet migrate topic for information on migrating your project to MSBuild/.csproj for use with release tooling. 对于预览版 2 工具发布之前所创建的 .NET Core 项目,请按照从 DNX 迁移到 .NET Core CLI (project.json) 中的指南手动更新项目,然后使用 dotnet migrate 或直接升级项目。For .NET Core projects created prior to the release of Preview 2 tooling, either manually update the project following the guidance in Migrating from DNX to .NET Core CLI (project.json) and then use dotnet migrate or directly upgrade your projects.

请参阅See also