March 27, 2018

Quick Tip: Pass arguments to your app when using the .NET CLI

I am playing around with developing a .NET Core Global tool (which is coming in .NET Core 2.1), and I am allowing users to pass command line arguments to my tool which I parse using Nate McMasterโ€™s CommandLineUtils library.

At this stage of development I am not installing my app as a Global Tool yet, but instead just running it using the standard dotnet run command.

One of the options a user can pass to my app is a -h option which will print out the list of commands. So, I ran dotnet run -h, and this is the output I saw:

dotnet cli help

As you will notice, that is not the help text from my application, but instead the help text from the .NET Core CLI. To pass arguments to your application, you need to pass a -- argument, and then the arguments to your application. As per the .NET Core CLI documentation, -- delimits arguments to dotnet run from arguments for the application being run. All arguments after this one are passed to the application run.

So in other words, if I want to pass the -h argument to my application, I need to do it as follows:

dotnet run -- -h

And with that, I get the correct output:

dotnet app help