.NET Core CLI 概觀.NET Core CLI overview

本文適用于:✔️ .NET 核心 2.1 SDK 和更高版本This article applies to: ✔️ .NET Core 2.1 SDK and later versions

.NET Core 命令列介面 (CLI) 是一個跨平臺工具鏈,用於開發、構建、運行和發佈 .NET Core 應用程式。The .NET Core command-line interface (CLI) is a cross-platform toolchain for developing, building, running, and publishing .NET Core applications.

.NET 核心 CLI 包含在.NET 核心 SDK中。The .NET Core CLI is included with the .NET Core SDK. 要瞭解如何安裝 .NET 核心 SDK,請參閱安裝 .NET 核心 SDKTo learn how to install the .NET Core SDK, see Install the .NET Core SDK.

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 Core 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 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. 如果沒有全域.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.

CommandCommand

此命令會執行動作。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參數指示要發佈的專案,並將其傳遞給命令。 publishFor 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選項及其值將傳遞給命令。 publishFor example, when you execute dotnet publish --output /build_output, the --output option and its value are passed to the publish command.

另請參閱See also