This article applies to: ✔️ .NET Core 2.1 SDK and later versions
This tutorial teaches you how to create and package a .NET tool. The .NET CLI lets you create a console application as a tool, which others can install and run. .NET tools are NuGet packages that are installed from the .NET CLI. For more information about tools, see .NET tools overview.
The tool that you'll create is a console application that takes a message as input and displays the message along with lines of text that create the image of a robot.
This is the first in a series of three tutorials. In this tutorial, you create and package a tool. In the next two tutorials you use the tool as a global tool and use the tool as a local tool. The procedures for creating a tool are the same whether you use it as a global tool or as a local tool.
This tutorial uses .NET SDK 6.0, but global tools are available starting in .NET Core SDK 2.1. Local tools are available starting in .NET Core SDK 3.0.
A text editor or code editor of your choice.
Create a project
Open a command prompt and create a folder named repository.
Navigate to the repository folder and enter the following command:
.NET CLI
dotnetnew console -n microsoft.botsay -f net6.0
The command creates a new folder named microsoft.botsay under the repository folder.
Note
For this tutorial you create a tool that targets .NET 6.0. To target a different framework, change the -f|--framework option. To target multiple frameworks, change the TargetFramework element to a TargetFrameworks element in the project file, as shown in the following example:
If no arguments are passed, a short help message is displayed. Otherwise, all of the arguments are concatenated into a single string and printed by calling the ShowBot method that you create in the next step.
Add a new method named ShowBot that takes a string parameter. The method prints out the message and an image of a robot using lines of text.
<ToolCommandName> is an optional element that specifies the command that will invoke the tool after it's installed. If this element isn't provided, the command name for the tool is the assembly name, which is typically the project file name without the .csproj extension.
<PackageOutputPath> is an optional element that determines where the NuGet package will be produced. The NuGet package is what the .NET CLI uses to install your tool.
The project file now looks like the following example:
Create a NuGet package by running the dotnet pack command:
.NET CLI
dotnetpack
The microsoft.botsay.1.0.0.nupkg file is created in the folder identified by the <PackageOutputPath> value from the microsoft.botsay.csproj file, which in this example is the ./nupkg folder.
When you want to release a tool publicly, you can upload it to https://www.nuget.org. Once the tool is available on NuGet, developers can install the tool by using the dotnet tool install command. For this tutorial you install the package directly from the local nupkg folder, so there's no need to upload the package to NuGet.
In this tutorial, you created a console application and packaged it as a tool. To learn how to use the tool as a global tool, advance to the next tutorial.
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Create a .NET project and learn to add packages and manage package dependencies in your project. Use the .NET Core CLI and NuGet registry to add libraries and tools to your C# applications through Visual Studio Code.