Member-only story
Creating a Custom .NET Core Global Tool

We have been using quite a lot of .NET Core command line interface (CLI) tools, such as dotnet new
, dotnet build
, dotnet add package
, dotnet watch run
, dotnet ef database update
, and so on. Have you ever thought about building a similar CLI tool of you own?
In the .NET Core world, these tools are called Global Tools (link). A .NET Core Global Tool is a special NuGet package that contains a Console application, which can be invoked in a terminal via predefined commands in the Global Tool.
When talking about the CLI program in .NET, we might immediately consider Console applications (link) and PowerShell applications (PowerShell binary modules (link) and PowerShell Cmdlets (link)). Let’s briefly talk about them first.
PowerShell applications are easy to use, because the command line gives hints about the available commands, sub-commands and parameters in the PowerShell terminal and/or the PowerShell ISE. PowerShell also supports scripting with Windows management (link), data transformation, pipelines (link), and so on. We can build our CLI programs in C# and integrate them with PowerShell scripts that are used to manage computer systems or process data. However, despite of all those goodies in PowerShell, PowerShell applications in C# rose up for a short period then died out in my work place, because the debugging experience was not pleasant. Currently, all system administration and data process jobs are preferably written in pure PowerShell scripts.
On the other hand, the Console applications are easy to debug and test, even though the command line arguments parsing is not as good as that in PowerShell. The good news is that .NET is developing a set of native Command Line APIs in the System.CommandLine
namespace (link). Also, since .NET Core 2.1 arrived, we can publish our applications as self-contained applications (link), so that a Console app can really be built once and executed cross-platform without worrying about the operating systems and .NET Core versions. Moreover, with the release of .NET Core 3.0, we can publish our .NET Core applications as single-file executables (link), so that a Console app can be bundled as a single executable file, which improves the deployment and usage experiences.