.NET Core CLI

Introduction 

 
The .NET Core command-line interface (CLI) is a cross-platform for developing, building, running, and publishing .NET Core applications.
 

CLI commands

 
There are some commands which are installed by default:
 
Command Description
new Initialize .NET projects.
restore Restore dependencies specified in the .NET project.
build Builds a .NET project.
publish Publishes a .NET project for deployment (including the runtime).
run Compiles and immediately executes a .NET project.
test Runs unit tests using the test runner specified in the project.
pack Creates a NuGet package.
migrate Migrates a project.json based project to a msbuild based project
clean Clean build output(s).
sln Modify solution (SLN) files.
Project modification commands
add Add items to the project
remove Remove items from the project
list List items in the project
NuGet packages
 
Now go to cmd command prompt and type dotnet --help and enter.
 
You will see the below details:
  1. C:\windows\system32>dotnet --help
  2. .NET Core SDK (3.1.101)  
  3. Usage: dotnet [runtime-options] [path-to-application] [arguments]  
  4.   
  5. Execute a .NET Core application.  
  6.   
  7. runtime-options:  
  8.   --additionalprobingpath <path>   Path containing probing policy and assemblies to probe for.  
  9.   --additional-deps <path>         Path to additional deps.json file.  
  10.   --fx-version <version>           Version of the installed Shared Framework to use to run the application.  
  11.   --roll-forward <setting>         Roll forward to framework version  (LatestPatch, Minor, LatestMinor, Major, LatestMajor, Disable).  
  12.   
  13. path-to-application:  
  14.   The path to an application .dll file to execute.  
  15.   
  16. Usage: dotnet [sdk-options] [command] [command-options] [arguments]  
  17.   
  18. Execute a .NET Core SDK command.  
  19.   
  20. sdk-options:  
  21.   -d|--diagnostics  Enable diagnostic output.  
  22.   -h|--help         Show command line help.  
  23.   --info            Display .NET Core information.  
  24.   --list-runtimes   Display the installed runtimes.  
  25.   --list-sdks       Display the installed SDKs.  
  26.   --version         Display .NET Core SDK version in use.  
  27.   
  28. SDK commands:  
  29.   add               Add a package or reference to a .NET project.  
  30.   build             Build a .NET project.  
  31.   build-server      Interact with servers started by a build.  
  32.   clean             Clean build outputs of a .NET project.  
  33.   help              Show command line help.  
  34.   list              List project references of a .NET project.  
  35.   msbuild           Run Microsoft Build Engine (MSBuild) commands.  
  36.   new               Create a new .NET project or file.  
  37.   nuget             Provides additional NuGet commands.  
  38.   pack              Create a NuGet package.  
  39.   publish           Publish a .NET project for deployment.  
  40.   remove            Remove a package or reference from a .NET project.  
  41.   restore           Restore dependencies specified in a .NET project.  
  42.   run               Build and run a .NET project output.  
  43.   sln               Modify Visual Studio solution files.  
  44.   store             Store the specified assemblies in the runtime package store.  
  45.   test              Run unit tests using the test runner specified in a .NET project.  
  46.   tool              Install or manage tools that extend the .NET experience.  
  47.   vstest            Run Microsoft Test Engine (VSTest) commands.  
  48.   
  49. Additional commands from bundled tools:  
  50.   dev-certs         Create and manage development certificates.  
  51.   fsi               Start F# Interactive / execute F# scripts.  
  52.   sql-cache         SQL Server cache command-line tools.  
  53.   user-secrets      Manage development user secrets.  
  54.   watch             Start a file watcher that runs a command when files change.  
  55.   
  56. Run 'dotnet [command] --help' for more information on a command.  
 
 
 
 
Now let's do some practical example to create and build a new dotnet core application using dotnet core CLI (command-line interface)
 
create new application
 
The dotnet new command creates a .NET Core project as expected in your command prompt, as shown below:
 
I have given the console project name inside CliDemo
  1. D:\>pushd D:\CliDemo  
  2.   
  3. D:\CliDemo>dotnet new console  
The template "Console Application" was created successfully.
 
Processing post-creation actions...
 
Running 'dotnet restore' on D:\CliDemo\CliDemo.csproj...
 
Restore completed in 131.77 ms for D:\CliDemo\CliDemo.csproj.
 
Restore succeeded.
 
D:\CliDemo>
 
See the attached image:
 
 
Restore the Nuget package
 
We can restore all Nuget package, just enter the following command:
  1. dotnet restore  
Build a Project
 
This following command needs to be entered:
 
dotnet build
 
You will able to see as shown below:
  1. D:\CliDemo>dotnet build  
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
 
Copyright (C) Microsoft Corporation. All rights reserved.
 
Restore completed in 30.14 ms for D:\CliDemo\CliDemo.csproj.
 
CliDemo -> D:\CliDemo\bin\Debug\netcoreapp3.1\CliDemo.dll
 
Build succeeded.
 
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.18
D:\CliDemo>
 
See the attached image:
 
 
 
Run an application
 
This following command needs to be entered:
 
dotnet run
 
You will be able to see hello world like this:
  1. D:\CliDemo>dotnet run  
  2. Hello World!  
  3.   
  4. D:\CliDemo  
Note
I have created a console application. But we can create a lot another project as well, for example, class library project, webapl, MVC, WPF, WCF, etc.. as listed below:
 
Templates Short name
Console Application console
Class library classlib
WPF Application wpf
Windows Forms (WinForms) Application winforms
Unit Test Project mstest
NUnit 3 Test Project nunit
xUnit Test Project xunit
Razor Page page
MVC ViewImports viewimports
ASP.NET Core Empty web
ASP.NET Core Web App (Model-View-Controller) mvc
ASP.NET Core Web App webapp, razor
ASP.NET Core with Angular angular
ASP.NET Core with React.js react
ASP.NET Core with React.js and Redux reactredux
Razor Class Library razorclasslib
ASP.NET Core Web API webapi
ASP.NET Core gRPC Service grpc
 
Thank you for taking your valuable time to read the full article.