Parsing command line arguments in .NET Core

1 -> Experimenting with the Kudu API

2 -> Building and Packaging .NET Core with AppVeyor

3 -> Parsing command line arguments in .NET Core

I’m working on a little command line too called k-scratch that allows you to interact with the Kudu API to pull and push files (and monitor them locally for upload) as well as watch the log stream – all from the command prompt.

Now that I’ve made the decision that this project will not be a UWP, I’m refocusing on the console app.

How to parse?

I realised my app was going to do more than the basic args[] input and I’d need to do some work to parse and organise the command line inputs in to recognisable commands.

I had a hunt around and found a few things.

I started off investigating commandline by gsscoder. I managed to get it going, but I had some troubles with .NET Core. Their current published nuget packages do not support .NET Core. They have a branch, which worked – but without a working build I can reference in my app it’s a bit too much overhead to manage for this project.

I started out forking commandline and modifying the AppVeyor.yml file to get a nuget package out – which worked, but ran in to a few issues down the line which had me searching around for another solution.

I came across this thread on the dotnet cli page after @GeoffreyHuntley suggested I search for it.

//platform.twitter.com/widgets.js

Great, so I went and found System.CommandLine which is the component that the dotnet CLI is using (seemingly!). It’s not published as part of the main core framework – it’s in the labs. It’s also not on the main Nuget, only on MyGet.

I had to adjust my nuget package sources to add the myget feed

> nuget sources add -name "Corefxlab" -Source "https://dotnet.myget.org/F/dotnet-corefxlab/"

I also did the same in my AppVeyor.yml file which worked nicely.

before_build:
– nuget sources add -name "Corefxlab" -Source "https://dotnet.myget.org/F/dotnet-corefxlab/"

Great now the package is installed!

I’ve so far had a play around with it working in .NET Core from the samples on the System.CommandLine site.

It’s super easy to use. You have Commands and Options. A command will come paired with following options until another command is raised.

ks pull -p
ks commit -m -amend

etc.

var command = string.Empty;
var prune = false;
var message = string.Empty;
var amend = false;

ArgumentSyntax.Parse(args, syntax =>
{
    //syntax.DefineOption("n|name", ref addressee, "The addressee to greet");

    syntax.DefineCommand("pull", ref command, "Pull from another repo");
    syntax.DefineOption("p|prune", ref prune, "Prune branches");

    syntax.DefineCommand("commit", ref command, "Committing changes");
    syntax.DefineOption("m|message", ref message, "The message to use");
    syntax.DefineOption("amend", ref amend, "Amend existing commit");
});

Console.WriteLine($"Command {command}, Prune {prune}, Message {message}, Amend {amend}");

Now to go and make it do stuff!

2 thoughts on “Parsing command line arguments in .NET Core

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s