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

Published: 27 March 2018

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:

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:

If you found value in this blog post and want to return the favour, you can Buy me a coffee

PS: I publish a weekly newsletter for ASP.NET Developers called ASP.NET Weekly. If you want to get an email every Friday with all the best ASP.NET related blog posts from the previous week, please sign up!