CLI Instructions for .NET

Introduction

CLI (Command Line Interface) is a command-line interface in which we can execute instructions for compiling, testing, publishing, and running .NET applications, as well as managing references and Nuget packages in projects.

CLI comes with the .NET Core Software Development Kit (SDK). We will see 10 commonly used CLI instructions, which can be used from version 3.1 of .NET Core.

The command-line interface (CLI) instructions incorporated in scripts are frequently utilized to mechanize DevOps duties, during the process of development and continuous integration (CI/CD), with the aim of saving time by automating the execution of tasks.

To write these instructions, we will use the dotnet driver, which is used as follows:

dotnet <command> <arguments> <options> 

The command, arguments, and options are optional, but at least one of these pieces of information must be specified.

Getting help on commands

To get help using the dotnet driver, various commands are available that give the same result:

dotnet help 
dotnet -h 
dotnet -? 

To get help using a command presented below, you can use this instruction:

dotnet <command> --help 

As previously, the –help option can be replaced with -h or -?

The following command opens a web browser to display the online help for the command on learn.microsoft.com:

dotnet help <command>

Creating a project

The following instruction template is used to create a new project:

dotnet new <project template name> -o <directory name> 

Thus, to create a console project in the d:\projects\appConsole folder, we can write:

 dotnet new console -o d:\projects\appConsole 

The directory structure is created, and the appConsole directory contains the project file (csproj), as well as the C# code file containing the Program class.

Restoring dependencies

dotnet restore 

The restore command restores dependencies for a project explicitly. This restoration is also done implicitly with the new, build, build-server, run, test, publish, and pack commands.

Generating assemblies

dotnet build 

The build command generates assemblies. When executed in a solution directory, it generates assemblies for all projects in the solution. When executed from a project directory, it generates the assembly for that project as well as any referenced projects.

Running an assembly

dotnet run 

After positioning yourself in the directory of a project, this command allows you to run the assembly generated from that project. If the project’s assembly has not been generated, the build command is executed implicitly.

Testing an assembly

dotnet test 

The test command runs tests contained in a test project.

Creating a Nuget package

dotnet pack 

The pack command creates a Nuget package. By default, the Nuget package (file extension « nupkg ») is generated in the directory corresponding to the build mode (debug/release) of the bin directory.

Cleaning the project or solution

dotnet clean 

The clean command deletes the contents of the folders contained in the obj and bin directories. In these directories, if the application has been compiled with different build modes (e.g., debug/release), it is possible to target this mode:

dotnet clean --configuration Release

Managing solutions

dotnet sln 

A solution is a group of projects. The sln command is used to manage solutions. For example, the following instruction creates a new solution with the name of the current folder:

dotnet new sln 

The following instruction adds the project contained in the appConsole folder to the solution:

dotnet sln add ..\appConsole 

The following instructions allow you to remove a project from the solution:

dotnet sln remove ..\appConsole

Managing references

dotnet list reference

dotnet add reference

dotnet remove reference

The above commands respectively list, add and remove project references in a project.

For example, the following instruction adds a reference to the « lib » project in the « app » project:

dotnet add app/app.csproj reference lib/lib.csproj

Managing Nuget packages dotnet list package

dotnet add package

dotnet remove package

The above commands respectively list, add and remove Nuget package references in a project.

For example, the following instruction adds a reference to version 5.0.0 of the System.Reactive Nuget package in the « app » project:

dotnet add app/app.csproj package System.Reactive -v5.0.0

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *