.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)、編輯器和建置 Orchestrator 等高層級工具所依靠的基礎。The CLI is a foundation upon which higher-level tools, such as Integrated Development Environments (IDEs), editors, and build orchestrators, can rest.

安裝Installation

使用原生安裝程式或使用安裝殼層指令碼: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 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).

根據預設,CLI 會以並存 (Side-By-Side, 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 (or "verb"), 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. 唯獨啟動應用程式時會不搭配命令使用 dotnetThe only time dotnet is used without a command is when it's used to start an application.

若要執行相依於架構的應用程式,請在驅動器之後指定應用程式,例如 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.

當您提供命令給驅動器時,dotnet.exe 會啟動 CLI 命令執行程序。When you supply a command to the driver, dotnet.exe starts the CLI command execution process. 首先,驅動器會決定要使用的 SDK 版本。First, the driver determines the version of the SDK to use. 如果命令選項中未指定版本,則驅動器會使用可用的最新版本。If the version isn't specified in the command options, the driver uses the latest version available. 若要指定已安裝之最新版本以外的版本,請使用 --fx-version <VERSION> 選項 (請參閱 dotnet 命令參考)。To specify a version other than the latest installed version, use the --fx-version <VERSION> option (see the dotnet command reference). 決定 SDK 版本之後,驅動器會執行命令。Once the SDK version is determined, the driver executes the command.

命令 (「動詞」)Command ("verb")

命令 (或「動詞」) 只是執行動作的命令。The command (or "verb") is simply a command that performs an action. 例如,dotnet build 會建置程式碼。For example, dotnet build builds your code. dotnet publish 會發行程式碼。dotnet publish publishes your code. 命令是使用 dotnet {verb} 慣例實作為主控台應用程式。The commands are implemented as a console application using a dotnet {verb} 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

如果您使用 Preview 2 工具來產生 project.json 型專案,請參閱 dotnet migrate 主題,來取得移轉專案至 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. 對於在 Preview 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